@@ -88,7 +88,6 @@ func runServe(env *command.Env) error {
8888 return env .Usagef ("you must provide a --plugin addr (or port)" )
8989 }
9090
91- log .Printf ("Otel exporter initialized with address: %s" , flags .OtelCollectorAddress )
9291 // Initialize the cache server. Unlike a direct server, only close down and
9392 // wait for cache cleanup when the whole process exits.
9493 s , s3c , err := initCacheServer (env )
@@ -138,16 +137,22 @@ func runServe(env *command.Env) error {
138137 // If an HTTP server is enabled, start it up with debug routes
139138 // and whatever other services were requested.
140139 if serveFlags .HTTP != "" {
140+ otelCleanup , tracingContext , err := initModTracing (ctx )
141+ if err != nil {
142+ return fmt .Errorf ("tracing: %w" , err )
143+ }
144+
141145 srv := & http.Server {
142146 Addr : serveFlags .HTTP ,
143- Handler : makeHandler (modProxy , revProxy ),
147+ Handler : makeHandler (modProxy , revProxy , tracingContext ),
144148 }
145149 g .Go (srv .ListenAndServe )
146150 vprintf ("HTTP server listening at %q" , serveFlags .HTTP )
147151 g .Run (func () {
148152 <- ctx .Done ()
153+ _ = otelCleanup (ctx )
149154 vprintf ("stopping HTTP service" )
150- srv .Shutdown (context .Background ())
155+ _ = srv .Shutdown (context .Background ())
151156 })
152157 }
153158
@@ -253,6 +258,27 @@ func initTracing(ctx context.Context) (func(context.Context) error, func([]byte)
253258
254259 return shutdown , spanReporter , err
255260}
261+ func initModTracing (ctx context.Context ) (func (context.Context ) error , * otel.TracingContext , error ) {
262+ if ! flags .TracingEnabled {
263+ return func (context.Context ) error { return nil }, nil , nil
264+ }
265+
266+ var shutdown func (context.Context ) error
267+ var err error
268+ if flags .OtelCollectorAddress != "" {
269+ shutdown , err = otel .SetupOtelTraceProvider (ctx , flags .OtelCollectorAddress )
270+ } else if flags .LogFile != "" {
271+ log .Printf ("Otel Collector address not specified, starting with the logging reporter, log file: %s" , flags .LogFile )
272+ shutdown , err = otel .SetupLoggingProvider (ctx , flags .LogFile )
273+ } else {
274+ log .Printf ("please specify either --otel-collector or --log-file to setup tracing or disable tracing" )
275+ return nil , nil , errors .New ("otel exporter not initialized" )
276+ }
277+
278+ tracingContext := otel .NewTracedFromString (flags .TracingContext )
279+
280+ return shutdown , tracingContext , err
281+ }
256282
257283// copy emulates the base case of io.Copy, but does not attempt to use the
258284// io.ReaderFrom or io.WriterTo implementations.
0 commit comments