Debian Codegraph @

pkg/server/options/tracing.go

raw
1 /*
2 Copyright 2021 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package options
18
19 import (
20 "context"
21 "fmt"
22 "io/ioutil"
23 "net"
24
25 "github.com/spf13/pflag"
26 "go.opentelemetry.io/otel"
27 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
28 "go.opentelemetry.io/otel/metric/noop"
29 "go.opentelemetry.io/otel/sdk/resource"
30 semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
31 "google.golang.org/grpc"
32
33 "k8s.io/apimachinery/pkg/runtime"
34 "k8s.io/apimachinery/pkg/runtime/serializer"
35 "k8s.io/apiserver/pkg/apis/apiserver"
36 "k8s.io/apiserver/pkg/apis/apiserver/install"
37 "k8s.io/apiserver/pkg/features"
38 "k8s.io/apiserver/pkg/server"
39 "k8s.io/apiserver/pkg/server/egressselector"
40 "k8s.io/apiserver/pkg/util/feature"
41 tracing "k8s.io/component-base/tracing"
42 tracingapi "k8s.io/component-base/tracing/api/v1"
43 "k8s.io/utils/path"
44 )
45
46 const apiserverService = "apiserver"
47
48 var (
49 cfgScheme = runtime.NewScheme()
50 codecs = serializer.NewCodecFactory(cfgScheme, serializer.EnableStrict)
51 )
52
53 func init() {
54 // Prevent memory leak from OTel metrics, which we don't use:
55 // https://github.com/open-telemetry/opentelemetry-go-contrib/issues/5190
56 otel.SetMeterProvider(noop.NewMeterProvider())
57 }
58
59 func init() {
60 install.Install(cfgScheme)
61 }
62
63 // TracingOptions contain configuration options for tracing
64 // exporters
65 type TracingOptions struct {
66 // ConfigFile is the file path with api-server tracing configuration.
67 ConfigFile string
68 }
69
70 // NewTracingOptions creates a new instance of TracingOptions
71 func NewTracingOptions() *TracingOptions {
72 return &TracingOptions{}
73 }
74
75 // AddFlags adds flags related to tracing to the specified FlagSet
76 func (o *TracingOptions) AddFlags(fs *pflag.FlagSet) {
77 if o == nil {
78 return
79 }
80
81 fs.StringVar(&o.ConfigFile, "tracing-config-file", o.ConfigFile,
82 "File with apiserver tracing configuration.")
83 }
84
85 // ApplyTo fills up Tracing config with options.
86 func (o *TracingOptions) ApplyTo(es *egressselector.EgressSelector, c *server.Config) error {
87 if o == nil || o.ConfigFile == "" {
88 return nil
89 }
90 if !feature.DefaultFeatureGate.Enabled(features.APIServerTracing) {
91 return fmt.Errorf("APIServerTracing feature is not enabled, but tracing config file was provided")
92 }
93
94 traceConfig, err := ReadTracingConfiguration(o.ConfigFile)
95 if err != nil {
96 return fmt.Errorf("failed to read tracing config: %v", err)
97 }
98
99 errs := tracingapi.ValidateTracingConfiguration(traceConfig, feature.DefaultFeatureGate, nil)
100 if len(errs) > 0 {
101 return fmt.Errorf("failed to validate tracing configuration: %v", errs.ToAggregate())
102 }
103
104 opts := []otlptracegrpc.Option{}
105 if es != nil {
106 // Only use the egressselector dialer if egressselector is enabled.
107 // Endpoint is on the "ControlPlane" network
108 egressDialer, err := es.Lookup(egressselector.ControlPlane.AsNetworkContext())
109 if err != nil {
110 return err
111 }
112 if egressDialer != nil {
113 otelDialer := func(ctx context.Context, addr string) (net.Conn, error) {
114 return egressDialer(ctx, "tcp", addr)
115 }
116 opts = append(opts, otlptracegrpc.WithDialOption(grpc.WithContextDialer(otelDialer)))
117 }
118 }
119
120 resourceOpts := []resource.Option{
121 resource.WithAttributes(
122 semconv.ServiceNameKey.String(apiserverService),
123 semconv.ServiceInstanceIDKey.String(c.APIServerID),
124 ),
125 }
126 tp, err := tracing.NewProvider(context.Background(), traceConfig, opts, resourceOpts)
127 if err != nil {
128 return err
129 }
130 c.TracerProvider = tp
131 if c.LoopbackClientConfig != nil {
132 c.LoopbackClientConfig.Wrap(tracing.WrapperFor(c.TracerProvider))
133 }
134 return nil
135 }
136
137 // Validate verifies flags passed to TracingOptions.
138 func (o *TracingOptions) Validate() (errs []error) {
139 if o == nil || o.ConfigFile == "" {
140 return
141 }
142
143 if exists, err := path.Exists(path.CheckFollowSymlink, o.ConfigFile); !exists {
144 errs = append(errs, fmt.Errorf("tracing-config-file %s does not exist", o.ConfigFile))
145 } else if err != nil {
146 errs = append(errs, fmt.Errorf("error checking if tracing-config-file %s exists: %v", o.ConfigFile, err))
147 }
148 return
149 }
150
151 // ReadTracingConfiguration reads the tracing configuration from a file
152 func ReadTracingConfiguration(configFilePath string) (*tracingapi.TracingConfiguration, error) {
153 if configFilePath == "" {
154 return nil, fmt.Errorf("tracing config file was empty")
155 }
156 data, err := ioutil.ReadFile(configFilePath)
157 if err != nil {
158 return nil, fmt.Errorf("unable to read tracing configuration from %q: %v", configFilePath, err)
159 }
160 internalConfig := &apiserver.TracingConfiguration{}
161 // this handles json/yaml/whatever, and decodes all registered version to the internal version
162 if err := runtime.DecodeInto(codecs.UniversalDecoder(), data, internalConfig); err != nil {
163 return nil, fmt.Errorf("unable to decode tracing configuration data: %v", err)
164 }
165 return &internalConfig.TracingConfiguration, nil
166 }