|
| 1 | +/* |
| 2 | + Copyright The containerd 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 plugin |
| 18 | + |
| 19 | +import ( |
| 20 | + "io" |
| 21 | + |
| 22 | + "github.com/containerd/containerd/log" |
| 23 | + "github.com/containerd/containerd/plugin" |
| 24 | + "github.com/pkg/errors" |
| 25 | + "go.opentelemetry.io/otel" |
| 26 | + "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" |
| 27 | + "go.opentelemetry.io/otel/propagation" |
| 28 | + "go.opentelemetry.io/otel/sdk/resource" |
| 29 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 30 | + semconv "go.opentelemetry.io/otel/semconv/v1.4.0" |
| 31 | + "google.golang.org/grpc" |
| 32 | +) |
| 33 | + |
| 34 | +const exporterPlugin = "otlp" |
| 35 | + |
| 36 | +func init() { |
| 37 | + plugin.Register(&plugin.Registration{ |
| 38 | + ID: exporterPlugin, |
| 39 | + Type: plugin.TracingProcessorPlugin, |
| 40 | + Config: &OTLPConfig{}, |
| 41 | + InitFn: func(ic *plugin.InitContext) (interface{}, error) { |
| 42 | + cfg := ic.Config.(*OTLPConfig) |
| 43 | + if cfg.Endpoint == "" { |
| 44 | + return nil, errors.Wrap(plugin.ErrSkipPlugin, "otlp endpoint not set") |
| 45 | + } |
| 46 | + dialOpts := []grpc.DialOption{grpc.WithBlock()} |
| 47 | + if cfg.Insecure { |
| 48 | + dialOpts = append(dialOpts, grpc.WithInsecure()) |
| 49 | + } |
| 50 | + |
| 51 | + exp, err := otlptracegrpc.New(ic.Context, |
| 52 | + otlptracegrpc.WithEndpoint(cfg.Endpoint), |
| 53 | + otlptracegrpc.WithDialOption(dialOpts...), |
| 54 | + ) |
| 55 | + if err != nil { |
| 56 | + return nil, errors.Wrap(err, "failed to create otlp exporter") |
| 57 | + } |
| 58 | + return sdktrace.NewBatchSpanProcessor(exp), nil |
| 59 | + }, |
| 60 | + }) |
| 61 | + plugin.Register(&plugin.Registration{ |
| 62 | + ID: "tracing", |
| 63 | + Type: plugin.InternalPlugin, |
| 64 | + Requires: []plugin.Type{plugin.TracingProcessorPlugin}, |
| 65 | + Config: &TraceConfig{ServiceName: "containerd"}, |
| 66 | + InitFn: func(ic *plugin.InitContext) (interface{}, error) { |
| 67 | + return newTracer(ic) |
| 68 | + }, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +// OTLPConfig holds the configurations for the built-in otlp span processor |
| 73 | +type OTLPConfig struct { |
| 74 | + Endpoint string `toml:"endpoint"` |
| 75 | + Insecure bool `toml:"insecure"` |
| 76 | +} |
| 77 | + |
| 78 | +// TraceConfig is the common configuration for open telemetry. |
| 79 | +type TraceConfig struct { |
| 80 | + ServiceName string `toml:"service_name"` |
| 81 | + TraceSamplingRatio float64 `toml:"sampling_ratio"` |
| 82 | +} |
| 83 | + |
| 84 | +type closer struct { |
| 85 | + close func() error |
| 86 | +} |
| 87 | + |
| 88 | +func (c *closer) Close() error { |
| 89 | + return c.close() |
| 90 | +} |
| 91 | + |
| 92 | +// InitOpenTelemetry reads config and initializes otel middleware, sets the exporter |
| 93 | +// propagator and global tracer provider |
| 94 | +func newTracer(ic *plugin.InitContext) (io.Closer, error) { |
| 95 | + ctx := ic.Context |
| 96 | + config := ic.Config.(*TraceConfig) |
| 97 | + |
| 98 | + res, err := resource.New(ctx, |
| 99 | + resource.WithAttributes( |
| 100 | + // Service name used to displace traces in backends |
| 101 | + semconv.ServiceNameKey.String(config.ServiceName), |
| 102 | + ), |
| 103 | + ) |
| 104 | + if err != nil { |
| 105 | + return nil, errors.Wrap(err, "failed to create resource") |
| 106 | + } |
| 107 | + |
| 108 | + opts := []sdktrace.TracerProviderOption{ |
| 109 | + sdktrace.WithSampler(sdktrace.TraceIDRatioBased(config.TraceSamplingRatio)), |
| 110 | + sdktrace.WithResource(res), |
| 111 | + } |
| 112 | + |
| 113 | + ls, err := ic.GetByType(plugin.TracingProcessorPlugin) |
| 114 | + if err != nil { |
| 115 | + return nil, errors.Wrap(err, "failed to get tracing processors") |
| 116 | + } |
| 117 | + |
| 118 | + procs := make([]sdktrace.SpanProcessor, 0, len(ls)) |
| 119 | + for id, pctx := range ls { |
| 120 | + p, err := pctx.Instance() |
| 121 | + if err != nil { |
| 122 | + log.G(ctx).WithError(err).Errorf("Failed to init tracing processor %q", id) |
| 123 | + continue |
| 124 | + } |
| 125 | + proc := p.(sdktrace.SpanProcessor) |
| 126 | + opts = append(opts, sdktrace.WithSpanProcessor(proc)) |
| 127 | + procs = append(procs, proc) |
| 128 | + } |
| 129 | + |
| 130 | + provider := sdktrace.NewTracerProvider(opts...) |
| 131 | + |
| 132 | + otel.SetTracerProvider(provider) |
| 133 | + otel.SetTextMapPropagator(propagation.TraceContext{}) |
| 134 | + |
| 135 | + return &closer{close: func() error { |
| 136 | + for _, p := range procs { |
| 137 | + if err := p.Shutdown(ctx); err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + } |
| 141 | + return nil |
| 142 | + }}, nil |
| 143 | +} |
0 commit comments