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

Add WithResourceAttributes option #522

Merged
merged 11 commits into from
Nov 23, 2023
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.
edeNFed marked this conversation as resolved.
Show resolved Hide resolved

### 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
})
}
edeNFed marked this conversation as resolved.
Show resolved Hide resolved
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"
edeNFed marked this conversation as resolved.
Show resolved Hide resolved
)

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
Loading