-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
137 lines (122 loc) · 4.25 KB
/
config.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
package otel
import (
"fmt"
"os"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.uber.org/zap"
)
type Exporter string
const (
zipkinExp Exporter = "zipkin"
jaegerExp Exporter = "jaeger"
stdout Exporter = "stdout"
stderr Exporter = "stderr"
otlp Exporter = "otlp"
)
type Client string
const (
grpcClient Client = "grpc"
httpClient Client = "http"
)
// Resource describes an entity about which identifying information and metadata is exposed.
// Resource is an immutable object, equivalent to a map from key to unique value
type Resource struct {
ServiceNameKey string `mapstructure:"service_name"`
ServiceNamespaceKey string `mapstructure:"service_namespace"`
ServiceInstanceIDKey string `mapstructure:"service_instance_id"`
ServiceVersionKey string `mapstructure:"service_version"`
}
type Config struct {
// Resource describes an entity about which identifying information and metadata is exposed.
Resource *Resource `mapstructure:"resource"`
// Insecure endpoint (http)
Insecure bool `mapstructure:"insecure"`
// Compress - use gzip compression
Compress bool `mapstructure:"compress"`
// Exporter type, can be zipkin,stdout or otlp
Exporter Exporter `mapstructure:"exporter"`
// CustomURL to use to send spans, has effect only for the HTTP exporter
CustomURL string `mapstructure:"custom_url"`
// Client
Client Client `mapstructure:"client"`
// Endpoint to connect
Endpoint string `mapstructure:"endpoint"`
// ServiceName describes the service in the attributes
ServiceName string `mapstructure:"service_name"`
// ServiceVersion in semver format
ServiceVersion string `mapstructure:"service_version"`
// Headers for the otlp protocol
Headers map[string]string `mapstructure:"headers"`
}
func (c *Config) InitDefault(log *zap.Logger) {
if c.Exporter == "" {
c.Exporter = otlp
}
if c.ServiceName != "" {
log.Warn("service_name is deprecated, use resource.service_name instead")
}
if c.ServiceVersion != "" {
log.Warn("service_version is deprecated, use resource.service_version instead")
}
if c.Exporter == jaegerExp {
log.Warn("jaeger exporter is deprecated, use OTLP instead: https://github.com/roadrunner-server/roadrunner/issues/1699")
}
switch c.Client {
case grpcClient, httpClient:
// ok value, do nothing
case "":
c.Client = httpClient
setClientFromEnv(&c.Client, log)
default:
log.Warn("unknown exporter client", zap.String("client", string(c.Client)))
c.Client = httpClient
}
if c.Resource == nil {
c.Resource = &Resource{}
}
envAttrs := resource.Environment()
fillValue(&c.Resource.ServiceNameKey, c.ServiceName, envAttrs, semconv.ServiceNameKey, "RoadRunner")
fillValue(&c.Resource.ServiceVersionKey, c.ServiceVersion, envAttrs, semconv.ServiceVersionKey, "1.0.0")
fillValue(&c.Resource.ServiceInstanceIDKey, "", envAttrs, semconv.ServiceInstanceIDKey, uuid.NewString())
fillValue(&c.Resource.ServiceNamespaceKey, "", envAttrs, semconv.ServiceNamespaceKey, fmt.Sprintf("%s-%s", c.Resource.ServiceNameKey, uuid.NewString()))
}
func setClientFromEnv(client *Client, log *zap.Logger) {
// https://opentelemetry.io/docs/specs/otel/protocol/exporter/#specify-protocol
exporterEnv := "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"
exporterVal := os.Getenv(exporterEnv)
if exporterVal == "" {
exporterEnv = "OTEL_EXPORTER_OTLP_PROTOCOL"
exporterVal = os.Getenv(exporterEnv)
}
switch exporterVal {
case "":
// env var not set, do not change the client
case "grpc":
*client = grpcClient
case "http/protobuf":
*client = httpClient
case "http/json":
log.Warn("unsupported exporter protocol", zap.String("env.name", exporterEnv), zap.String("env.value", exporterVal))
default:
log.Warn("unknown exporter protocol", zap.String("env.name", exporterEnv), zap.String("env.value", exporterVal))
}
}
func fillValue(target *string, fromConf string, fromResource *resource.Resource, fromResourceKey attribute.Key, fromDefault string) {
if *target != "" {
return
}
if fromConf != "" {
*target = fromConf
return
}
if resValue, haveValue := fromResource.Set().Value(fromResourceKey); haveValue {
if resStr := resValue.AsString(); resStr != "" {
*target = resStr
return
}
}
*target = fromDefault
}