-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpcplatform_opentelemetry.go
More file actions
114 lines (96 loc) · 3.21 KB
/
Copy pathrpcplatform_opentelemetry.go
File metadata and controls
114 lines (96 loc) · 3.21 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright 2025 RPCPlatform Authors
*
* 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 rpcplatform
import (
"context"
"errors"
"log"
"net"
"strconv"
"time"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.37.0"
"google.golang.org/grpc/stats"
)
func (p *RPCPlatform) openTelemetry(instanceID string, localAddr net.Addr, publicAddr string) (stats.Handler, error) {
resOptions := []resource.Option{
resource.WithHost(),
resource.WithOS(),
resource.WithContainer(),
resource.WithProcess(),
resource.WithTelemetrySDK(),
resource.WithSchemaURL(semconv.SchemaURL),
resource.WithAttributes(semconv.ServiceName(p.config.OpenTelemetry.ServiceName)),
resource.WithAttributes(semconv.ServiceInstanceID(instanceID)),
}
if localAddr != nil {
host, port, err := net.SplitHostPort(localAddr.String())
if err != nil {
return nil, err
}
portInt, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
resOptions = append(resOptions,
resource.WithAttributes(semconv.NetworkTransportKey.String(localAddr.Network())),
resource.WithAttributes(semconv.NetworkLocalAddress(host)),
resource.WithAttributes(semconv.NetworkLocalPort(portInt)),
)
if publicAddr != localAddr.String() {
host, port, err = net.SplitHostPort(publicAddr)
if err != nil {
return nil, err
}
portInt, err = strconv.Atoi(port)
if err != nil {
return nil, err
}
}
resOptions = append(resOptions,
resource.WithAttributes(semconv.ServerAddress(host)),
resource.WithAttributes(semconv.ServerPort(portInt)),
)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
res, err := resource.New(ctx, resOptions...)
cancel()
if errors.Is(err, resource.ErrPartialResource) || errors.Is(err, resource.ErrSchemaURLConflict) {
log.Println(err)
} else if err != nil {
return nil, err
}
tpOptions := []trace.TracerProviderOption{
trace.WithSampler(trace.ParentBased(trace.TraceIDRatioBased(p.config.OpenTelemetry.SampleRate))),
trace.WithResource(res),
}
for _, exporter := range p.config.OpenTelemetry.Exporters {
tpOptions = append(tpOptions, trace.WithBatcher(exporter))
}
tracerProvider := otelgrpc.WithTracerProvider(
trace.NewTracerProvider(tpOptions...),
)
propagators := otelgrpc.WithPropagators(
propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}),
)
if localAddr != nil {
return otelgrpc.NewServerHandler(tracerProvider, propagators), nil
}
return otelgrpc.NewClientHandler(tracerProvider, propagators), nil
}