Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Middleware to containerinsightrecieve/resourcedetection #265

Merged
merged 7 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
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
7 changes: 7 additions & 0 deletions internal/metadataproviders/aws/ec2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
Expand All @@ -14,6 +16,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 +61,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.

6 changes: 6 additions & 0 deletions processor/resourcedetectionprocessor/internal/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"regexp"

"github.com/aws/aws-sdk-go/aws/request"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -122,6 +124,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)
}

Choose a reason for hiding this comment

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

nit: For consistency and to make it easier to follow, keep the names of the functions used to access the request handlers the same. Would suggest keeping it the same as the internal/aws/cwlogs client.

func (client *Client) Handlers() *request.Handlers {
return &client.svc.(*cloudwatchlogs.CloudWatchLogs).Handlers
}


func getClientConfig(ctx context.Context, logger *zap.Logger) *http.Client {
client, err := internal.ClientFromContext(ctx)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions processor/resourcedetectionprocessor/internal/resourcedetection.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"sync"
"time"

"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
"github.com/aws/aws-sdk-go/aws/request"
"go.opentelemetry.io/collector/component"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
"go.uber.org/zap"
Expand All @@ -23,6 +27,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 +124,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)))

Choose a reason for hiding this comment

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

nit: Could grab the Configurer once and pass that into this function instead of the host and middlewareID so it isn't trying to get it each time.

}
}
}

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
4 changes: 4 additions & 0 deletions receiver/awscontainerinsightreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package awscontainerinsightreceiver // import "github.com/open-telemetry/opentel
import (
"time"

"go.opentelemetry.io/collector/component"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
)

Expand Down Expand Up @@ -72,4 +74,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
24 changes: 24 additions & 0 deletions receiver/awscontainerinsightreceiver/go.sum

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

14 changes: 12 additions & 2 deletions receiver/awscontainerinsightreceiver/internal/host/ebsvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"context"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
Expand All @@ -15,6 +16,8 @@ import (
"sync"
"time"

"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -56,7 +59,7 @@ type ebsVolume struct {
type ebsVolumeOption func(*ebsVolume)

func newEBSVolume(ctx context.Context, session *session.Session, instanceID string, region string,
refreshInterval time.Duration, logger *zap.Logger, options ...ebsVolumeOption) ebsVolumeProvider {
refreshInterval time.Duration, logger *zap.Logger, configurer *awsmiddleware.Configurer, options ...ebsVolumeOption) ebsVolumeProvider {
e := &ebsVolume{
dev2Vol: make(map[string]string),
instanceID: instanceID,
Expand All @@ -69,7 +72,14 @@ func newEBSVolume(ctx context.Context, session *session.Session, instanceID stri
osLstat: os.Lstat,
evalSymLinks: filepath.EvalSymlinks,
}

if configurer != nil {
err := configurer.Configure(awsmiddleware.SDKv1(&e.client.(*ec2.EC2).Handlers))
if err != nil {
log.Println("There was a problem configuring middleware on ec2 client")
} else {
log.Println("Successfully configured sdk with middleware handlers")
}
}
for _, opt := range options {
opt(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestEBSVolume(t *testing.T) {
}
}

e := newEBSVolume(ctx, sess, "instanceId", "us-west-2", time.Millisecond, zap.NewNop(),
e := newEBSVolume(ctx, sess, "instanceId", "us-west-2", time.Millisecond, zap.NewNop(), nil,
clientOption, maxJitterOption, hostMountsOption, LstatOption, evalSymLinksOption)

<-mockVolumeClient.success
Expand All @@ -180,7 +180,7 @@ func TestEBSVolume(t *testing.T) {
hostMountsOption = func(e *ebsVolume) {
e.hostMounts = "/an-invalid-path"
}
e = newEBSVolume(ctx, sess, "instanceId", "us-west-2", time.Millisecond, zap.NewNop(),
e = newEBSVolume(ctx, sess, "instanceId", "us-west-2", time.Millisecond, zap.NewNop(), nil,
clientOption, maxJitterOption, hostMountsOption, LstatOption, evalSymLinksOption)
ebsIDs = e.extractEbsIDsUsedByKubernetes()
assert.Equal(t, 0, len(ebsIDs))
Expand Down
Loading
Loading