|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package traces |
| 15 | + |
| 16 | +import ( |
| 17 | + "go.opentelemetry.io/otel/api/global" |
| 18 | + "go.opentelemetry.io/otel/exporters/otlp" |
| 19 | + "go.opentelemetry.io/otel/sdk/resource" |
| 20 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 21 | + "go.opentelemetry.io/otel/semconv" |
| 22 | + |
| 23 | + "k8s.io/klog/v2" |
| 24 | +) |
| 25 | + |
| 26 | +// InitTraces initializes tracing in the component. |
| 27 | +// Components must use the OTLP exporter, but can pass additional exporter |
| 28 | +// options if needed |
| 29 | +func InitTraces(service string, opts ...otlp.ExporterOption) { |
| 30 | + opts = append(opts, otlp.WithInsecure()) |
| 31 | + exporter, err := otlp.NewExporter(opts...) |
| 32 | + if err != nil { |
| 33 | + klog.Fatalf("Failed to create OTLP exporter: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // Use ParentBased(NeverSample()) to preserve the sampling decision of the |
| 37 | + // parent, but not start additional spans. |
| 38 | + tp := sdktrace.NewTracerProvider( |
| 39 | + sdktrace.WithConfig(sdktrace.Config{ |
| 40 | + DefaultSampler: sdktrace.ParentBased(sdktrace.NeverSample())}, |
| 41 | + ), |
| 42 | + sdktrace.WithBatcher(exporter), |
| 43 | + sdktrace.WithResource(resource.New(semconv.ServiceNameKey.String(service)))) |
| 44 | + global.SetTracerProvider(tp) |
| 45 | +} |
0 commit comments