Skip to content

Commit

Permalink
Add WithAdditionalResourceAttributes option
Browse files Browse the repository at this point in the history
  • Loading branch information
edeNFed committed Nov 21, 2023
1 parent cdfad4a commit 57140b8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- The instrumentation scope now includes the version of the auto-instrumentation project. ([#442](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/442))
- Add a new `WithSampler` method allowing end-users to provide their own implementation of OpenTelemetry sampler directly through the package API. ([#468](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/468)).
- Add uprobes to `execDC` in order to instrument SQL DML. ([#475](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/475))
- Add `WithAdditionalResourceAttributes` `InstrumentationOption` to configure `Instrumentation` to add additional resource attributes.

### Changed

Expand Down
36 changes: 29 additions & 7 deletions instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ import (
"runtime"
"strings"

"go.opentelemetry.io/otel/attribute"

"github.com/go-logr/logr"
"github.com/go-logr/stdr"
"github.com/go-logr/zapr"
"go.uber.org/zap"

"go.opentelemetry.io/contrib/exporters/autoexport"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.uber.org/zap"

"go.opentelemetry.io/auto/internal/pkg/instrumentation"
"go.opentelemetry.io/auto/internal/pkg/opentelemetry"
Expand Down Expand Up @@ -163,10 +166,11 @@ type InstrumentationOption interface {
}

type instConfig struct {
sampler trace.Sampler
traceExp trace.SpanExporter
target process.TargetArgs
serviceName string
sampler trace.Sampler
traceExp trace.SpanExporter
target process.TargetArgs
serviceName string
additionalResAttrs []attribute.KeyValue
}

func newInstConfig(ctx context.Context, opts []InstrumentationOption) (instConfig, error) {
Expand Down Expand Up @@ -239,14 +243,22 @@ func (c instConfig) res() *resource.Resource {
runVer, runtime.GOOS, runtime.GOARCH,
)

return resource.NewWithAttributes(
semconv.SchemaURL,
attrs := []attribute.KeyValue{
semconv.ServiceNameKey.String(c.serviceName),
semconv.TelemetrySDKLanguageGo,
semconv.TelemetryAutoVersionKey.String(Version()),
semconv.ProcessRuntimeName(runName),
semconv.ProcessRuntimeVersion(runVer),
semconv.ProcessRuntimeDescription(runDesc),
}

if len(c.additionalResAttrs) > 0 {
attrs = append(attrs, c.additionalResAttrs...)
}

return resource.NewWithAttributes(
semconv.SchemaURL,
attrs...,
)
}

Expand Down Expand Up @@ -396,3 +408,13 @@ func WithSampler(sampler trace.Sampler) InstrumentationOption {
return c, nil
})
}

// WithAdditionalResourceAttributes returns an [InstrumentationOption] that will
// configure an [Instrumentation] to use the provided attributes as additional
// OpenTelemetry Resource attributes.
func WithAdditionalResourceAttributes(attrs []attribute.KeyValue) InstrumentationOption {
return fnOpt(func(_ context.Context, c instConfig) (instConfig, error) {
c.additionalResAttrs = attrs
return c, nil
})
}
15 changes: 15 additions & 0 deletions instrumentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
"fmt"
"testing"

"go.opentelemetry.io/otel/attribute"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

Expand Down Expand Up @@ -127,6 +130,18 @@ func TestOptionPrecedence(t *testing.T) {
})
}

func TestWithAdditionalResourceAttributes(t *testing.T) {
testAttributes := []attribute.KeyValue{
semconv.K8SContainerName("test_container_name"),
semconv.K8SPodName("test_pod_name"),
}

// Use WithAdditionalResourceAttributes to config the additional resource attributes
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithAdditionalResourceAttributes(testAttributes)})
require.NoError(t, err)
assert.Equal(t, testAttributes, c.additionalResAttrs)
}

func mockEnv(t *testing.T, env map[string]string) {
orig := lookupEnv
t.Cleanup(func() { lookupEnv = orig })
Expand Down

0 comments on commit 57140b8

Please sign in to comment.