Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions collector/internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,22 @@ func getConfig(logger *zap.Logger) string {
return defaultVal
}

func NewCollector(logger *zap.Logger, factories otelcol.Factories, version string) *Collector {
func NewCollector(logger *zap.Logger, factories otelcol.Factories, version string, customConverters []confmap.ConverterFactory) *Collector {
l := logger.Named("NewCollector")

// Combine built-in converters with custom converters
converters := []confmap.ConverterFactory{
confmap.NewConverterFactory(func(set confmap.ConverterSettings) confmap.Converter {
return disablequeuedretryconverter.New()
}),
}
converters = append(converters, customConverters...)

cfgSet := otelcol.ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{getConfig(l)},
ProviderFactories: []confmap.ProviderFactory{fileprovider.NewFactory(), envprovider.NewFactory(), yamlprovider.NewFactory(), httpsprovider.NewFactory(), httpprovider.NewFactory(), s3provider.NewFactory(), secretsmanagerprovider.NewFactory()},
ConverterFactories: []confmap.ConverterFactory{
confmap.NewConverterFactory(func(set confmap.ConverterSettings) confmap.Converter {
return disablequeuedretryconverter.New()
}),
},
ConverterFactories: converters,
},
}

Expand Down
103 changes: 103 additions & 0 deletions collector/internal/confmap/converter/accountidprocessor/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// The accountidprocessor implements the Converter for mutating Collector
// configurations to automatically inject the cloud.account.id attribute
// via a resource processor into all pipelines.
package accountidprocessor

import (
"context"
"fmt"

"go.opentelemetry.io/collector/confmap"
)

const (
serviceKey = "service"
pipelinesKey = "pipelines"
processorsKey = "processors"
resourceProc = "resource/aws-account-id"
accountIDAttrKey = "cloud.account.id"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you know if those strings already exists in a semver convention naming package? I'm not sure for other keys, but a quick search for this one: https://github.com/open-telemetry/opentelemetry-go/blob/a0a0acdceb0608265890ab6d983e9d1f7b73e735/semconv/v1.4.0/resource.go#L25 shows that we are already exporting this one, let's reuse it?

)

type converter struct {
accountID string
}

// New returns a confmap.Converter that injects cloud.account.id into all pipelines
func New(accountID string) confmap.Converter {
return &converter{accountID: accountID}
}

func (c converter) Convert(_ context.Context, conf *confmap.Conf) error {
Copy link
Member

@maxday maxday Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can tweak this function a bit:

  • use `(c* converter) in the definition
  • simplify a bit the for loop
  • remove the usage of sprintf

This will give something like:

func (c *converter) Convert(_ context.Context, conf *confmap.Conf) error {
	if c.accountID == "" {
		return nil
	}

	svc, ok := conf.Get(serviceKey).(map[string]interface{})
	if !ok {
		return nil
	}

	pipes, ok := svc[pipelinesKey].(map[string]interface{})
	if !ok {
		return nil
	}

	for _, v := range pipes {
		p, ok := v.(map[string]interface{})
		if !ok {
			continue
		}

		raw, _ := p[processorsKey]
		procs, _ := raw.([]interface{})
		p[processorsKey] = append([]interface{}{resourceProc}, procs...)
	}

	resourceProcessor := map[string]interface{}{
		resourceProc: map[string]interface{}{
			"attributes": []map[string]interface{}{
				{
					"key":    accountIDAttrKey,
					"value":  c.accountID,
					"action": "insert",
				},
			},
		},
	}

	return conf.Merge(confmap.NewFromStringMap(map[string]interface{}{
		"processors": resourceProcessor,
	}))
}

WDYT?

if c.accountID == "" {
return nil // Skip if no account ID
}

// Navigate to service.pipelines
serviceVal := conf.Get(serviceKey)
service, ok := serviceVal.(map[string]interface{})
if !ok {
return nil
}

pipelinesVal, ok := service[pipelinesKey]
if !ok {
return nil
}

pipelines, ok := pipelinesVal.(map[string]interface{})
if !ok {
return nil
}

updates := make(map[string]interface{})

// For each pipeline, add resource processor to beginning
for telemetryType, pipelineVal := range pipelines {
pipeline, ok := pipelineVal.(map[string]interface{})
if !ok {
continue
}

processorsVal, _ := pipeline[processorsKey]
processors, ok := processorsVal.([]interface{})
if !ok {
processors = []interface{}{}
}

// Prepend resource/aws-account-id processor
processors = append([]interface{}{resourceProc}, processors...)
updates[fmt.Sprintf("%s::%s::%s::%s", serviceKey, pipelinesKey, telemetryType, processorsKey)] = processors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm by no means a golang / otel collector expert. But I compared the way this update is being done to how the converter for the decouple processor works. It's idempotent in a way, because there it is checked whether the processor should be added instead of blindly adding it: https://github.com/open-telemetry/opentelemetry-lambda/blob/main/collector/internal/confmap/converter/decoupleafterbatchconverter/converter.go#L82-L86 However I'm guessing that is less relevant here as we do not expect users to define an accountIdProcessor themselves? And instead we assume that this processor will always be added once to each pipeline?

}

// Configure the resource processor with cloud.account.id attribute
updates[fmt.Sprintf("processors::%s::attributes", resourceProc)] = []map[string]interface{}{
{
"key": accountIDAttrKey,
"value": c.accountID,
"action": "insert",
},
}

// Apply all updates
if len(updates) > 0 {
if err := conf.Merge(confmap.NewFromStringMap(updates)); err != nil {
return err
}
}

return nil
}
Loading
Loading