Skip to content

Commit 17595b2

Browse files
committed
feat: support custom resource attribute in apmplus
1 parent ea27324 commit 17595b2

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

callbacks/apmplus/apmplus.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6468
func 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
}

callbacks/apmplus/apmplus_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ import (
3131
)
3232

3333
func TestApmplusCallback(t *testing.T) {
34+
// optional: add custom resource attributes
35+
resourceAttributes := make(map[string]string)
36+
resourceAttributes["attr-key"] = "attr-value"
37+
3438
cbh, _, _ := NewApmplusHandler(&Config{
35-
Host: "apmplus host",
36-
AppKey: "app key",
37-
ServiceName: "MyService",
38-
Release: "release",
39+
Host: "apmplus host",
40+
AppKey: "app key",
41+
ServiceName: "MyService",
42+
Release: "release",
43+
ResourceAttributes: resourceAttributes,
3944
})
4045
callbacks.AppendGlobalHandlers(cbh)
4146
ctx := context.Background()

0 commit comments

Comments
 (0)