Skip to content

Commit 1355caf

Browse files
authored
fix: otel flags (#44)
1 parent 9104986 commit 1355caf

File tree

5 files changed

+139
-51
lines changed

5 files changed

+139
-51
lines changed

go.mod

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ require (
77
github.com/go-logr/logr v1.4.2
88
github.com/onsi/gomega v1.34.2
99
github.com/spf13/pflag v1.0.5
10-
github.com/spf13/viper v1.19.0
1110
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0
1211
go.opentelemetry.io/contrib/propagators/b3 v1.30.0
1312
go.opentelemetry.io/otel v1.30.0
1413
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0
1514
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0
1615
go.opentelemetry.io/otel/sdk v1.30.0
16+
google.golang.org/grpc v1.67.0
1717
k8s.io/api v0.31.1
1818
k8s.io/apimachinery v0.31.1
1919
k8s.io/client-go v0.31.1
@@ -55,40 +55,30 @@ require (
5555
github.com/gorilla/websocket v1.5.3 // indirect
5656
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
5757
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
58-
github.com/hashicorp/hcl v1.0.0 // indirect
5958
github.com/imdario/mergo v0.3.16 // indirect
6059
github.com/inconshreveable/mousetrap v1.1.0 // indirect
6160
github.com/josharian/intern v1.0.0 // indirect
6261
github.com/json-iterator/go v1.1.12 // indirect
6362
github.com/klauspost/compress v1.17.10 // indirect
6463
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
65-
github.com/magiconair/properties v1.8.7 // indirect
6664
github.com/mailru/easyjson v0.7.7 // indirect
6765
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
68-
github.com/mitchellh/mapstructure v1.5.0 // indirect
6966
github.com/moby/spdystream v0.5.0 // indirect
7067
github.com/moby/term v0.5.0 // indirect
7168
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7269
github.com/modern-go/reflect2 v1.0.2 // indirect
7370
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
7471
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
7572
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
76-
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
7773
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
7874
github.com/pkg/errors v0.9.1 // indirect
7975
github.com/prometheus/client_golang v1.20.4 // indirect
8076
github.com/prometheus/client_model v0.6.1 // indirect
8177
github.com/prometheus/common v0.59.1 // indirect
8278
github.com/prometheus/procfs v0.15.1 // indirect
8379
github.com/russross/blackfriday/v2 v2.1.0 // indirect
84-
github.com/sagikazarmark/locafero v0.6.0 // indirect
85-
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
86-
github.com/sourcegraph/conc v0.3.0 // indirect
87-
github.com/spf13/afero v1.11.0 // indirect
88-
github.com/spf13/cast v1.7.0 // indirect
8980
github.com/spf13/cobra v1.8.1 // indirect
9081
github.com/stretchr/objx v0.5.2 // indirect
91-
github.com/subosito/gotenv v1.6.0 // indirect
9282
github.com/x448/float16 v0.8.4 // indirect
9383
github.com/xlab/treeprint v1.2.0 // indirect
9484
go.opentelemetry.io/otel/metric v1.30.0 // indirect
@@ -108,11 +98,9 @@ require (
10898
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
10999
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
110100
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
111-
google.golang.org/grpc v1.67.0 // indirect
112101
google.golang.org/protobuf v1.34.2 // indirect
113102
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
114103
gopkg.in/inf.v0 v0.9.1 // indirect
115-
gopkg.in/ini.v1 v1.67.0 // indirect
116104
gopkg.in/yaml.v2 v2.4.0 // indirect
117105
gopkg.in/yaml.v3 v3.0.1 // indirect
118106
k8s.io/apiextensions-apiserver v0.31.1 // indirect

go.sum

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ github.com/fluxcd/pkg/apis/meta v1.6.1 h1:maLhcRJ3P/70ArLCY/LF/YovkxXbX+6sTWZwZQ
3939
github.com/fluxcd/pkg/apis/meta v1.6.1/go.mod h1:YndB/gxgGZmKfqpAfFxyCDNFJFP0ikpeJzs66jwq280=
4040
github.com/fluxcd/pkg/runtime v0.49.1 h1:Xyruu1VvkaKZaAhm/32tHJnHab9aU3HzZCf+w6Xoq2A=
4141
github.com/fluxcd/pkg/runtime v0.49.1/go.mod h1:ieDaIEcxzVj77Nw64q4Vd3ZGYdLqpnXOr+GX+XwqTS4=
42-
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
43-
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
4442
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
4543
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
4644
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
@@ -90,8 +88,6 @@ github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJr
9088
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
9189
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys=
9290
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
93-
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
94-
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
9591
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
9692
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
9793
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
@@ -112,14 +108,10 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
112108
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
113109
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
114110
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
115-
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
116-
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
117111
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
118112
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
119113
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
120114
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
121-
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
122-
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
123115
github.com/moby/spdystream v0.5.0 h1:7r0J1Si3QO/kjRitvSLVVFUjxMEb/YLj6S9FF62JBCU=
124116
github.com/moby/spdystream v0.5.0/go.mod h1:xBAYlnt/ay+11ShkdFKNAG7LsyK/tmNBVvVOwrfMgdI=
125117
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
@@ -139,8 +131,6 @@ github.com/onsi/ginkgo/v2 v2.20.1 h1:YlVIbqct+ZmnEph770q9Q7NVAz4wwIiVNahee6JyUzo
139131
github.com/onsi/ginkgo/v2 v2.20.1/go.mod h1:lG9ey2Z29hR41WMVthyJBGUBcBhGOtoPF2VFMvBXFCI=
140132
github.com/onsi/gomega v1.34.2 h1:pNCwDkzrsv7MS9kpaQvVb1aVLahQXyJ/Tv5oAZMI3i8=
141133
github.com/onsi/gomega v1.34.2/go.mod h1:v1xfxRgk0KIsG+QOdm7p8UosrOzPYRo60fd3B/1Dukc=
142-
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
143-
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
144134
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
145135
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
146136
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -160,24 +150,12 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
160150
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
161151
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
162152
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
163-
github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk=
164-
github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0=
165-
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
166-
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
167153
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
168154
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
169-
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
170-
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
171-
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
172-
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
173-
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
174-
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
175155
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
176156
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
177157
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
178158
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
179-
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
180-
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
181159
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
182160
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
183161
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
@@ -186,8 +164,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
186164
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
187165
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
188166
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
189-
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
190-
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
191167
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
192168
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
193169
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
@@ -281,8 +257,6 @@ gopkg.in/evanphx/json-patch.v4 v4.12.0 h1:n6jtcsulIzXPJaxegRbvFNNrZDjbij7ny3gmSP
281257
gopkg.in/evanphx/json-patch.v4 v4.12.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
282258
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
283259
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
284-
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
285-
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
286260
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
287261
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
288262
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

internal/otelsetup/options.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package otelsetup
2+
3+
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"os"
7+
8+
"github.com/spf13/pflag"
9+
)
10+
11+
type Options struct {
12+
ServiceName string
13+
Endpoint string
14+
Insecure bool
15+
TLSVerify bool
16+
TLSClientKeyPath string
17+
TLSClientCertPath string
18+
TLSRootCAPath string
19+
}
20+
21+
// getTls returns a configuration that enables the use of mutual TLS.
22+
func (o *Options) getTLSConfig() (*tls.Config, error) {
23+
var tlsConfig = tls.Config{}
24+
25+
if o.TLSClientCertPath != "" && o.TLSClientKeyPath != "" {
26+
clientAuth, err := tls.LoadX509KeyPair(o.TLSClientCertPath, o.TLSClientKeyPath)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
tlsConfig.Certificates = []tls.Certificate{clientAuth}
32+
}
33+
34+
if o.TLSRootCAPath != "" {
35+
caCert, err := os.ReadFile(o.TLSRootCAPath)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
caCertPool := x509.NewCertPool()
41+
caCertPool.AppendCertsFromPEM(caCert)
42+
tlsConfig.RootCAs = caCertPool
43+
}
44+
45+
return &tlsConfig, nil
46+
}
47+
48+
// BindFlags will parse the given pflag.FlagSet
49+
func (o *Options) BindFlags(fs *pflag.FlagSet) {
50+
fs.StringVar(&o.ServiceName, "otel-service-name", "k8skeycloak-controller", "Opentelemetry service name")
51+
fs.StringVar(&o.Endpoint, "otel-endpoint", "", "Opentelemetry gRPC endpoint (without protocol)")
52+
fs.BoolVar(&o.Insecure, "otel-insecure", false, "Opentelemetry gRPC disable tls")
53+
fs.StringVar(&o.TLSClientKeyPath, "otel-tls-client-key-path", "", "Opentelemetry gRPC mTLS client key path")
54+
fs.StringVar(&o.TLSClientCertPath, "otel-tls-client-cert-path", "", "Opentelemetry gRPC mTLS client cert path")
55+
fs.StringVar(&o.TLSRootCAPath, "otel-tls-root-ca-path", "", "Opentelemetry gRPC mTLS root CA path")
56+
}

internal/otelsetup/tracing.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package otelsetup
2+
3+
import (
4+
"context"
5+
6+
"go.opentelemetry.io/otel"
7+
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
8+
"go.opentelemetry.io/otel/propagation"
9+
"go.opentelemetry.io/otel/sdk/resource"
10+
"go.opentelemetry.io/otel/sdk/trace"
11+
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
12+
"google.golang.org/grpc/credentials"
13+
)
14+
15+
func Tracing(ctx context.Context, opts Options) (*trace.TracerProvider, error) {
16+
var grpcOptions []otlptracegrpc.Option
17+
18+
if opts.Endpoint != "" {
19+
grpcOptions = append(grpcOptions, otlptracegrpc.WithEndpoint(opts.Endpoint))
20+
}
21+
22+
if opts.Insecure {
23+
grpcOptions = append(grpcOptions, otlptracegrpc.WithInsecure())
24+
} else {
25+
tlsOpts, err := opts.getTLSConfig()
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
grpcOptions = append(grpcOptions, otlptracegrpc.WithTLSCredentials(credentials.NewTLS(tlsOpts)))
31+
32+
}
33+
34+
exporter, err := otlptracegrpc.New(
35+
ctx,
36+
grpcOptions...,
37+
)
38+
39+
if err != nil {
40+
return nil, err
41+
}
42+
43+
// labels/tags/resources that are common to all traces.
44+
resource := resource.NewWithAttributes(
45+
semconv.SchemaURL,
46+
semconv.ServiceNameKey.String(opts.ServiceName),
47+
)
48+
49+
provider := trace.NewTracerProvider(
50+
trace.WithBatcher(exporter),
51+
trace.WithResource(resource),
52+
trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(1))),
53+
)
54+
55+
otel.SetTextMapPropagator(
56+
propagation.NewCompositeTextMapPropagator(
57+
propagation.TraceContext{}, // W3C Trace Context format; https://www.w3.org/TR/trace-context/
58+
),
59+
)
60+
61+
otel.SetTracerProvider(provider)
62+
63+
return provider, nil
64+
}

main.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ import (
2525

2626
infrav1beta1 "github.com/DoodleScheduling/oauth2-redirect-controller/api/v1beta1"
2727
"github.com/DoodleScheduling/oauth2-redirect-controller/internal/controllers"
28+
"github.com/DoodleScheduling/oauth2-redirect-controller/internal/otelsetup"
2829
"github.com/DoodleScheduling/oauth2-redirect-controller/internal/proxy"
2930
"github.com/fluxcd/pkg/runtime/client"
3031
helper "github.com/fluxcd/pkg/runtime/controller"
3132
"github.com/fluxcd/pkg/runtime/leaderelection"
3233
"github.com/fluxcd/pkg/runtime/logger"
3334
flag "github.com/spf13/pflag"
34-
"github.com/spf13/viper"
3535
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
3636
"go.opentelemetry.io/contrib/propagators/b3"
3737
"go.opentelemetry.io/otel"
@@ -80,6 +80,7 @@ var (
8080
leaderElectionOptions leaderelection.Options
8181
rateLimiterOptions helper.RateLimiterOptions
8282
watchOptions helper.WatchOptions
83+
otelOptions otelsetup.Options
8384
)
8485

8586
func main() {
@@ -101,6 +102,7 @@ func main() {
101102
rateLimiterOptions.BindFlags(flag.CommandLine)
102103
kubeConfigOpts.BindFlags(flag.CommandLine)
103104
watchOptions.BindFlags(flag.CommandLine)
105+
otelOptions.BindFlags(flag.CommandLine)
104106

105107
flag.Parse()
106108
logger.SetLogger(logger.NewLogger(logOptions))
@@ -199,21 +201,25 @@ func main() {
199201

200202
wrappedHandler := otelhttp.NewHandler(proxy, "oauth2-proxy")
201203

202-
readTimeout, err := time.ParseDuration(viper.GetString("proxy-read-timeout"))
203-
if err != nil {
204-
setupLog.Error(err, "failed to parse proxy read timeout")
205-
}
204+
if otelOptions.Endpoint != "" {
205+
tp, err := otelsetup.Tracing(context.Background(), otelOptions)
206+
defer func() {
207+
if err := tp.Shutdown(context.Background()); err != nil {
208+
setupLog.Error(err, "failed to shutdown trace provider")
209+
}
210+
}()
206211

207-
writeTimeout, err := time.ParseDuration(viper.GetString("proxy-write-timeout"))
208-
if err != nil {
209-
setupLog.Error(err, "failed to parse proxy write timeout")
212+
if err != nil {
213+
setupLog.Error(err, "failed to setup trace provider")
214+
}
210215
}
211216

212217
s := &http.Server{
213-
Addr: httpAddr,
214-
Handler: wrappedHandler,
215-
ReadTimeout: readTimeout,
216-
WriteTimeout: writeTimeout,
218+
Addr: httpAddr,
219+
Handler: wrappedHandler,
220+
ReadTimeout: proxyReadTimeout,
221+
WriteTimeout: proxyWriteTimeout,
222+
MaxHeaderBytes: 1 << 20,
217223
}
218224

219225
go func() {

0 commit comments

Comments
 (0)