@@ -2,10 +2,13 @@ package sharedotlptraces
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "strings"
5
7
6
8
sharedotlp "github.com/formancehq/go-libs/sharedotlp/pkg"
7
9
"go.opentelemetry.io/contrib/propagators/b3"
8
10
"go.opentelemetry.io/otel"
11
+ "go.opentelemetry.io/otel/attribute"
9
12
"go.opentelemetry.io/otel/exporters/jaeger"
10
13
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
11
14
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
@@ -37,29 +40,38 @@ type OTLPConfig struct {
37
40
}
38
41
39
42
type ModuleConfig struct {
40
- Exporter string
41
- Batch bool
42
- JaegerConfig * JaegerConfig
43
- OTLPConfig * OTLPConfig
44
- Resource * resource.Resource
43
+ Exporter string
44
+ Batch bool
45
+ JaegerConfig * JaegerConfig
46
+ OTLPConfig * OTLPConfig
47
+ ResourceAttributes []string
48
+ ServiceName string
45
49
}
46
50
47
51
func ProvideTracerProviderOption (v any , annotations ... fx.Annotation ) fx.Option {
48
52
annotations = append (annotations , fx .ResultTags (TracerProviderOptionKey ))
49
53
return fx .Provide (fx .Annotate (v , annotations ... ))
50
54
}
51
55
56
+ func loadResource (cfg ModuleConfig ) (* resource.Resource , error ) {
57
+ defaultResource := resource .Default ()
58
+ attributes := make ([]attribute.KeyValue , 0 )
59
+ attributes = append (attributes , attribute .String ("service.name" , cfg .ServiceName ))
60
+ for _ , ra := range cfg .ResourceAttributes {
61
+ parts := strings .SplitN (ra , "=" , 2 )
62
+ if len (parts ) < 2 {
63
+ return nil , fmt .Errorf ("malformed otlp attribute: %s" , ra )
64
+ }
65
+ attributes = append (attributes , attribute .String (parts [0 ], parts [1 ]))
66
+ }
67
+ return resource .Merge (defaultResource , resource .NewSchemaless (attributes ... ))
68
+ }
69
+
52
70
func TracesModule (cfg ModuleConfig ) fx.Option {
53
71
options := make ([]fx.Option , 0 )
54
72
options = append (options ,
55
- fx .Provide (func () (* resource.Resource , error ) {
56
- defaultResource := resource .Default ()
57
- if cfg .Resource == nil {
58
- return defaultResource , nil
59
- }
60
- return resource .Merge (defaultResource , cfg .Resource )
61
- }),
62
- fx .Supply (resource .Default ()),
73
+ fx .Supply (cfg ),
74
+ fx .Provide (loadResource ),
63
75
fx .Provide (func (tp * tracesdk.TracerProvider ) trace.TracerProvider { return tp }),
64
76
fx .Provide (fx .Annotate (func (options ... tracesdk.TracerProviderOption ) * tracesdk.TracerProvider {
65
77
return tracesdk .NewTracerProvider (options ... )
0 commit comments