Skip to content

Commit 5bfd266

Browse files
committed
Fix span never close
Signed-off-by: Bryan Frimin <bryan@getprobo.com>
1 parent b2b2656 commit 5bfd266

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

httpserver/httpserver.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
stdlog "log"
2323
"net"
2424
"net/http"
25+
"sync"
2526
"time"
2627

2728
"github.com/prometheus/client_golang/prometheus"
@@ -91,20 +92,30 @@ func NewServer(addr string, h http.Handler, options ...Option) *http.Server {
9192
),
9293
)
9394

95+
connSpans := &sync.Map{}
96+
9497
return &http.Server{
9598
Addr: addr,
9699
Handler: handler,
97100
ErrorLog: stdlog.New(logger, "", 0),
98101
ReadHeaderTimeout: 5 * time.Second,
99102
IdleTimeout: 15 * time.Second,
100103
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
101-
ctx, _ = tracer.Start(ctx, "http.connection",
104+
ctx, span := tracer.Start(ctx, "http.connection",
102105
trace.WithSpanKind(trace.SpanKindServer),
103106
trace.WithAttributes(
104107
attribute.String("net.peer.addr", c.RemoteAddr().String()),
105108
),
106109
)
110+
connSpans.Store(c, span)
107111
return ctx
108112
},
113+
ConnState: func(c net.Conn, state http.ConnState) {
114+
if state == http.StateClosed || state == http.StateHijacked {
115+
if span, ok := connSpans.LoadAndDelete(c); ok {
116+
span.(trace.Span).End()
117+
}
118+
}
119+
},
109120
}
110121
}

0 commit comments

Comments
 (0)