-
Notifications
You must be signed in to change notification settings - Fork 27
/
metrics.go
272 lines (236 loc) 路 8.56 KB
/
metrics.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright Lightstep 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 pipelines
import (
"context"
"fmt"
"strings"
"time"
// The Lightstep SDK
sdkmetric "github.com/lightstep/otel-launcher-go/lightstep/sdk/metric"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/aggregator/aggregation"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/exporters/otlp/otlpmetric"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/exporters/otlp/otlpmetric/otlpmetricgrpc"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/sdkinstrument"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/view"
// The v1 instrumentation
lightstepCputime "github.com/lightstep/otel-launcher-go/lightstep/instrumentation/cputime"
lightstepHost "github.com/lightstep/otel-launcher-go/lightstep/instrumentation/host"
lightstepRuntime "github.com/lightstep/otel-launcher-go/lightstep/instrumentation/runtime"
// The v0 instrumentation
contribHost "go.opentelemetry.io/contrib/instrumentation/host"
contribRuntime "go.opentelemetry.io/contrib/instrumentation/runtime"
// OTel APIs
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
metricglobal "go.opentelemetry.io/otel/metric/global"
// The otel Metrics SDK
otelotlpmetricgrpc "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
otelsdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"google.golang.org/grpc/encoding/gzip"
)
func prestableHostMetrics(provider metric.MeterProvider) error {
return contribHost.Start(contribHost.WithMeterProvider(provider))
}
func prestableRuntimeMetrics(provider metric.MeterProvider) error {
return contribRuntime.Start(contribRuntime.WithMeterProvider(provider))
}
func stableHostMetrics(provider metric.MeterProvider) error {
return lightstepHost.Start(lightstepHost.WithMeterProvider(provider))
}
func stableRuntimeMetrics(provider metric.MeterProvider) error {
return lightstepRuntime.Start(lightstepRuntime.WithMeterProvider(provider))
}
func stableCputimeMetrics(provider metric.MeterProvider) error {
return lightstepCputime.Start(lightstepCputime.WithMeterProvider(provider))
}
type initFunc func(metric.MeterProvider) error
func libraries(inits ...initFunc) []initFunc {
return inits
}
const prestableVersion = "prestable"
const defaultVersion = "stable"
var builtinMetricsVersions = map[string]map[string][]initFunc{
"all": {
defaultVersion: libraries(stableHostMetrics, stableRuntimeMetrics, stableCputimeMetrics),
prestableVersion: libraries(prestableHostMetrics, prestableRuntimeMetrics),
},
"cputime": {
defaultVersion: libraries(stableCputimeMetrics),
prestableVersion: libraries(),
},
"host": {
defaultVersion: libraries(stableHostMetrics),
prestableVersion: libraries(prestableHostMetrics),
},
"runtime": {
defaultVersion: libraries(stableRuntimeMetrics),
prestableVersion: libraries(prestableRuntimeMetrics),
},
}
func NewMetricsPipeline(c PipelineConfig) (func() error, error) {
var err error
period := 30 * time.Second
if c.ReportingPeriod != "" {
period, err = time.ParseDuration(c.ReportingPeriod)
if err != nil {
return nil, fmt.Errorf("invalid metric reporting period: %v", err)
}
if period <= 0 {
return nil, fmt.Errorf("invalid metric reporting period: %v", c.ReportingPeriod)
}
}
var provider metric.MeterProvider
var shutdown func() error
lsPref, otelPref, err := tempoOptions(c)
lsSecure, otelSecure := c.secureMetricOption()
if err != nil {
return nil, fmt.Errorf("invalid metric view configuration: %v", err)
}
if c.UseLightstepMetricsSDK {
// Install the Lightstep metrics SDK
metricExporter, err := c.newMetricsExporter(lsSecure)
if err != nil {
return nil, fmt.Errorf("failed to create metric exporter: %v", err)
}
sdk := sdkmetric.NewMeterProvider(
sdkmetric.WithResource(c.Resource),
sdkmetric.WithReader(
sdkmetric.NewPeriodicReader(metricExporter, period),
lsPref,
),
)
provider = sdk
shutdown = func() error {
return sdk.Shutdown(context.Background())
}
} else {
// Install the OTel-Go community metrics SDK.
metricExporter, err := c.newOtelMetricsExporter(otelPref, otelSecure)
if err != nil {
return nil, fmt.Errorf("failed to create metric exporter: %v", err)
}
meterProvider := otelsdkmetric.NewMeterProvider(
otelsdkmetric.WithResource(c.Resource),
otelsdkmetric.WithReader(otelsdkmetric.NewPeriodicReader(
metricExporter,
otelsdkmetric.WithInterval(period),
)),
)
metricglobal.SetMeterProvider(meterProvider)
provider = meterProvider
shutdown = func() error {
return meterProvider.Shutdown(context.Background())
}
}
if c.MetricsBuiltinsEnabled {
for _, lib := range c.MetricsBuiltinLibraries {
name, version, _ := strings.Cut(lib, ":")
if version == "" {
version = defaultVersion
}
vm, has := builtinMetricsVersions[name]
if !has {
otel.Handle(fmt.Errorf("unrecognized builtin: %q", name))
continue
}
fs, has := vm[version]
if !has {
otel.Handle(fmt.Errorf("unrecognized builtin version: %v: %q", name, version))
continue
}
for _, f := range fs {
if err := f(provider); err != nil {
otel.Handle(fmt.Errorf("failed to start %v instrumentation: %w", name, err))
}
}
}
}
metricglobal.SetMeterProvider(provider)
return shutdown, nil
}
func (c PipelineConfig) newClient(secure otlpmetricgrpc.Option) (otlpmetric.Client, error) {
return otlpmetricgrpc.NewClient(
context.Background(),
secure,
otlpmetricgrpc.WithEndpoint(c.Endpoint),
otlpmetricgrpc.WithHeaders(c.Headers),
otlpmetricgrpc.WithCompressor(gzip.Name),
)
}
func (c PipelineConfig) newMetricsExporter(secure otlpmetricgrpc.Option) (*otlpmetric.Exporter, error) {
client, err := c.newClient(secure)
if err != nil {
return nil, err
}
return otlpmetric.New(client), nil
}
func (c PipelineConfig) newOtelMetricsExporter(temporality otelsdkmetric.TemporalitySelector, secureOpt otelotlpmetricgrpc.Option) (otelsdkmetric.Exporter, error) {
return otelotlpmetricgrpc.New(
context.Background(),
secureOpt,
otelotlpmetricgrpc.WithTemporalitySelector(temporality),
otelotlpmetricgrpc.WithEndpoint(c.Endpoint),
otelotlpmetricgrpc.WithHeaders(c.Headers),
otelotlpmetricgrpc.WithCompressor(gzip.Name),
)
}
func tempoOptions(c PipelineConfig) (view.Option, otelsdkmetric.TemporalitySelector, error) {
syncPref := aggregation.CumulativeTemporality
asyncPref := aggregation.CumulativeTemporality
var otelSelector otelsdkmetric.TemporalitySelector
switch lower := strings.ToLower(c.TemporalityPreference); lower {
case "delta":
// Delta means exercising the cumulative-to-delta
// export path. This is an unusual setting for
// Lightstep users to choose.
syncPref = aggregation.DeltaTemporality
asyncPref = aggregation.DeltaTemporality
// Note: the following is incorrect for UpDownCounter
// and async UpDownCounter, which the OTel
// specification stipulates are not affected by the
// preference setting. We WILL NOT FIX this defect.
// Instead, as otel-launcher-go v1.10.x will use the
// Lightstep metrics SDK by default.
otelSelector = func(otelsdkmetric.InstrumentKind) metricdata.Temporality {
return metricdata.DeltaTemporality
}
case "stateless":
// asyncPref set above.
syncPref = aggregation.DeltaTemporality
otelSelector = func(otelsdkmetric.InstrumentKind) metricdata.Temporality {
return metricdata.DeltaTemporality
}
case "", "cumulative":
// syncPref, asyncPref set above.
otelSelector = func(otelsdkmetric.InstrumentKind) metricdata.Temporality {
return metricdata.CumulativeTemporality
}
default:
return nil, nil, fmt.Errorf("invalid temporality preference: %v", c.TemporalityPreference)
}
return view.WithDefaultAggregationTemporalitySelector(
func(k sdkinstrument.Kind) aggregation.Temporality {
switch k {
case sdkinstrument.SyncUpDownCounter, sdkinstrument.AsyncUpDownCounter:
return aggregation.CumulativeTemporality
case sdkinstrument.SyncCounter, sdkinstrument.SyncHistogram:
return syncPref
default:
return asyncPref
}
},
), otelSelector, nil
}