-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
opentelemetry.go
187 lines (159 loc) · 5.11 KB
/
opentelemetry.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//go:generate ../../../tools/readme_config_includer/generator
package opentelemetry
import (
"context"
ntls "crypto/tls"
_ "embed"
"time"
"github.com/influxdata/influxdb-observability/common"
"github.com/influxdata/influxdb-observability/influx2otel"
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
_ "google.golang.org/grpc/encoding/gzip" // Blank import to allow gzip encoding
"google.golang.org/grpc/metadata"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
)
var userAgent = internal.ProductToken()
//go:embed sample.conf
var sampleConfig string
type OpenTelemetry struct {
ServiceAddress string `toml:"service_address"`
tls.ClientConfig
Timeout config.Duration `toml:"timeout"`
Compression string `toml:"compression"`
Headers map[string]string `toml:"headers"`
Attributes map[string]string `toml:"attributes"`
Coralogix *CoralogixConfig `toml:"coralogix"`
Log telegraf.Logger `toml:"-"`
metricsConverter *influx2otel.LineProtocolToOtelMetrics
grpcClientConn *grpc.ClientConn
metricsServiceClient pmetricotlp.GRPCClient
callOptions []grpc.CallOption
}
type CoralogixConfig struct {
AppName string `toml:"application"`
SubSystem string `toml:"subsystem"`
PrivateKey string `toml:"private_key"`
}
func (*OpenTelemetry) SampleConfig() string {
return sampleConfig
}
func (o *OpenTelemetry) Connect() error {
logger := &otelLogger{o.Log}
if o.ServiceAddress == "" {
o.ServiceAddress = defaultServiceAddress
}
if o.Timeout <= 0 {
o.Timeout = defaultTimeout
}
if o.Compression == "" {
o.Compression = defaultCompression
}
if o.Coralogix != nil {
if o.Headers == nil {
o.Headers = make(map[string]string)
}
o.Headers["ApplicationName"] = o.Coralogix.AppName
o.Headers["ApiName"] = o.Coralogix.SubSystem
o.Headers["Authorization"] = "Bearer " + o.Coralogix.PrivateKey
}
metricsConverter, err := influx2otel.NewLineProtocolToOtelMetrics(logger)
if err != nil {
return err
}
var grpcTLSDialOption grpc.DialOption
if tlsConfig, err := o.ClientConfig.TLSConfig(); err != nil {
return err
} else if tlsConfig != nil {
grpcTLSDialOption = grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
} else if o.Coralogix != nil {
// For coralogix, we enforce GRPC connection with TLS
grpcTLSDialOption = grpc.WithTransportCredentials(credentials.NewTLS(&ntls.Config{}))
} else {
grpcTLSDialOption = grpc.WithTransportCredentials(insecure.NewCredentials())
}
grpcClientConn, err := grpc.Dial(o.ServiceAddress, grpcTLSDialOption, grpc.WithUserAgent(userAgent))
if err != nil {
return err
}
metricsServiceClient := pmetricotlp.NewGRPCClient(grpcClientConn)
o.metricsConverter = metricsConverter
o.grpcClientConn = grpcClientConn
o.metricsServiceClient = metricsServiceClient
if o.Compression != "" && o.Compression != "none" {
o.callOptions = append(o.callOptions, grpc.UseCompressor(o.Compression))
}
return nil
}
func (o *OpenTelemetry) Close() error {
if o.grpcClientConn != nil {
err := o.grpcClientConn.Close()
o.grpcClientConn = nil
return err
}
return nil
}
func (o *OpenTelemetry) Write(metrics []telegraf.Metric) error {
batch := o.metricsConverter.NewBatch()
for _, metric := range metrics {
var vType common.InfluxMetricValueType
switch metric.Type() {
case telegraf.Gauge:
vType = common.InfluxMetricValueTypeGauge
case telegraf.Untyped:
vType = common.InfluxMetricValueTypeUntyped
case telegraf.Counter:
vType = common.InfluxMetricValueTypeSum
case telegraf.Histogram:
vType = common.InfluxMetricValueTypeHistogram
case telegraf.Summary:
vType = common.InfluxMetricValueTypeSummary
default:
o.Log.Warnf("unrecognized metric type %Q", metric.Type())
continue
}
err := batch.AddPoint(metric.Name(), metric.Tags(), metric.Fields(), metric.Time(), vType)
if err != nil {
o.Log.Warnf("failed to add point: %s", err)
continue
}
}
md := pmetricotlp.NewExportRequestFromMetrics(batch.GetMetrics())
if md.Metrics().ResourceMetrics().Len() == 0 {
return nil
}
if len(o.Attributes) > 0 {
for i := 0; i < md.Metrics().ResourceMetrics().Len(); i++ {
for k, v := range o.Attributes {
md.Metrics().ResourceMetrics().At(i).Resource().Attributes().PutStr(k, v)
}
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(o.Timeout))
if len(o.Headers) > 0 {
ctx = metadata.NewOutgoingContext(ctx, metadata.New(o.Headers))
}
defer cancel()
_, err := o.metricsServiceClient.Export(ctx, md, o.callOptions...)
return err
}
const (
defaultServiceAddress = "localhost:4317"
defaultTimeout = config.Duration(5 * time.Second)
defaultCompression = "gzip"
)
func init() {
outputs.Add("opentelemetry", func() telegraf.Output {
return &OpenTelemetry{
ServiceAddress: defaultServiceAddress,
Timeout: defaultTimeout,
Compression: defaultCompression,
}
})
}