forked from wealdtech/chaind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.go
153 lines (135 loc) · 4.59 KB
/
tracing.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright © 2022 Weald Technology Trading.
// 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"
"crypto/tls"
"crypto/x509"
"fmt"
// #nosec G108
_ "net/http/pprof"
"os"
"time"
"github.com/pkg/errors"
"github.com/spf13/viper"
majordomo "github.com/wealdtech/go-majordomo"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"google.golang.org/grpc/credentials"
)
// initTracing initialises the tracing system.
func initTracing(ctx context.Context, majordomo majordomo.Service) error {
if viper.GetString("tracing.address") == "" {
log.Debug().Msg("No tracing endpoint supplied; tracing not enabled")
return nil
}
log.Info().Str("endpoint", viper.GetString("tracing.address")).Msg("Starting tracing")
driverOpts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(viper.GetString("tracing.address")),
}
if viper.GetString("tracing.client-cert") != "" {
log.Trace().Msg("Using TLS tracing connection")
creds, err := credentialsFromCerts(ctx, majordomo, "tracing")
if err != nil {
return errors.Wrap(err, "invalid TLS credentials")
}
driverOpts = append(driverOpts,
otlptracegrpc.WithTLSCredentials(creds),
)
} else {
log.Trace().Msg("Using insecure tracing connection")
driverOpts = append(driverOpts,
otlptracegrpc.WithInsecure(),
)
}
driver := otlptracegrpc.NewClient(driverOpts...)
exp, err := otlptrace.New(ctx, driver)
if err != nil {
return errors.Wrap(err, "failed to set up OTLP exporter")
}
hostname, err := os.Hostname()
if err != nil {
log.Debug().Err(err).Msg("Failed to obtain hostname")
hostname = "unknown"
}
tp := trace.NewTracerProvider(
trace.WithBatcher(exp),
trace.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String("chaind"),
semconv.ServiceInstanceIDKey.String(hostname),
attribute.String("release", ReleaseVersion),
)),
)
// Register our TracerProvider as the global so any imported
// instrumentation in the future will default to using it.
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},
propagation.Baggage{},
))
// Shut down cleanly on exit.
go func(ctx context.Context) {
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
//nolint:contextcheck
if err := tp.Shutdown(ctx); err != nil {
log.Error().Err(err).Msg("Failed to shut down tracing")
} else {
log.Trace().Msg("Shut down tracing")
}
}(ctx)
return nil
}
func credentialsFromCerts(ctx context.Context, majordomo majordomo.Service, base string) (credentials.TransportCredentials, error) {
_, span := otel.Tracer("wealdtech.chaind").Start(ctx, "credentialsFromCerts")
defer span.End()
clientCert, err := majordomo.Fetch(ctx, viper.GetString(fmt.Sprintf("%s.client-cert", base)))
if err != nil {
return nil, errors.Wrap(err, "failed to obtain server certificate")
}
clientKey, err := majordomo.Fetch(ctx, viper.GetString(fmt.Sprintf("%s.client-key", base)))
if err != nil {
return nil, errors.Wrap(err, "failed to obtain server key")
}
var caCert []byte
if viper.GetString(fmt.Sprintf("%s.ca-cert", base)) != "" {
caCert, err = majordomo.Fetch(ctx, viper.GetString(fmt.Sprintf("%s.ca-cert", base)))
if err != nil {
return nil, errors.Wrap(err, "failed to obtain client CA certificate")
}
}
clientPair, err := tls.X509KeyPair(clientCert, clientKey)
if err != nil {
return nil, errors.Wrap(err, "failed to load client keypair")
}
tlsCfg := &tls.Config{
Certificates: []tls.Certificate{clientPair},
MinVersion: tls.VersionTLS13,
}
if caCert != nil {
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(caCert) {
return nil, errors.New("failed to add CA certificate")
}
tlsCfg.RootCAs = cp
}
return credentials.NewTLS(tlsCfg), nil
}