@@ -29,12 +29,78 @@ import (
2929 semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
3030)
3131
32- var tp * trace.TracerProvider
32+ // Init() enables OpenTelemetry tracing for an AIS node.
33+ // Semantics and constraints:
34+ // - Build-time feature:
35+ // This function is only compiled when AIS is built with the `oteltracing`
36+ // build tag (see tracing_off.go for the no-op stub used otherwise).
37+ // Callers invoke Init() unconditionally.
38+ // - Config handling:
39+ // The config.Tracing field is a *cmn.TracingConf pointer. A nil conf
40+ // or a conf with Enabled == false is treated as "tracing disabled":
41+ // - we log an informational message and return without error.
42+ // This makes it safe to omit the `tracing` section from ais.conf entirely.
43+ // - Exporter endpoint:
44+ // When tracing is enabled, ExporterEndpoint MUST be non-empty. If it is
45+ // empty we log an error, trip a debug.Assert in debug builds, and return
46+ // without installing a tracer provider. The node continues to run with
47+ // tracing effectively disabled.
48+ // - Error handling (is intentionally tolerant):
49+ // Any failure to construct the exporter (newExporter) or resource will
50+ // trip debug assertions but will not crash non-debug builds. In those
51+ // cases, tp remains nil and no global tracer provider is installed.
52+
53+ var (
54+ tp * trace.TracerProvider
55+ )
56+
57+ func Init (conf * cmn.TracingConf , snode * meta.Snode , exp any /* trace.SpanExporter */ , version string ) {
58+ if conf == nil || ! conf .Enabled {
59+ nlog .Infof ("distributed tracing not enabled (%+v)" , conf )
60+ return
61+ }
62+ if conf .ExporterEndpoint == "" {
63+ nlog .Errorln ("exporter endpoint can't be empty" )
64+ debug .Assert (false )
65+ return
66+ }
67+ var (
68+ exporter trace.SpanExporter
69+ err error
70+ ok bool
71+ )
72+ if exp == nil {
73+ // default trace.SpanExporter
74+ exporter , err = newExporter (conf )
75+ if err != nil {
76+ nlog .Errorln ("failed to start exporter:" , err )
77+ debug .AssertNoErr (err )
78+ return
79+ }
80+ } else {
81+ // unit test
82+ exporter , ok = exp .(trace.SpanExporter )
83+ debug .Assertf (ok , "invalid exporter type %T" , exp )
84+ }
85+ tp = trace .NewTracerProvider (
86+ trace .WithSampler (trace .ParentBased (trace .TraceIDRatioBased (conf .SamplerProbability ))),
87+ trace .WithBatcher (exporter ),
88+ trace .WithResource (newResource (conf , snode , version )),
89+ )
90+
91+ otel .SetTextMapPropagator (
92+ propagation .NewCompositeTextMapPropagator (
93+ propagation.TraceContext {},
94+ propagation.Baggage {},
95+ ),
96+ )
97+ otel .SetTracerProvider (tp )
98+ }
3399
34100func loadAccessToken (tokenFilePath string ) string {
35- cos . AssertMsg (tokenFilePath != "" , "token filepath cannot be empty" )
101+ debug . Assert (tokenFilePath != "" , "token filepath cannot be empty" )
36102 data , err := os .ReadFile (tokenFilePath )
37- cos .AssertNoErr (err )
103+ debug .AssertNoErr (err )
38104 return strings .TrimSpace (string (data ))
39105}
40106
@@ -86,42 +152,6 @@ func IsEnabled() bool {
86152 return tp != nil
87153}
88154
89- func Init (conf * cmn.TracingConf , snode * meta.Snode , exp any /* trace.SpanExporter */ , version string ) {
90- if conf == nil || ! conf .Enabled {
91- nlog .Infof ("distributed tracing not enabled (%+v)" , conf )
92- return
93- }
94- cos .AssertMsg (conf .ExporterEndpoint != "" , "exporter endpoint can't be empty" )
95-
96- var (
97- exporter trace.SpanExporter
98- err error
99- ok bool
100- )
101- if exp == nil {
102- // default trace.SpanExporter
103- exporter , err = newExporter (conf )
104- cos .AssertNoErr (err )
105- } else {
106- // unit test
107- exporter , ok = exp .(trace.SpanExporter )
108- debug .Assertf (ok , "invalid exporter type %T" , exp )
109- }
110- tp = trace .NewTracerProvider (
111- trace .WithSampler (trace .ParentBased (trace .TraceIDRatioBased (conf .SamplerProbability ))),
112- trace .WithBatcher (exporter ),
113- trace .WithResource (newResource (conf , snode , version )),
114- )
115-
116- otel .SetTextMapPropagator (
117- propagation .NewCompositeTextMapPropagator (
118- propagation.TraceContext {},
119- propagation.Baggage {},
120- ),
121- )
122- otel .SetTracerProvider (tp )
123- }
124-
125155// used in tests only
126156func ForceFlush () { tp .ForceFlush (context .Background ()) }
127157
0 commit comments