diff --git a/callbacks/apmplus/apmplus.go b/callbacks/apmplus/apmplus.go index 50cc84a9c..e02360c5d 100644 --- a/callbacks/apmplus/apmplus.go +++ b/callbacks/apmplus/apmplus.go @@ -59,16 +59,36 @@ type Config struct { // Default: "" // Example: "v1.2.3" Release string + + // ResourceAttributes is custom resource attributes (Optional) + ResourceAttributes map[string]string } func NewApmplusHandler(cfg *Config) (handler callbacks.Handler, shutdown func(ctx context.Context) error, err error) { - p, err := opentelemetry.NewOpenTelemetryProvider( + resourceAttributes := []attribute.KeyValue{ + attribute.String("apmplus.business_type", "gen_ai"), + } + if len(cfg.ResourceAttributes) > 0 { + for k, v := range cfg.ResourceAttributes { + resourceAttributes = append(resourceAttributes, attribute.String(k, v)) + } + } + var resourceOpts []opentelemetry.Option + for _, attr := range resourceAttributes { + resourceOpts = append(resourceOpts, + opentelemetry.WithResourceAttribute(attr), + ) + } + + providerOpts := []opentelemetry.Option{ opentelemetry.WithServiceName(cfg.ServiceName), opentelemetry.WithExportEndpoint(cfg.Host), opentelemetry.WithInsecure(), opentelemetry.WithHeaders(map[string]string{"x-byteapm-appkey": cfg.AppKey}), - opentelemetry.WithResourceAttribute(attribute.String("apmplus.business_type", "gen_ai")), - ) + } + providerOpts = append(providerOpts, resourceOpts...) + + p, err := opentelemetry.NewOpenTelemetryProvider(providerOpts...) if p == nil || err != nil { return nil, nil, errors.New("init opentelemetry provider failed") } diff --git a/callbacks/apmplus/apmplus_test.go b/callbacks/apmplus/apmplus_test.go index de025542f..c3a8d5564 100644 --- a/callbacks/apmplus/apmplus_test.go +++ b/callbacks/apmplus/apmplus_test.go @@ -31,11 +31,16 @@ import ( ) func TestApmplusCallback(t *testing.T) { + // optional: add custom resource attributes + resourceAttributes := make(map[string]string) + resourceAttributes["attr-key"] = "attr-value" + cbh, _, _ := NewApmplusHandler(&Config{ - Host: "apmplus host", - AppKey: "app key", - ServiceName: "MyService", - Release: "release", + Host: "apmplus host", + AppKey: "app key", + ServiceName: "MyService", + Release: "release", + ResourceAttributes: resourceAttributes, }) callbacks.AppendGlobalHandlers(cbh) ctx := context.Background() diff --git a/callbacks/apmplus/go.mod b/callbacks/apmplus/go.mod index 1a86720c7..2b4c274ec 100644 --- a/callbacks/apmplus/go.mod +++ b/callbacks/apmplus/go.mod @@ -11,6 +11,7 @@ require ( go.opentelemetry.io/contrib/instrumentation/runtime v0.59.0 go.opentelemetry.io/otel v1.34.0 go.opentelemetry.io/otel/metric v1.34.0 + go.opentelemetry.io/otel/sdk v1.34.0 go.opentelemetry.io/otel/trace v1.34.0 ) @@ -50,7 +51,6 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.34.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.34.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.34.0 // indirect - go.opentelemetry.io/otel/sdk v1.34.0 // indirect go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect go.opentelemetry.io/proto/otlp v1.5.0 // indirect golang.org/x/arch v0.12.0 // indirect