Skip to content

Commit

Permalink
story(issue-329): add type constraint for otel middleware (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaba505 authored Nov 21, 2024
1 parent c788be4 commit 1e6e1f0
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 434 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/z5labs/bedrock.svg)](https://pkg.go.dev/github.com/z5labs/bedrock)
[![Go Report Card](https://goreportcard.com/badge/github.com/z5labs/bedrock)](https://goreportcard.com/report/github.com/z5labs/bedrock)
![Coverage](https://img.shields.io/badge/Coverage-94.4%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-94.2%25-brightgreen)
[![build](https://github.com/z5labs/bedrock/actions/workflows/build.yaml/badge.svg)](https://github.com/z5labs/bedrock/actions/workflows/build.yaml)

**bedrock provides a minimal, modular and composable foundation for
Expand Down
155 changes: 52 additions & 103 deletions pkg/appbuilder/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,70 @@ import (
"go.opentelemetry.io/otel/trace"
)

type otelOptions struct {
initPropogator func(context.Context) (propagation.TextMapPropagator, error)
initTracerProvider func(context.Context) (trace.TracerProvider, error)
initMeterProvider func(context.Context) (metric.MeterProvider, error)
initLoggerProvider func(context.Context) (log.LoggerProvider, error)
// TextMapPropagatorInitializer
type TextMapPropagatorInitializer interface {
InitTextMapPropogator(context.Context) (propagation.TextMapPropagator, error)
}

// OTelOption
type OTelOption func(*otelOptions)

// OTelTextMapPropogator
func OTelTextMapPropogator(f func(context.Context) (propagation.TextMapPropagator, error)) OTelOption {
return func(oo *otelOptions) {
oo.initPropogator = f
}
// TracerProviderInitializer
type TracerProviderInitializer interface {
InitTracerProvider(context.Context) (trace.TracerProvider, error)
}

// OTelTracerProvider
func OTelTracerProvider(f func(context.Context) (trace.TracerProvider, error)) OTelOption {
return func(oo *otelOptions) {
oo.initTracerProvider = f
}
// MeterProviderInitializer
type MeterProviderInitializer interface {
InitMeterProvider(context.Context) (metric.MeterProvider, error)
}

// OTelMeterProvider
func OTelMeterProvider(f func(context.Context) (metric.MeterProvider, error)) OTelOption {
return func(oo *otelOptions) {
oo.initMeterProvider = f
}
// LoggerProviderInitializer
type LoggerProviderInitializer interface {
InitLoggerProvider(context.Context) (log.LoggerProvider, error)
}

// OTelLoggerProvider
func OTelLoggerProvider(f func(context.Context) (log.LoggerProvider, error)) OTelOption {
return func(oo *otelOptions) {
oo.initLoggerProvider = f
}
// OTelInitializer
type OTelInitializer interface {
TextMapPropagatorInitializer
TracerProviderInitializer
MeterProviderInitializer
LoggerProviderInitializer
}

// WithOTel
func WithOTel[T any](builder bedrock.AppBuilder[T], opts ...OTelOption) bedrock.AppBuilder[T] {
oo := &otelOptions{}
for _, opt := range opts {
opt(oo)
}

// OTel
func OTel[T OTelInitializer](builder bedrock.AppBuilder[T]) bedrock.AppBuilder[T] {
return bedrock.AppBuilderFunc[T](func(ctx context.Context, cfg T) (bedrock.App, error) {
fs := []func(context.Context) error{
initTextMapPropogator(oo),
initTracerProvider(oo),
initMeterProvider(oo),
initLoggerProvider(oo),
func(ctx context.Context) error {
tmp, err := cfg.InitTextMapPropogator(ctx)
if err != nil || tmp == nil {
return err
}
otel.SetTextMapPropagator(tmp)
return nil
},
func(ctx context.Context) error {
tp, err := cfg.InitTracerProvider(ctx)
if err != nil || tp == nil {
return err
}
otel.SetTracerProvider(tp)
return nil
},
func(ctx context.Context) error {
mp, err := cfg.InitMeterProvider(ctx)
if err != nil || mp == nil {
return err
}
otel.SetMeterProvider(mp)
return nil
},
func(ctx context.Context) error {
lp, err := cfg.InitLoggerProvider(ctx)
if err != nil || lp == nil {
return err
}
global.SetLoggerProvider(lp)
return nil
},
}

for _, f := range fs {
Expand All @@ -81,67 +94,3 @@ func WithOTel[T any](builder bedrock.AppBuilder[T], opts ...OTelOption) bedrock.
return builder.Build(ctx, cfg)
})
}

func initTextMapPropogator(oo *otelOptions) func(context.Context) error {
return func(ctx context.Context) error {
if oo.initPropogator == nil {
return nil
}

p, err := oo.initPropogator(ctx)
if err != nil {
return err
}

otel.SetTextMapPropagator(p)
return nil
}
}

func initTracerProvider(oo *otelOptions) func(context.Context) error {
return func(ctx context.Context) error {
if oo.initTracerProvider == nil {
return nil
}

tp, err := oo.initTracerProvider(ctx)
if err != nil {
return err
}

otel.SetTracerProvider(tp)
return nil
}
}

func initMeterProvider(oo *otelOptions) func(context.Context) error {
return func(ctx context.Context) error {
if oo.initMeterProvider == nil {
return nil
}

mp, err := oo.initMeterProvider(ctx)
if err != nil {
return err
}

otel.SetMeterProvider(mp)
return nil
}
}

func initLoggerProvider(oo *otelOptions) func(context.Context) error {
return func(ctx context.Context) error {
if oo.initLoggerProvider == nil {
return nil
}

lp, err := oo.initLoggerProvider(ctx)
if err != nil {
return err
}

global.SetLoggerProvider(lp)
return nil
}
}
Loading

0 comments on commit 1e6e1f0

Please sign in to comment.