forked from transparency-dev/tessera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathotel.go
More file actions
94 lines (85 loc) · 3.08 KB
/
Copy pathotel.go
File metadata and controls
94 lines (85 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2025 The Tessera authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"go.opentelemetry.io/contrib/detectors/gcp"
"go.opentelemetry.io/otel"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
mexporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
"k8s.io/klog/v2"
)
// initOTel initialises the open telemetry support for metrics and tracing.
//
// Tracing is enabled with statistical sampling, with the probability passed in.
// Returns a shutdown function which should be called just before exiting the process.
func initOTel(ctx context.Context, traceFraction float64) func(context.Context) {
var shutdownFuncs []func(context.Context) error
// shutdown combines shutdown functions from multiple OpenTelemetry
// components into a single function.
shutdown := func(ctx context.Context) {
var err error
for _, fn := range shutdownFuncs {
err = errors.Join(err, fn(ctx))
}
shutdownFuncs = nil
if err != nil {
klog.Errorf("OTel shutdown: %v", err)
}
}
resources, err := resource.New(ctx,
resource.WithTelemetrySDK(),
resource.WithFromEnv(), // unpacks OTEL_RESOURCE_ATTRIBUTES
// Add your own custom attributes to identify your application
resource.WithAttributes(
semconv.ServiceNameKey.String("conformance"),
semconv.ServiceNamespaceKey.String("tessera"),
),
resource.WithDetectors(gcp.NewDetector()),
)
if err != nil {
klog.Exitf("Failed to detect resources: %v", err)
}
me, err := mexporter.New()
if err != nil {
klog.Exitf("Failed to create metric exporter: %v", err)
return nil
}
// initialize a MeterProvider that periodically exports to the GCP exporter.
mp := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(me)),
sdkmetric.WithResource(resources),
)
shutdownFuncs = append(shutdownFuncs, mp.Shutdown)
otel.SetMeterProvider(mp)
te, err := texporter.New()
if err != nil {
klog.Exitf("Failed to create trace exporter: %v", err)
return nil
}
// initialize a TracerProvier that periodically exports to the GCP exporter.
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.TraceIDRatioBased(traceFraction)),
sdktrace.WithBatcher(te),
sdktrace.WithResource(resources),
)
shutdownFuncs = append(shutdownFuncs, mp.Shutdown)
otel.SetTracerProvider(tp)
return shutdown
}