Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Po-Yao Chen committed Nov 12, 2024
1 parent d8754c2 commit 88fde96
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions otellib/otel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 Yahoo Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.

package otellib

import (
"context"
"crypto/tls"
"log"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
)

// InitOTelSDK initializes the OTel meter provider
func InitOTelSDK(ctx context.Context, collectorEndpoint string, tlsConfig *tls.Config, res *resource.Resource) (shutdown func(context.Context) error) {
// Set up an OTel exporter for metrics.
var opts []otlpmetrichttp.Option
opts = append(opts, otlpmetrichttp.WithEndpoint(collectorEndpoint))
opts = append(opts, otlpmetrichttp.WithInsecure())
//opts = append(opts, otlpmetrichttp.WithTLSClientConfig(tlsConfig))

metricExporter, err := otlpmetrichttp.New(ctx, opts...)
if err != nil {
log.Fatalf("failed to create metric exporter: %v", err)
}

// Set up a metric provider.
meterProvider := sdkmetric.NewMeterProvider(
sdkmetric.WithReader(
sdkmetric.NewPeriodicReader(metricExporter),
),
sdkmetric.WithResource(res),
)

otel.SetMeterProvider(meterProvider)

return meterProvider.Shutdown
}

0 comments on commit 88fde96

Please sign in to comment.