Skip to content

Commit ed47ff6

Browse files
committed
feat(webserver): add EnableOpenTelemetry option to capture traces and metrics and send them to an observability platform by OpenTelemetry, since opentelemetry-go metric and trace are both stable since v1.20.0.
https://github.com/open-telemetry/opentelemetry-go/blob/v1.20.0/README.md
1 parent ee03135 commit ed47ff6

File tree

8 files changed

+180
-25
lines changed

8 files changed

+180
-25
lines changed

pkg/webserver/go.mod

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ require (
77
github.com/go-playground/validator/v10 v10.20.0
88
github.com/google/uuid v1.6.0
99
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
10+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0
1011
github.com/rs/cors v1.11.1
1112
github.com/searKing/golang/go v1.2.118
1213
github.com/searKing/golang/third_party/github.com/gin-gonic/gin v1.2.118
1314
github.com/searKing/golang/third_party/github.com/grpc-ecosystem/grpc-gateway-v2 v1.2.118
1415
github.com/searKing/golang/third_party/google.golang.org/grpc v1.2.118
16+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0
17+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0
18+
go.opentelemetry.io/otel v1.29.0
19+
go.opentelemetry.io/otel/trace v1.29.0
1520
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
1621
golang.org/x/sync v0.8.0
1722
google.golang.org/grpc v1.66.0
@@ -22,14 +27,15 @@ require (
2227
github.com/bytedance/sonic/loader v0.1.1 // indirect
2328
github.com/cloudwego/base64x v0.1.4 // indirect
2429
github.com/cloudwego/iasm v0.2.0 // indirect
30+
github.com/felixge/httpsnoop v1.0.4 // indirect
2531
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
2632
github.com/gin-contrib/sse v0.1.0 // indirect
33+
github.com/go-logr/logr v1.4.2 // indirect
34+
github.com/go-logr/stdr v1.2.2 // indirect
2735
github.com/go-playground/locales v0.14.1 // indirect
2836
github.com/go-playground/universal-translator v0.18.1 // indirect
2937
github.com/goccy/go-json v0.10.2 // indirect
3038
github.com/golang/protobuf v1.5.4 // indirect
31-
github.com/google/uuid v1.6.0 // indirect
32-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
3339
github.com/json-iterator/go v1.1.12 // indirect
3440
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
3541
github.com/leodido/go-urn v1.4.0 // indirect
@@ -39,11 +45,12 @@ require (
3945
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
4046
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
4147
github.com/ugorji/go/codec v1.2.12 // indirect
48+
go.opentelemetry.io/otel/metric v1.29.0 // indirect
4249
golang.org/x/arch v0.8.0 // indirect
43-
golang.org/x/crypto v0.24.0 // indirect
44-
golang.org/x/net v0.26.0 // indirect
45-
golang.org/x/sys v0.21.0 // indirect
46-
golang.org/x/term v0.21.0 // indirect
50+
golang.org/x/crypto v0.26.0 // indirect
51+
golang.org/x/net v0.28.0 // indirect
52+
golang.org/x/sys v0.24.0 // indirect
53+
golang.org/x/term v0.23.0 // indirect
4754
golang.org/x/text v0.17.0 // indirect
4855
golang.org/x/time v0.6.0 // indirect
4956
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect

pkg/webserver/go.sum

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
99
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1010
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1111
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
13+
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
1214
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
1315
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
1416
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
1517
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
1618
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
1719
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
20+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
21+
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
22+
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
23+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
24+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1825
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
1926
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
2027
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
@@ -79,23 +86,33 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
7986
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
8087
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
8188
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
89+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 h1:r6I7RJCN86bpD/FQwedZ0vSixDpwuWREjW9oRMsmqDc=
90+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0/go.mod h1:B9yO6b04uB80CzjedvewuqDhxJxi11s7/GtiGa8bAjI=
91+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 h1:TT4fX+nBOA/+LUkobKGW1ydGcn+G3vRw9+g5HwCphpk=
92+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0/go.mod h1:L7UH0GbB0p47T4Rri3uHjbpCFYrVrwc1I25QhNPiGK8=
93+
go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw=
94+
go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8=
95+
go.opentelemetry.io/otel/metric v1.29.0 h1:vPf/HFWTNkPu1aYeIsc98l4ktOQaL6LeSoeV2g+8YLc=
96+
go.opentelemetry.io/otel/metric v1.29.0/go.mod h1:auu/QWieFVWx+DmQOUMgj0F8LHWdgalxXqvp7BII/W8=
97+
go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4=
98+
go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ=
8299
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
83100
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
84101
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
85-
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
86-
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
102+
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
103+
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
87104
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
88105
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
89-
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
90-
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
106+
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
107+
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
91108
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
92109
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
93110
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
94111
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
95-
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
96-
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
97-
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
98-
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
112+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
113+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
114+
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
115+
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
99116
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
100117
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
101118
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=

pkg/webserver/pkg/otel/otel.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2024 The searKing Author. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package otel
6+
7+
import (
8+
"net/http"
9+
"slices"
10+
11+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
12+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
13+
http_ "github.com/searKing/golang/go/net/http"
14+
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
15+
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
16+
"go.opentelemetry.io/otel"
17+
"go.opentelemetry.io/otel/propagation"
18+
"go.opentelemetry.io/otel/trace"
19+
"google.golang.org/grpc"
20+
)
21+
22+
func DialOptions() []grpc.DialOption {
23+
return []grpc.DialOption{grpc.WithStatsHandler(otelgrpc.NewClientHandler())}
24+
}
25+
26+
func ServerOptions() []grpc.ServerOption {
27+
return []grpc.ServerOption{grpc.StatsHandler(otelgrpc.NewServerHandler())}
28+
}
29+
30+
func ServeMuxOptions() []runtime.ServeMuxOption {
31+
// Trace Header Matcher From HTTP To gRPC!
32+
return []runtime.ServeMuxOption{runtime.WithIncomingHeaderMatcher(func(key string) (string, bool) {
33+
// HTTP Server -> gRPC Server
34+
if alias := http.CanonicalHeaderKey(key); slices.ContainsFunc(
35+
otel.GetTextMapPropagator().Fields(), func(s string) bool { return http.CanonicalHeaderKey(s) == alias }) {
36+
return alias, true
37+
}
38+
return runtime.DefaultHeaderMatcher(key)
39+
})}
40+
}
41+
42+
func HttpHandlerDecorators() []http_.HandlerDecorator {
43+
return []http_.HandlerDecorator{
44+
// Root Span of HTTP!
45+
http_.HandlerDecoratorFunc(
46+
func(handler http.Handler) http.Handler {
47+
return otelhttp.NewHandler(handler, "gRPC-Gateway",
48+
otelhttp.WithMeterProvider(otel.GetMeterProvider()),
49+
otelhttp.WithTracerProvider(otel.GetTracerProvider()),
50+
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
51+
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
52+
if operation == "" {
53+
operation = "HTTP"
54+
}
55+
return operation + "." + r.Method + "." + r.URL.Path
56+
}))
57+
}),
58+
// Trace Header From HTTP To gRPC!
59+
http_.HandlerDecoratorFunc(func(handler http.Handler) http.Handler {
60+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61+
// Inject set cross-cutting concerns from the Context into the carrier.
62+
// HTTP Server -> [gRPC Client] -> gRPC Server
63+
otel.GetTextMapPropagator().Inject(r.Context(), propagation.HeaderCarrier(r.Header))
64+
handler.ServeHTTP(w, r)
65+
})
66+
}),
67+
// Inject "trace_id" and "span_id" into logging fields!
68+
http_.HandlerDecoratorFunc(func(handler http.Handler) http.Handler {
69+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
70+
fields := spanFields(trace.SpanFromContext(r.Context()))
71+
if len(fields) > 0 {
72+
r = r.WithContext(logging.InjectFields(r.Context(), fields))
73+
}
74+
handler.ServeHTTP(w, r)
75+
})
76+
}),
77+
}
78+
}
79+
80+
func spanFields(span trace.Span) logging.Fields {
81+
var fields logging.Fields
82+
if span.SpanContext().HasTraceID() {
83+
fields = append(fields, "trace_id", span.SpanContext().TraceID())
84+
}
85+
if span.SpanContext().HasSpanID() {
86+
fields = append(fields, "span_id", span.SpanContext().SpanID())
87+
}
88+
return fields
89+
}

