feat(telemetry): added base functions

added base function for OTEL Setup
This commit is contained in:
SoXX 2024-08-26 13:00:51 +02:00
parent 119d3b6d0d
commit 0ab2d12c39
3 changed files with 88 additions and 0 deletions

28
pkg/telemetry/metrics.go Normal file
View File

@ -0,0 +1,28 @@
package telemetry
import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
)
func SetupMeterProvider(plugName string) error {
res, err := newResource(plugName)
if err != nil {
return err
}
promReader, err := prometheus.New()
if err != nil {
return err
}
meterProvider := metric.NewMeterProvider(
metric.WithResource(res),
metric.WithReader(promReader),
)
otel.SetMeterProvider(meterProvider)
return nil
}

19
pkg/telemetry/otlp.go Normal file
View File

@ -0,0 +1,19 @@
package telemetry
import (
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)
func newResource(serviceName string) (*resource.Resource, error) {
otelResource, err := resource.Merge(resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceName(serviceName),
))
if err != nil {
return nil, err
}
return otelResource, nil
}

41
pkg/telemetry/tracing.go Normal file
View File

@ -0,0 +1,41 @@
package telemetry
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/trace"
)
func SetupTraceProvider(ctx context.Context, plugName string) error {
res, err := newResource(plugName)
if err != nil {
return err
}
exporter, err := traceExporter(ctx)
if err != nil {
return err
}
provider := trace.NewTracerProvider(
trace.WithResource(res),
trace.WithBatcher(exporter),
)
otel.SetTracerProvider(provider)
return nil
}
func traceExporter(ctx context.Context) (trace.SpanExporter, error) {
spanExporter, err := otlptracegrpc.New(ctx)
if err != nil {
return nil, err
}
return spanExporter, nil
}