@@ -37,6 +37,7 @@ import (
3737 "go.opentelemetry.io/otel/attribute"
3838 "go.opentelemetry.io/otel/codes"
3939 "go.opentelemetry.io/otel/metric"
40+ "go.opentelemetry.io/otel/sdk/resource"
4041 "go.opentelemetry.io/otel/trace"
4142)
4243
@@ -59,16 +60,44 @@ type Config struct {
5960 // Default: ""
6061 // Example: "v1.2.3"
6162 Release string
63+
64+ // ResourceAttributes is custom resource attributes (Optional)
65+ ResourceAttributes map [string ]string
6266}
6367
6468func NewApmplusHandler (cfg * Config ) (handler callbacks.Handler , shutdown func (ctx context.Context ) error , err error ) {
65- p , err := opentelemetry .NewOpenTelemetryProvider (
69+ resourceAttributes := []attribute.KeyValue {
70+ attribute .String ("apmplus.business_type" , "gen_ai" ),
71+ }
72+ if len (cfg .ResourceAttributes ) > 0 {
73+ for k , v := range cfg .ResourceAttributes {
74+ resourceAttributes = append (resourceAttributes , attribute .String (k , v ))
75+ }
76+ }
77+ res , err := resource .New (context .Background (),
78+ resource .WithFromEnv (),
79+ resource .WithAttributes (resourceAttributes ... ),
80+ )
81+ if err != nil {
82+ log .Printf ("resource merge error: %v" , err )
83+ }
84+
85+ var resourceOpts []opentelemetry.Option
86+ for _ , attr := range res .Attributes () {
87+ resourceOpts = append (resourceOpts ,
88+ opentelemetry .WithResourceAttribute (attr ),
89+ )
90+ }
91+
92+ providerOpts := []opentelemetry.Option {
6693 opentelemetry .WithServiceName (cfg .ServiceName ),
6794 opentelemetry .WithExportEndpoint (cfg .Host ),
6895 opentelemetry .WithInsecure (),
6996 opentelemetry .WithHeaders (map [string ]string {"x-byteapm-appkey" : cfg .AppKey }),
70- opentelemetry .WithResourceAttribute (attribute .String ("apmplus.business_type" , "gen_ai" )),
71- )
97+ }
98+ providerOpts = append (providerOpts , resourceOpts ... )
99+
100+ p , err := opentelemetry .NewOpenTelemetryProvider (providerOpts ... )
72101 if p == nil || err != nil {
73102 return nil , nil , errors .New ("init opentelemetry provider failed" )
74103 }
0 commit comments