pkg/webserver/webserver.factory.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type FactoryConfig struct {
6767
StatsHandling bool // log for the related stats handling (e.g., RPCs, connections).
6868
Validator *validator.Validate // for value validations for structs and individual fields based on tags (e.g., request).
6969
FillRequestId bool // for the field "RequestId" filling in Request and Response.
70+
EnableOpenTelemetry bool // captures traces and metrics and send them to an observability platform by OpenTelemetry.
7071

7172
// Deprecated: takes no effect, use slog instead.
7273
EnableLogrusMiddleware bool // disable logrus middleware
@@ -154,6 +155,7 @@ func (f *Factory) New() (*WebServer, error) {
154155
// http -> grpc client -> grpc server
155156
opts = append(opts, grpc_.WithGrpcServerOption(f.ServerOptions()...))
156157
opts = append(opts, grpc_.WithGrpcDialOption(f.DialOptions()...))
158+
opts = append(opts, grpc_.WithGrpcServeMuxOption(f.ServeMuxOptions()...))
157159
}
158160
{
159161
// grpc interceptors
@@ -162,7 +164,7 @@ func (f *Factory) New() (*WebServer, error) {
162164
}
163165
{
164166
// http interceptors
165-
opts = append(opts, grpc_.WithHttpHandlerDecorators(f.HttpServerInterceptors()...))
167+
opts = append(opts, grpc_.WithHttpHandlerDecorators(f.HttpHandlerDecorators()...))
166168
}
167169

168170
opts = append(opts, f.fc.GatewayOptions...)

pkg/webserver/webserver.interceptors.go renamed to pkg/webserver/webserver.interceptors.grpc.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
package webserver
66

77
import (
8-
"github.com/rs/cors"
98
"github.com/searKing/golang/pkg/webserver/pkg/requestid"
109
"google.golang.org/grpc"
1110

12-
http_ "github.com/searKing/golang/go/net/http"
1311
"github.com/searKing/golang/pkg/webserver/pkg/recovery"
1412
validator_ "github.com/searKing/golang/pkg/webserver/pkg/validator"
1513
grpc_ "github.com/searKing/golang/third_party/google.golang.org/grpc"
@@ -70,9 +68,3 @@ func (f *Factory) StreamServerInterceptors(interceptors ...grpc.StreamServerInte
7068
}
7169
return interceptors
7270
}
73-
74-
func (f *Factory) HttpServerInterceptors(decorators ...http_.HandlerDecorator) []http_.HandlerDecorator {
75-
// cors
76-
decorators = append(decorators, http_.HandlerDecoratorFunc(cors.New(f.fc.Cors).Handler))
77-
return decorators
78-
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2024 The searKing Author. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package webserver
6+
7+
import (
8+
"github.com/rs/cors"
9+
10+
http_ "github.com/searKing/golang/go/net/http"
11+
"github.com/searKing/golang/pkg/webserver/pkg/otel"
12+
)
13+
14+
func (f *Factory) HttpHandlerDecorators(decorators ...http_.HandlerDecorator) []http_.HandlerDecorator {
15+
// otel
16+
if f.fc.EnableOpenTelemetry {
17+
decorators = append(decorators, otel.HttpHandlerDecorators()...)
18+
}
19+
20+
// cors
21+
decorators = append(decorators, http_.HandlerDecoratorFunc(cors.New(f.fc.Cors).Handler))
22+
return decorators
23+
}

pkg/webserver/webserver.connection.go renamed to pkg/webserver/webserver.options.grpc.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
package webserver
66

77
import (
8-
"github.com/searKing/golang/pkg/webserver/pkg/stats"
98
"google.golang.org/grpc"
109
"google.golang.org/grpc/credentials/insecure"
10+
11+
"github.com/searKing/golang/pkg/webserver/pkg/otel"
12+
"github.com/searKing/golang/pkg/webserver/pkg/stats"
1113
)
1214

1315
func (f *Factory) ServerOptions(opts ...grpc.ServerOption) []grpc.ServerOption {
@@ -20,6 +22,9 @@ func (f *Factory) ServerOptions(opts ...grpc.ServerOption) []grpc.ServerOption {
2022
// log for the related stats handling (e.g., RPCs, connections).
2123
opts = append(opts, grpc.StatsHandler(&stats.ServerHandler{}))
2224
}
25+
if f.fc.EnableOpenTelemetry {
26+
opts = append(opts, otel.ServerOptions()...)
27+
}
2328
return opts
2429
}
2530

@@ -39,6 +44,8 @@ func (f *Factory) DialOptions(opts ...grpc.DialOption) []grpc.DialOption {
3944
// log for the related stats handling (e.g., RPCs, connections).
4045
opts = append(opts, grpc.WithStatsHandler(&stats.ClientHandler{}))
4146
}
42-
47+
if f.fc.EnableOpenTelemetry {
48+
opts = append(opts, otel.DialOptions()...)
49+
}
4350
return opts
4451
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2024 The searKing Author. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package webserver
6+
7+
import (
8+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
9+
10+
"github.com/searKing/golang/pkg/webserver/pkg/otel"
11+
)
12+
13+
func (f *Factory) ServeMuxOptions(opts ...runtime.ServeMuxOption) []runtime.ServeMuxOption {
14+
if f.fc.EnableOpenTelemetry {
15+
opts = append(opts, otel.ServeMuxOptions()...)
16+
}
17+
return opts
18+
}

0 commit comments

Comments
 (0)