Skip to content

Commit 2935236

Browse files
committed
intercept calls to mod proxy
1 parent b2d7990 commit 2935236

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

cmd/go-cache-plugin/commands.go

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

cmd/go-cache-plugin/setup.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"expvar"
1313
"fmt"
14+
"github.com/grafana/go-cache-plugin/lib/otel"
1415
"log"
1516
"net/http"
1617
"os"
@@ -306,7 +307,7 @@ func initServerCert(env *command.Env, hosts []string) (tls.Certificate, error) {
306307

307308
// makeHandler returns an HTTP handler that dispatches requests to debug
308309
// handlers or to the specified proxies, if they are defined.
309-
func makeHandler(modProxy, revProxy http.Handler) http.HandlerFunc {
310+
func makeHandler(modProxy, revProxy http.Handler, tracingContext *otel.TracingContext) http.HandlerFunc {
310311
mux := http.NewServeMux()
311312
tsweb.Debugger(mux)
312313
return func(w http.ResponseWriter, r *http.Request) {
@@ -327,6 +328,11 @@ func makeHandler(modProxy, revProxy http.Handler) http.HandlerFunc {
327328
return
328329
}
329330
if modProxy != nil && r.Method == http.MethodGet && strings.HasPrefix(path, "/mod/") {
331+
if tracingContext != nil {
332+
_, span := tracingContext.SpanWithContext(r.Context(), strings.TrimPrefix(path, "/mod/"))
333+
defer span.End()
334+
}
335+
330336
log.Printf("proxying %s %s", r.Method, r.URL.Path)
331337
modProxy.ServeHTTP(w, r)
332338
return

0 commit comments

Comments
 (0)