github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/trace/inject/http-server/http_server.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package traceinject 16 17 import ( 18 "context" 19 "fmt" 20 "net/http" 21 _ "unsafe" //nolint 22 23 "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" 24 25 injectcontext "github.com/erda-project/erda-infra/pkg/trace/inject/context" 26 "github.com/erda-project/erda-infra/pkg/trace/inject/hook" 27 ) 28 29 type serverHandler struct { 30 srv *http.Server 31 } 32 33 //go:linkname serveHTTP net/http.serverHandler.ServeHTTP 34 //go:noinline 35 func serveHTTP(s *serverHandler, rw http.ResponseWriter, req *http.Request) 36 37 //go:noinline 38 func originalServeHTTP(s *serverHandler, rw http.ResponseWriter, req *http.Request) { 39 // a dummy function to make room for a shadow copy of the original function. 40 // it doesn't matter what we do here, just to create an addressable function with adequate size. 41 originalServeHTTP(s, rw, req) 42 originalServeHTTP(s, rw, req) 43 originalServeHTTP(s, rw, req) 44 for { 45 fmt.Printf("hello") 46 } 47 } 48 49 var tracedServerHandler = otelhttp.NewHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { 50 injectcontext.SetContext(r.Context()) 51 defer injectcontext.ClearContext() 52 s := getServerHandler(r.Context()) 53 originalServeHTTP(s, rw, r) 54 }), "", otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string { 55 u := *r.URL 56 u.RawQuery = "" 57 u.ForceQuery = false 58 return r.Method + " " + u.String() 59 })) 60 61 type _serverHandlerKey int8 62 63 const serverHandlerKey _serverHandlerKey = 0 64 65 func withServerHandler(ctx context.Context, s *serverHandler) context.Context { 66 return context.WithValue(ctx, serverHandlerKey, s) 67 } 68 69 func getServerHandler(ctx context.Context) *serverHandler { 70 return ctx.Value(serverHandlerKey).(*serverHandler) 71 } 72 73 //go:noinline 74 func wrappedHTTPHandler(s *serverHandler, rw http.ResponseWriter, req *http.Request) { 75 req = req.WithContext(withServerHandler(req.Context(), s)) 76 tracedServerHandler.ServeHTTP(rw, req) 77 } 78 79 func init() { 80 hook.Hook(serveHTTP, wrappedHTTPHandler, originalServeHTTP) 81 }