@@ -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