Skip to content

Commit

Permalink
adding handlers to containerinsightreciever and resourcedetectionproc…
Browse files Browse the repository at this point in the history
…essors
  • Loading branch information
Paramadon committed Dec 9, 2024
1 parent 66e9942 commit d63bbf4
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ require (
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/aliyun/aliyun-log-go-sdk v0.1.76 // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20240419190856-2f880467f335 // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20241204155332-be45e31638de // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20240415183253-230331014d2c // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/apache/arrow/go/v15 v15.0.0 // indirect
Expand Down
66 changes: 36 additions & 30 deletions exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"fmt"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
"strings"
"sync"

Expand All @@ -21,7 +22,6 @@ import (
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter/internal/appsignals"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs"
)

Expand All @@ -39,6 +39,7 @@ type emfExporter struct {
pusherMap map[cwlogs.StreamKey]cwlogs.Pusher
svcStructuredLog *cwlogs.Client
config *Config
set exporter.Settings

metricTranslator metricTranslator

Expand All @@ -57,42 +58,18 @@ func newEmfExporter(config *Config, set exporter.Settings) (*emfExporter, error)

config.logger = set.Logger

// create AWS session
awsConfig, session, err := awsutil.GetAWSConfigSession(set.Logger, &awsutil.Conn{}, &config.AWSSessionSettings)
if err != nil {
return nil, err
}

// create CWLogs client with aws session config
svcStructuredLog := cwlogs.NewClient(set.Logger,
awsConfig,
set.BuildInfo,
config.LogGroupName,
config.LogRetention,
config.Tags,
session,
cwlogs.WithEnabledContainerInsights(config.IsEnhancedContainerInsights()),
cwlogs.WithEnabledAppSignals(config.IsAppSignalsEnabled()),
)

collectorIdentifier, err := uuid.NewRandom()
if err != nil {
return nil, err
}

// Initialize emfExporter without AWS session and structured logs
emfExporter := &emfExporter{
svcStructuredLog: svcStructuredLog,
config: config,
metricTranslator: newMetricTranslator(*config),
retryCnt: *awsConfig.MaxRetries,
collectorID: collectorIdentifier.String(),
retryCnt: config.AWSSessionSettings.MaxRetries,
collectorID: uuid.New().String(),
pusherMap: map[cwlogs.StreamKey]cwlogs.Pusher{},
processResourceLabels: func(map[string]string) {},
}

if config.IsAppSignalsEnabled() {
userAgent := appsignals.NewUserAgent()
svcStructuredLog.Handlers().Build.PushBackNamed(userAgent.Handler())
emfExporter.processResourceLabels = userAgent.Process
}

Expand Down Expand Up @@ -196,10 +173,39 @@ func (emf *emfExporter) listPushers() []cwlogs.Pusher {
return pushers
}

func (emf *emfExporter) start(_ context.Context, host component.Host) error {
func (emf *emfExporter) start(ctx context.Context, host component.Host) error {
// Create AWS session here
awsConfig, session, err := awsutil.GetAWSConfigSession(emf.config.logger, &awsutil.Conn{}, &emf.config.AWSSessionSettings)
if err != nil {
return err
}

// create CWLogs client with aws session config
svcStructuredLog := cwlogs.NewClient(emf.config.logger,
awsConfig,
emf.set.BuildInfo,
emf.config.LogGroupName,
emf.config.LogRetention,
emf.config.Tags,
session,
cwlogs.WithEnabledContainerInsights(emf.config.IsEnhancedContainerInsights()),
cwlogs.WithEnabledAppSignals(emf.config.IsAppSignalsEnabled()),
)

// Assign to the struct
emf.svcStructuredLog = svcStructuredLog

if emf.config.IsAppSignalsEnabled() {
userAgent := appsignals.NewUserAgent()
svcStructuredLog.Handlers().Build.PushBackNamed(userAgent.Handler())
emf.processResourceLabels = userAgent.Process
}

// Optionally configure middleware
if emf.config.MiddlewareID != nil {
awsmiddleware.TryConfigure(emf.config.logger, host, *emf.config.MiddlewareID, awsmiddleware.SDKv1(emf.svcStructuredLog.Handlers()))
awsmiddleware.TryConfigure(emf.config.logger, host, *emf.config.MiddlewareID, awsmiddleware.SDKv1(svcStructuredLog.Handlers()))
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ require (
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/aliyun/aliyun-log-go-sdk v0.1.76 // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20240419190856-2f880467f335 // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20241204155332-be45e31638de // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20240415183253-230331014d2c // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/apache/arrow/go/v15 v15.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions internal/metadataproviders/aws/ec2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib

import (
"context"
"github.com/aws/aws-sdk-go/aws/request"

override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
"github.com/aws/aws-sdk-go/aws"
Expand All @@ -14,6 +15,7 @@ import (

type Provider interface {
Get(ctx context.Context) (ec2metadata.EC2InstanceIdentityDocument, error)
GetHandlers(ctx context.Context) *request.Handlers
Hostname(ctx context.Context) (string, error)
InstanceID(ctx context.Context) (string, error)
}
Expand Down Expand Up @@ -58,3 +60,7 @@ func (c *metadataClient) Get(_ context.Context) (ec2metadata.EC2InstanceIdentity
}
return c.metadataFallbackEnable.GetInstanceIdentityDocument()
}

func (c *metadataClient) GetHandlers(_ context.Context) *request.Handlers {
return &c.metadata.Handlers
}
3 changes: 3 additions & 0 deletions processor/resourcedetectionprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resourcedetectionprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor"

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
Expand Down Expand Up @@ -41,6 +42,8 @@ type Config struct {
// If a supplied attribute is not a valid attribute of a supplied detector it will be ignored.
// Deprecated: Please use detector's resource_attributes config instead
Attributes []string `mapstructure:"attributes"`

MiddlewareID *component.ID `mapstructure:"middleware,omitempty"`
}

// DetectorConfig contains user-specified configurations unique to all individual detectors
Expand Down
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (f *factory) getResourceDetectionProcessor(
}

return &resourceDetectionProcessor{
config: oCfg,
provider: provider,
override: oCfg.Override,
httpClientSettings: oCfg.ClientConfig,
Expand Down
3 changes: 3 additions & 0 deletions processor/resourcedetectionprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.5
require (
cloud.google.com/go/compute/metadata v0.3.0
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.23.0
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20241204155332-be45e31638de
github.com/aws/aws-sdk-go v1.53.11
github.com/google/go-cmp v0.6.0
github.com/hashicorp/consul/api v1.29.1
Expand Down Expand Up @@ -38,6 +39,8 @@ require (
github.com/Showmax/go-fqdn v1.0.0 // indirect
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-20240415183253-230331014d2c // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go-v2 v1.22.2 // indirect
github.com/aws/smithy-go v1.16.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand Down
24 changes: 24 additions & 0 deletions processor/resourcedetectionprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions processor/resourcedetectionprocessor/internal/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws/request"
"net/http"
"regexp"

Expand Down Expand Up @@ -122,6 +123,10 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return res, conventions.SchemaURL, nil
}

func (d *Detector) ExposeHandlers(ctx context.Context) (handlers *request.Handlers) {
return d.metadataProvider.GetHandlers(ctx)
}

func getClientConfig(ctx context.Context, logger *zap.Logger) *http.Client {
client, err := internal.ClientFromContext(ctx)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions processor/resourcedetectionprocessor/internal/resourcedetection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ package internal // import "github.com/open-telemetry/opentelemetry-collector-co
import (
"context"
"fmt"
"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
"github.com/aws/aws-sdk-go/aws/request"
"go.opentelemetry.io/collector/component"
"net/http"
"sync"
"time"
Expand All @@ -23,6 +26,10 @@ type Detector interface {
Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error)
}

type ExposeHandlerDetector interface {
Detector // Embed the existing Detector interface
ExposeHandlers(ctx context.Context) *request.Handlers
}
type DetectorConfig any

type ResourceDetectorConfig interface {
Expand Down Expand Up @@ -116,6 +123,14 @@ func (p *ResourceProvider) Get(ctx context.Context, client *http.Client) (resour
return p.detectedResource.resource, p.detectedResource.schemaURL, p.detectedResource.err
}

func (p *ResourceProvider) ConfigureHandlers(ctx context.Context, host component.Host, middlewareId component.ID) {
for _, detector := range p.detectors {
if handlerDetector, ok := detector.(ExposeHandlerDetector); ok {
awsmiddleware.TryConfigure(p.logger, host, middlewareId, awsmiddleware.SDKv1(handlerDetector.ExposeHandlers(ctx)))
}
}
}

func (p *ResourceProvider) detectResource(ctx context.Context) {
p.detectedResource = &resourceResult{}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

type resourceDetectionProcessor struct {
config *Config
provider *internal.ResourceProvider
resource pcommon.Resource
schemaURL string
Expand All @@ -30,6 +31,7 @@ func (rdp *resourceDetectionProcessor) Start(ctx context.Context, host component
client, _ := rdp.httpClientSettings.ToClient(ctx, host, rdp.telemetrySettings)
ctx = internal.ContextWithClient(ctx, client)
var err error
rdp.provider.ConfigureHandlers(ctx, host, *rdp.config.MiddlewareID) //configuring middleware in all clients of detectors
rdp.resource, rdp.schemaURL, err = rdp.provider.Get(ctx, client)
return err
}
Expand Down
3 changes: 3 additions & 0 deletions receiver/awscontainerinsightreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package awscontainerinsightreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver"

import (
"go.opentelemetry.io/collector/component"
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
Expand Down Expand Up @@ -72,4 +73,6 @@ type Config struct {

// RunOnSystemd is an optional attribute to run the receiver in an EC2 environment
RunOnSystemd bool `mapstructure:"run_on_systemd,omitempty"`

MiddlewareID *component.ID `mapstructure:"middleware,omitempty"`
}
3 changes: 3 additions & 0 deletions receiver/awscontainerinsightreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.5

require (
github.com/Microsoft/hcsshim v0.12.0-rc.3
github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware v0.0.0-20241204155332-be45e31638de
github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws v0.0.0-00010101000000-000000000000
github.com/aws/aws-sdk-go v1.53.11
github.com/go-kit/log v0.2.1
Expand Down Expand Up @@ -53,6 +54,8 @@ require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go-v2 v1.22.2 // indirect
github.com/aws/smithy-go v1.16.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down
Loading

0 comments on commit d63bbf4

Please sign in to comment.