| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otelconf |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"math" |
| 12 |
"net/url" |
| 13 |
"os" |
| 14 |
"time" |
| 15 |
|
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" |
| 18 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" |
| 19 |
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" |
| 20 |
"go.opentelemetry.io/otel/metric" |
| 21 |
"go.opentelemetry.io/otel/metric/noop" |
| 22 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 23 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 24 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 25 |
"go.opentelemetry.io/otel/sdk/resource" |
| 26 |
"google.golang.org/grpc/credentials" |
| 27 |
|
| 28 |
"go.opentelemetry.io/contrib/otelconf/internal/tls" |
| 29 |
) |
| 30 |
|
| 31 |
var zeroScope instrumentation.Scope |
| 32 |
|
| 33 |
const instrumentKindUndefined = sdkmetric.InstrumentKind(0) |
| 34 |
|
| 35 |
func meterProvider(cfg configOptions, res *resource.Resource) (metric.MeterProvider, shutdownFunc, error) { |
| 36 |
if cfg.opentelemetryConfig.MeterProvider == nil { |
| 37 |
return noop.NewMeterProvider(), noopShutdown, nil |
| 38 |
} |
| 39 |
opts := append(cfg.meterProviderOptions, sdkmetric.WithResource(res)) |
| 40 |
|
| 41 |
var errs []error |
| 42 |
for _, reader := range cfg.opentelemetryConfig.MeterProvider.Readers { |
| 43 |
r, err := metricReader(cfg.ctx, reader) |
| 44 |
if err == nil { |
| 45 |
opts = append(opts, sdkmetric.WithReader(r)) |
| 46 |
} else { |
| 47 |
errs = append(errs, err) |
| 48 |
} |
| 49 |
} |
| 50 |
for _, vw := range cfg.opentelemetryConfig.MeterProvider.Views { |
| 51 |
v, err := view(vw) |
| 52 |
if err == nil { |
| 53 |
opts = append(opts, sdkmetric.WithView(v)) |
| 54 |
} else { |
| 55 |
errs = append(errs, err) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if len(errs) > 0 { |
| 60 |
return noop.NewMeterProvider(), noopShutdown, errors.Join(errs...) |
| 61 |
} |
| 62 |
|
| 63 |
mp := sdkmetric.NewMeterProvider(opts...) |
| 64 |
return mp, mp.Shutdown, nil |
| 65 |
} |
| 66 |
|
| 67 |
func metricReader(ctx context.Context, r MetricReader) (sdkmetric.Reader, error) { |
| 68 |
if r.Periodic != nil && r.Pull != nil { |
| 69 |
return nil, newErrInvalid("must not specify multiple metric reader type") |
| 70 |
} |
| 71 |
|
| 72 |
if r.Periodic != nil { |
| 73 |
var opts []sdkmetric.PeriodicReaderOption |
| 74 |
if r.Periodic.Interval != nil { |
| 75 |
opts = append(opts, sdkmetric.WithInterval(time.Duration(*r.Periodic.Interval)*time.Millisecond)) |
| 76 |
} |
| 77 |
|
| 78 |
if r.Periodic.Timeout != nil { |
| 79 |
opts = append(opts, sdkmetric.WithTimeout(time.Duration(*r.Periodic.Timeout)*time.Millisecond)) |
| 80 |
} |
| 81 |
return periodicExporter(ctx, r.Periodic.Exporter, opts...) |
| 82 |
} |
| 83 |
|
| 84 |
if r.Pull != nil { |
| 85 |
return pullReader(ctx, r.Pull.Exporter) |
| 86 |
} |
| 87 |
return nil, newErrInvalid("no valid metric reader") |
| 88 |
} |
| 89 |
|
| 90 |
func pullReader(_ context.Context, _ PullMetricExporter) (sdkmetric.Reader, error) { |
| 91 |
return nil, newErrInvalid("no valid metric exporter") |
| 92 |
} |
| 93 |
|
| 94 |
func periodicExporter(ctx context.Context, exporter PushMetricExporter, opts ...sdkmetric.PeriodicReaderOption) (sdkmetric.Reader, error) { |
| 95 |
exportersConfigured := 0 |
| 96 |
var exportFunc func() (sdkmetric.Reader, error) |
| 97 |
|
| 98 |
if exporter.Console != nil { |
| 99 |
exportersConfigured++ |
| 100 |
enc := json.NewEncoder(os.Stdout) |
| 101 |
enc.SetIndent("", " ") |
| 102 |
|
| 103 |
exp, err := stdoutmetric.New( |
| 104 |
stdoutmetric.WithEncoder(enc), |
| 105 |
) |
| 106 |
if err != nil { |
| 107 |
return nil, err |
| 108 |
} |
| 109 |
exportFunc = func() (sdkmetric.Reader, error) { |
| 110 |
return sdkmetric.NewPeriodicReader(exp, opts...), nil |
| 111 |
} |
| 112 |
} |
| 113 |
if exporter.OTLPHttp != nil { |
| 114 |
exportersConfigured++ |
| 115 |
exp, err := otlpHTTPMetricExporter(ctx, exporter.OTLPHttp) |
| 116 |
if err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
exportFunc = func() (sdkmetric.Reader, error) { |
| 120 |
return sdkmetric.NewPeriodicReader(exp, opts...), nil |
| 121 |
} |
| 122 |
} |
| 123 |
if exporter.OTLPGrpc != nil { |
| 124 |
exportersConfigured++ |
| 125 |
exp, err := otlpGRPCMetricExporter(ctx, exporter.OTLPGrpc) |
| 126 |
if err != nil { |
| 127 |
return nil, err |
| 128 |
} |
| 129 |
exportFunc = func() (sdkmetric.Reader, error) { |
| 130 |
return sdkmetric.NewPeriodicReader(exp, opts...), nil |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if exportersConfigured > 1 { |
| 135 |
return nil, newErrInvalid("must not specify multiple exporters") |
| 136 |
} |
| 137 |
|
| 138 |
if exportFunc != nil { |
| 139 |
return exportFunc() |
| 140 |
} |
| 141 |
|
| 142 |
return nil, newErrInvalid("no valid metric exporter") |
| 143 |
} |
| 144 |
|
| 145 |
func otlpHTTPMetricExporter(ctx context.Context, otlpConfig *OTLPHttpMetricExporter) (sdkmetric.Exporter, error) { |
| 146 |
opts := []otlpmetrichttp.Option{} |
| 147 |
|
| 148 |
if otlpConfig.Endpoint != nil { |
| 149 |
u, err := url.ParseRequestURI(*otlpConfig.Endpoint) |
| 150 |
if err != nil { |
| 151 |
return nil, errors.Join(newErrInvalid("endpoint parsing failed"), err) |
| 152 |
} |
| 153 |
opts = append(opts, otlpmetrichttp.WithEndpoint(u.Host)) |
| 154 |
|
| 155 |
if u.Scheme == "http" { |
| 156 |
opts = append(opts, otlpmetrichttp.WithInsecure()) |
| 157 |
} |
| 158 |
if u.Path != "" { |
| 159 |
opts = append(opts, otlpmetrichttp.WithURLPath(u.Path)) |
| 160 |
} |
| 161 |
} |
| 162 |
if otlpConfig.Compression != nil { |
| 163 |
switch *otlpConfig.Compression { |
| 164 |
case compressionGzip: |
| 165 |
opts = append(opts, otlpmetrichttp.WithCompression(otlpmetrichttp.GzipCompression)) |
| 166 |
case compressionNone: |
| 167 |
opts = append(opts, otlpmetrichttp.WithCompression(otlpmetrichttp.NoCompression)) |
| 168 |
default: |
| 169 |
return nil, newErrInvalid(fmt.Sprintf("unsupported compression %q", *otlpConfig.Compression)) |
| 170 |
} |
| 171 |
} |
| 172 |
if otlpConfig.Timeout != nil { |
| 173 |
opts = append(opts, otlpmetrichttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 174 |
} |
| 175 |
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList) |
| 176 |
if err != nil { |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
if len(headersConfig) > 0 { |
| 180 |
opts = append(opts, otlpmetrichttp.WithHeaders(headersConfig)) |
| 181 |
} |
| 182 |
if otlpConfig.TemporalityPreference != nil { |
| 183 |
switch *otlpConfig.TemporalityPreference { |
| 184 |
case ExporterTemporalityPreferenceDelta: |
| 185 |
opts = append(opts, otlpmetrichttp.WithTemporalitySelector(deltaTemporality)) |
| 186 |
case ExporterTemporalityPreferenceCumulative: |
| 187 |
opts = append(opts, otlpmetrichttp.WithTemporalitySelector(cumulativeTemporality)) |
| 188 |
case ExporterTemporalityPreferenceLowMemory: |
| 189 |
opts = append(opts, otlpmetrichttp.WithTemporalitySelector(lowMemory)) |
| 190 |
default: |
| 191 |
return nil, newErrInvalid(fmt.Sprintf("unsupported temporality preference %q", *otlpConfig.TemporalityPreference)) |
| 192 |
} |
| 193 |
} |
| 194 |
if otlpConfig.Tls != nil { |
| 195 |
tlsConfig, err := tls.CreateConfig(otlpConfig.Tls.CaFile, otlpConfig.Tls.CertFile, otlpConfig.Tls.KeyFile) |
| 196 |
if err != nil { |
| 197 |
return nil, errors.Join(newErrInvalid("tls configuration"), err) |
| 198 |
} |
| 199 |
opts = append(opts, otlpmetrichttp.WithTLSClientConfig(tlsConfig)) |
| 200 |
} |
| 201 |
|
| 202 |
return otlpmetrichttp.New(ctx, opts...) |
| 203 |
} |
| 204 |
|
| 205 |
func otlpGRPCMetricExporter(ctx context.Context, otlpConfig *OTLPGrpcMetricExporter) (sdkmetric.Exporter, error) { |
| 206 |
var opts []otlpmetricgrpc.Option |
| 207 |
|
| 208 |
if otlpConfig.Endpoint != nil { |
| 209 |
u, err := url.ParseRequestURI(*otlpConfig.Endpoint) |
| 210 |
if err != nil { |
| 211 |
return nil, errors.Join(newErrInvalid("endpoint parsing failed"), err) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if u.Host != "" { |
| 219 |
opts = append(opts, otlpmetricgrpc.WithEndpoint(u.Host)) |
| 220 |
} else { |
| 221 |
opts = append(opts, otlpmetricgrpc.WithEndpoint(*otlpConfig.Endpoint)) |
| 222 |
} |
| 223 |
if u.Scheme == "http" || (u.Scheme != "https" && otlpConfig.Tls != nil && otlpConfig.Tls.Insecure != nil && *otlpConfig.Tls.Insecure) { |
| 224 |
opts = append(opts, otlpmetricgrpc.WithInsecure()) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if otlpConfig.Compression != nil { |
| 229 |
switch *otlpConfig.Compression { |
| 230 |
case compressionGzip: |
| 231 |
opts = append(opts, otlpmetricgrpc.WithCompressor(*otlpConfig.Compression)) |
| 232 |
case compressionNone: |
| 233 |
|
| 234 |
default: |
| 235 |
return nil, newErrInvalid(fmt.Sprintf("unsupported compression %q", *otlpConfig.Compression)) |
| 236 |
} |
| 237 |
} |
| 238 |
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 { |
| 239 |
opts = append(opts, otlpmetricgrpc.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout))) |
| 240 |
} |
| 241 |
headersConfig, err := createHeadersConfig(otlpConfig.Headers, otlpConfig.HeadersList) |
| 242 |
if err != nil { |
| 243 |
return nil, err |
| 244 |
} |
| 245 |
if len(headersConfig) > 0 { |
| 246 |
opts = append(opts, otlpmetricgrpc.WithHeaders(headersConfig)) |
| 247 |
} |
| 248 |
if otlpConfig.TemporalityPreference != nil { |
| 249 |
switch *otlpConfig.TemporalityPreference { |
| 250 |
case ExporterTemporalityPreferenceDelta: |
| 251 |
opts = append(opts, otlpmetricgrpc.WithTemporalitySelector(deltaTemporality)) |
| 252 |
case ExporterTemporalityPreferenceCumulative: |
| 253 |
opts = append(opts, otlpmetricgrpc.WithTemporalitySelector(cumulativeTemporality)) |
| 254 |
case ExporterTemporalityPreferenceLowMemory: |
| 255 |
opts = append(opts, otlpmetricgrpc.WithTemporalitySelector(lowMemory)) |
| 256 |
default: |
| 257 |
return nil, newErrInvalid(fmt.Sprintf("unsupported temporality preference %q", *otlpConfig.TemporalityPreference)) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if otlpConfig.Tls != nil && (otlpConfig.Tls.CaFile != nil || otlpConfig.Tls.CertFile != nil || otlpConfig.Tls.KeyFile != nil) { |
| 262 |
tlsConfig, err := tls.CreateConfig(otlpConfig.Tls.CaFile, otlpConfig.Tls.CertFile, otlpConfig.Tls.KeyFile) |
| 263 |
if err != nil { |
| 264 |
return nil, errors.Join(newErrInvalid("tls configuration"), err) |
| 265 |
} |
| 266 |
opts = append(opts, otlpmetricgrpc.WithTLSCredentials(credentials.NewTLS(tlsConfig))) |
| 267 |
} |
| 268 |
|
| 269 |
return otlpmetricgrpc.New(ctx, opts...) |
| 270 |
} |
| 271 |
|
| 272 |
func cumulativeTemporality(sdkmetric.InstrumentKind) metricdata.Temporality { |
| 273 |
return metricdata.CumulativeTemporality |
| 274 |
} |
| 275 |
|
| 276 |
func deltaTemporality(ik sdkmetric.InstrumentKind) metricdata.Temporality { |
| 277 |
switch ik { |
| 278 |
case sdkmetric.InstrumentKindCounter, sdkmetric.InstrumentKindHistogram, sdkmetric.InstrumentKindObservableCounter: |
| 279 |
return metricdata.DeltaTemporality |
| 280 |
default: |
| 281 |
return metricdata.CumulativeTemporality |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
func lowMemory(ik sdkmetric.InstrumentKind) metricdata.Temporality { |
| 286 |
switch ik { |
| 287 |
case sdkmetric.InstrumentKindCounter, sdkmetric.InstrumentKindHistogram: |
| 288 |
return metricdata.DeltaTemporality |
| 289 |
default: |
| 290 |
return metricdata.CumulativeTemporality |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
func newIncludeExcludeFilter(lists *IncludeExclude) (attribute.Filter, error) { |
| 300 |
if lists == nil { |
| 301 |
return func(attribute.KeyValue) bool { return true }, nil |
| 302 |
} |
| 303 |
|
| 304 |
included := make(map[attribute.Key]struct{}) |
| 305 |
for _, k := range lists.Included { |
| 306 |
included[attribute.Key(k)] = struct{}{} |
| 307 |
} |
| 308 |
excluded := make(map[attribute.Key]struct{}) |
| 309 |
for _, k := range lists.Excluded { |
| 310 |
if _, ok := included[attribute.Key(k)]; ok { |
| 311 |
return nil, fmt.Errorf("attribute cannot be in both include and exclude list: %s", k) |
| 312 |
} |
| 313 |
excluded[attribute.Key(k)] = struct{}{} |
| 314 |
} |
| 315 |
return func(kv attribute.KeyValue) bool { |
| 316 |
|
| 317 |
if _, ok := excluded[kv.Key]; ok { |
| 318 |
return false |
| 319 |
} |
| 320 |
|
| 321 |
if len(included) == 0 { |
| 322 |
return true |
| 323 |
} |
| 324 |
|
| 325 |
_, ok := included[kv.Key] |
| 326 |
return ok |
| 327 |
}, nil |
| 328 |
} |
| 329 |
|
| 330 |
func view(v View) (sdkmetric.View, error) { |
| 331 |
inst, err := instrument(v.Selector) |
| 332 |
if err != nil { |
| 333 |
return nil, err |
| 334 |
} |
| 335 |
|
| 336 |
s, err := stream(v.Stream) |
| 337 |
if err != nil { |
| 338 |
return nil, err |
| 339 |
} |
| 340 |
return sdkmetric.NewView(inst, s), nil |
| 341 |
} |
| 342 |
|
| 343 |
func instrument(vs ViewSelector) (sdkmetric.Instrument, error) { |
| 344 |
kind, err := instrumentKind(vs.InstrumentType) |
| 345 |
if err != nil { |
| 346 |
return sdkmetric.Instrument{}, fmt.Errorf("view_selector: %w", err) |
| 347 |
} |
| 348 |
inst := sdkmetric.Instrument{ |
| 349 |
Name: strOrEmpty(vs.InstrumentName), |
| 350 |
Unit: strOrEmpty(vs.Unit), |
| 351 |
Kind: kind, |
| 352 |
Scope: instrumentation.Scope{ |
| 353 |
Name: strOrEmpty(vs.MeterName), |
| 354 |
Version: strOrEmpty(vs.MeterVersion), |
| 355 |
SchemaURL: strOrEmpty(vs.MeterSchemaUrl), |
| 356 |
}, |
| 357 |
} |
| 358 |
|
| 359 |
if instrumentIsEmpty(inst) { |
| 360 |
return sdkmetric.Instrument{}, errors.New("view_selector: empty selector not supporter") |
| 361 |
} |
| 362 |
return inst, nil |
| 363 |
} |
| 364 |
|
| 365 |
func stream(vs ViewStream) (sdkmetric.Stream, error) { |
| 366 |
f, err := newIncludeExcludeFilter(vs.AttributeKeys) |
| 367 |
if err != nil { |
| 368 |
return sdkmetric.Stream{}, err |
| 369 |
} |
| 370 |
return sdkmetric.Stream{ |
| 371 |
Name: strOrEmpty(vs.Name), |
| 372 |
Description: strOrEmpty(vs.Description), |
| 373 |
Aggregation: aggregation(vs.Aggregation), |
| 374 |
AttributeFilter: f, |
| 375 |
}, nil |
| 376 |
} |
| 377 |
|
| 378 |
func aggregation(aggr *Aggregation) sdkmetric.Aggregation { |
| 379 |
if aggr == nil { |
| 380 |
return nil |
| 381 |
} |
| 382 |
|
| 383 |
if aggr.Base2ExponentialBucketHistogram != nil { |
| 384 |
return sdkmetric.AggregationBase2ExponentialHistogram{ |
| 385 |
MaxSize: int32OrZero(aggr.Base2ExponentialBucketHistogram.MaxSize), |
| 386 |
MaxScale: int32OrZero(aggr.Base2ExponentialBucketHistogram.MaxScale), |
| 387 |
|
| 388 |
NoMinMax: !boolOrFalse(aggr.Base2ExponentialBucketHistogram.RecordMinMax), |
| 389 |
} |
| 390 |
} |
| 391 |
if aggr.Default != nil { |
| 392 |
|
| 393 |
return nil |
| 394 |
} |
| 395 |
if aggr.Drop != nil { |
| 396 |
return sdkmetric.AggregationDrop{} |
| 397 |
} |
| 398 |
if aggr.ExplicitBucketHistogram != nil { |
| 399 |
return sdkmetric.AggregationExplicitBucketHistogram{ |
| 400 |
Boundaries: aggr.ExplicitBucketHistogram.Boundaries, |
| 401 |
|
| 402 |
NoMinMax: !boolOrFalse(aggr.ExplicitBucketHistogram.RecordMinMax), |
| 403 |
} |
| 404 |
} |
| 405 |
if aggr.LastValue != nil { |
| 406 |
return sdkmetric.AggregationLastValue{} |
| 407 |
} |
| 408 |
if aggr.Sum != nil { |
| 409 |
return sdkmetric.AggregationSum{} |
| 410 |
} |
| 411 |
return nil |
| 412 |
} |
| 413 |
|
| 414 |
func instrumentKind(vsit *InstrumentType) (sdkmetric.InstrumentKind, error) { |
| 415 |
if vsit == nil { |
| 416 |
|
| 417 |
return instrumentKindUndefined, nil |
| 418 |
} |
| 419 |
|
| 420 |
switch *vsit { |
| 421 |
case InstrumentTypeCounter: |
| 422 |
return sdkmetric.InstrumentKindCounter, nil |
| 423 |
case InstrumentTypeUpDownCounter: |
| 424 |
return sdkmetric.InstrumentKindUpDownCounter, nil |
| 425 |
case InstrumentTypeHistogram: |
| 426 |
return sdkmetric.InstrumentKindHistogram, nil |
| 427 |
case InstrumentTypeObservableCounter: |
| 428 |
return sdkmetric.InstrumentKindObservableCounter, nil |
| 429 |
case InstrumentTypeObservableUpDownCounter: |
| 430 |
return sdkmetric.InstrumentKindObservableUpDownCounter, nil |
| 431 |
case InstrumentTypeObservableGauge: |
| 432 |
return sdkmetric.InstrumentKindObservableGauge, nil |
| 433 |
} |
| 434 |
|
| 435 |
return instrumentKindUndefined, errors.New("instrument_type: invalid value") |
| 436 |
} |
| 437 |
|
| 438 |
func instrumentIsEmpty(i sdkmetric.Instrument) bool { |
| 439 |
return i.Name == "" && |
| 440 |
i.Description == "" && |
| 441 |
i.Kind == instrumentKindUndefined && |
| 442 |
i.Unit == "" && |
| 443 |
i.Scope == zeroScope |
| 444 |
} |
| 445 |
|
| 446 |
func boolOrFalse(pBool *bool) bool { |
| 447 |
if pBool == nil { |
| 448 |
return false |
| 449 |
} |
| 450 |
return *pBool |
| 451 |
} |
| 452 |
|
| 453 |
func int32OrZero(pInt *int) int32 { |
| 454 |
if pInt == nil { |
| 455 |
return 0 |
| 456 |
} |
| 457 |
i := *pInt |
| 458 |
if i > math.MaxInt32 { |
| 459 |
return math.MaxInt32 |
| 460 |
} |
| 461 |
if i < math.MinInt32 { |
| 462 |
return math.MinInt32 |
| 463 |
} |
| 464 |
return int32(i) |
| 465 |
} |
| 466 |
|
| 467 |
func strOrEmpty(pStr *string) string { |
| 468 |
if pStr == nil { |
| 469 |
return "" |
| 470 |
} |
| 471 |
return *pStr |
| 472 |
} |