Skip to content

Commit

Permalink
feat: add WithOpenTelemetryEnabled option
Browse files Browse the repository at this point in the history
  • Loading branch information
sysulq committed Jun 4, 2024
1 parent b2782c9 commit 0dbd532
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
60 changes: 38 additions & 22 deletions kod.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ func WithInterceptors(interceptors ...interceptor.Interceptor) func(*options) {
}
}

// WithOpenTelemetryEnabled is an option setter for enabling OpenTelemetry.
func WithOpenTelemetryEnabled() func(*options) {
return func(opts *options) {
opts.enableOpenTelemetry = true
}
}

// Run initializes and runs the application with the provided main component and options.
func Run[T any, _ PointerToMain[T]](ctx context.Context, run func(context.Context, *T) error, opts ...func(*options)) error {
opt := &options{}
Expand Down Expand Up @@ -278,11 +285,12 @@ type Kod struct {

// options defines the configuration options for Kod.
type options struct {
configFilename string
fakes map[reflect.Type]any
logWrapper func(slog.Handler) slog.Handler
registrations []*Registration
interceptors []interceptor.Interceptor
enableOpenTelemetry bool
configFilename string
fakes map[reflect.Type]any
logWrapper func(slog.Handler) slog.Handler
registrations []*Registration
interceptors []interceptor.Interceptor
}

// newKod creates a new instance of Kod with the provided registrations and options.
Expand Down Expand Up @@ -317,7 +325,14 @@ func newKod(ctx context.Context, opts options) (*Kod, error) {
return nil, err
}

kod.initOpenTelemetry(ctx)
if opts.enableOpenTelemetry && os.Getenv("OTEL_SDK_DISABLED") != "true" {
kod.initOpenTelemetry(ctx)
} else {
kod.log = kod.newSlog(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}))
}

return kod, nil
}
Expand Down Expand Up @@ -377,12 +392,8 @@ func (k *Kod) parseConfig(filename string) error {
return vip.UnmarshalKey("kod", &k.config)
}

// initOpenTelemetry initializes OpenTelemetry with the provided context.
func (k *Kod) initOpenTelemetry(ctx context.Context) {
if os.Getenv("OTEL_SDK_DISABLED") == "true" {
k.log = slog.Default()
return
}

lo.Must0(host.Start())
lo.Must0(runtime.Start())

Expand All @@ -402,6 +413,7 @@ func (k *Kod) initOpenTelemetry(ctx context.Context) {
k.configureLog(ctx, res)
}

// configureTrace configures the trace provider with the provided context and resource.
func (k *Kod) configureTrace(ctx context.Context, res *resource.Resource) {
spanExporter := lo.Must(autoexport.NewSpanExporter(ctx))
spanProvider := trace.NewTracerProvider(
Expand All @@ -417,6 +429,7 @@ func (k *Kod) configureTrace(ctx context.Context, res *resource.Resource) {
})
}

// configureMetric configures the metric provider with the provided context and resource.
func (k *Kod) configureMetric(ctx context.Context, res *resource.Resource) {
metricReader := lo.Must(autoexport.NewMetricReader(ctx))
metricProvider := metric.NewMeterProvider(
Expand All @@ -432,6 +445,7 @@ func (k *Kod) configureMetric(ctx context.Context, res *resource.Resource) {
})
}

// configureLog configures the log provider with the provided context and resource.
func (k *Kod) configureLog(ctx context.Context, res *resource.Resource) {
logExporter := lo.Must(getLogAutoExporter(ctx))
loggerProvider := log.NewLoggerProvider(
Expand All @@ -448,22 +462,24 @@ func (k *Kod) configureLog(ctx context.Context, res *resource.Resource) {
Fn: loggerProvider.Shutdown,
})

var handler slog.Handler
k.log = k.newSlog(otelslog.NewHandler(k.config.Name,
otelslog.WithSchemaURL(PkgPath),
otelslog.WithVersion(k.config.Version),
))
}

// newSlog creates a new slog logger with the provided handler.
func (k *Kod) newSlog(handler slog.Handler) *slog.Logger {
if k.opts.logWrapper != nil {
handler = k.opts.logWrapper(otelslog.NewHandler(k.config.Name,
otelslog.WithSchemaURL(PkgPath),
otelslog.WithVersion(k.config.Version),
))
} else {
handler = otelslog.NewHandler(k.config.Name,
otelslog.WithSchemaURL(PkgPath),
otelslog.WithVersion(k.config.Version),
)
handler = k.opts.logWrapper(handler)
}

k.log = slog.New(handler)
return slog.New(handler)
}

// getLogAutoExporter returns a log exporter based on the environment variables.
// The default exporter is OTLP over HTTP/Protobuf.
// NOTICE: It would be removed when the OpenTelemetry SDK supports the log auto exporter.
func getLogAutoExporter(ctx context.Context) (log.Exporter, error) {
var (
err error
Expand Down
37 changes: 17 additions & 20 deletions tests/case1/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import (
"github.com/go-kod/kod/interceptor/ktrace"
"github.com/go-kod/kod/interceptor/kvalidate"
"github.com/samber/lo"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

type test1Config struct {
Expand Down Expand Up @@ -140,24 +137,24 @@ func Run(ctx context.Context, app *App) error {
return err
}

func StartTrace(ctx context.Context) context.Context {
var opts []sdktrace.TracerProviderOption
// func StartTrace(ctx context.Context) context.Context {
// var opts []sdktrace.TracerProviderOption

provider := sdktrace.NewTracerProvider(opts...)
otel.SetTracerProvider(provider)
// provider := sdktrace.NewTracerProvider(opts...)
// otel.SetTracerProvider(provider)

exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
if err != nil {
panic(err)
} else {
provider.RegisterSpanProcessor(sdktrace.NewSimpleSpanProcessor(exporter))
}
// exporter, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
// if err != nil {
// panic(err)
// } else {
// provider.RegisterSpanProcessor(sdktrace.NewSimpleSpanProcessor(exporter))
// }

ctx, span := otel.Tracer("").Start(ctx, "Run")
defer func() {
span.End()
fmt.Println("!!!!!!")
}()
// ctx, span := otel.Tracer("").Start(ctx, "Run")
// defer func() {
// span.End()
// fmt.Println("!!!!!!")
// }()

return ctx
}
// return ctx
// }
1 change: 0 additions & 1 deletion tests/case1/case_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func TestHttpHandler(t *testing.T) {
record := httptest.NewRecorder()

r, _ := http.NewRequest(http.MethodGet, "/hello/gin", nil)
ctx = StartTrace(ctx)
r = r.WithContext(ctx)

k.Foo(record, r)
Expand Down
4 changes: 2 additions & 2 deletions tests/case1/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestImpl(t *testing.T) {
func TestInterface(t *testing.T) {
t.Parallel()
kod.RunTest(t, func(ctx context.Context, k Test1Component) {
ctx = StartTrace(ctx)
// ctx = StartTrace(ctx)

ctx, span := otel.Tracer("").Start(ctx, "Run", trace.WithSpanKind(trace.SpanKindInternal))
defer func() {
Expand All @@ -61,7 +61,7 @@ func TestInterface(t *testing.T) {
require.Equal(t, "test1:B", err.Error())
require.True(t, span.SpanContext().IsValid())
require.Equal(t, 2, res.Id)
})
}, kod.WithOpenTelemetryEnabled())
}

func TestInterfacePanic(t *testing.T) {
Expand Down

0 comments on commit 0dbd532

Please sign in to comment.