diff --git a/gcp/option.go b/gcp/option.go index c11b7ce..0238ff9 100644 --- a/gcp/option.go +++ b/gcp/option.go @@ -7,8 +7,6 @@ import ( "github.com/nil-go/sloth/gcp" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" ) // WithProject provides the GCP project ID. @@ -50,7 +48,7 @@ func WithTrace(opts ...otlptracegrpc.Option) Option { return func(options *options) { if options.traceOpts == nil { options.traceOpts = []otlptracegrpc.Option{ - otlptracegrpc.WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), + otlptracegrpc.WithInsecure(), } } options.traceOpts = append(options.traceOpts, opts...) @@ -62,7 +60,7 @@ func WithMetric(opts ...otlpmetricgrpc.Option) Option { return func(options *options) { if options.metricOpts == nil { options.metricOpts = []otlpmetricgrpc.Option{ - otlpmetricgrpc.WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())), + otlpmetricgrpc.WithInsecure(), } } options.metricOpts = append(options.metricOpts, opts...) diff --git a/gcp/profiler.go b/gcp/profiler.go index 6366339..2eca6a8 100644 --- a/gcp/profiler.go +++ b/gcp/profiler.go @@ -45,15 +45,15 @@ func profile(options *options) func(context.Context) error { } return func(ctx context.Context) error { - slog.LogAttrs(ctx, slog.LevelInfo, "Start cloud profiler.", slog.String("project", options.project)) if err := profiler.Start(profiler.Config{ ProjectID: options.project, Service: options.service, ServiceVersion: options.version, MutexProfiling: options.mutextProfiling, }, options.profilerOpts...); err != nil { - return fmt.Errorf("start cloud profiler: %w", err) + return fmt.Errorf("start cloud profiling: %w", err) } + slog.LogAttrs(ctx, slog.LevelInfo, "Cloud profiling has been initialized.") return nil } diff --git a/pprof.go b/pprof.go index dc42128..41917d2 100644 --- a/pprof.go +++ b/pprof.go @@ -38,7 +38,7 @@ func PProf(ctx context.Context) error { slog.LogAttrs(ctx, slog.LevelInfo, "Shutdown pprof Server completed.") })() - slog.LogAttrs(ctx, slog.LevelInfo, "Starting pprof server.") + slog.LogAttrs(ctx, slog.LevelInfo, "Starting pprof server...") listener, err := net.Listen("tcp", "localhost:6060") if err != nil { listener, err = net.Listen("tcp", "localhost:0") diff --git a/run.go b/run.go index 1ffcf58..86b5aad 100644 --- a/run.go +++ b/run.go @@ -34,10 +34,12 @@ import ( // - func(context.Context) error func Run(args ...any) error { //nolint:cyclop,funlen var ( - configOpts []config.Option - logOpts []log.Option - runOpts []run.Option - runners []func(context.Context) error + configOpts []config.Option + logOpts []log.Option + runOpts []run.Option + runners []func(context.Context) error + traceProvider trace.TracerProvider + meterProvider metric.MeterProvider ) for _, arg := range args { switch opt := arg.(type) { @@ -48,18 +50,7 @@ func Run(args ...any) error { //nolint:cyclop,funlen case log.Option: logOpts = append(logOpts, opt) case trace.TracerProvider: - otel.SetTracerProvider(opt) - otel.SetTextMapPropagator( - propagation.NewCompositeTextMapPropagator( - propagation.TraceContext{}, - propagation.Baggage{}, - ), - ) - if provider, ok := opt.(interface { - Shutdown(ctx context.Context) error - }); ok { - runOpts = append(runOpts, run.WithPostRun(provider.Shutdown)) - } + traceProvider = opt logOpts = append([]log.Option{ log.WithSampler(func(ctx context.Context) bool { sc := trace.SpanContextFromContext(ctx) @@ -68,12 +59,7 @@ func Run(args ...any) error { //nolint:cyclop,funlen }), }, logOpts...) case metric.MeterProvider: - otel.SetMeterProvider(opt) - if provider, ok := opt.(interface { - Shutdown(ctx context.Context) error - }); ok { - runOpts = append(runOpts, run.WithPostRun(provider.Shutdown)) - } + meterProvider = opt case run.Option: runOpts = append(runOpts, opt) case func(context.Context) error: @@ -83,16 +69,42 @@ func Run(args ...any) error { //nolint:cyclop,funlen } } - // Initialize the global konf.Config. + logger := log.New(logOpts...) + slog.SetDefault(logger) + slog.Info("Logger has been initialized.") + cfg, err := config.New(configOpts...) if err != nil { return fmt.Errorf("init config: %w", err) } konf.SetDefault(cfg) + slog.Info("Config has been initialized.") - // Initialize the global slog.Logger. - logger := log.New(logOpts...) - slog.SetDefault(logger) + if traceProvider != nil { + otel.SetTracerProvider(traceProvider) + otel.SetTextMapPropagator( + propagation.NewCompositeTextMapPropagator( + propagation.TraceContext{}, + propagation.Baggage{}, + ), + ) + if provider, ok := traceProvider.(interface { + Shutdown(ctx context.Context) error + }); ok { + runOpts = append(runOpts, run.WithPostRun(provider.Shutdown)) + } + slog.Info("Trace provider has been initialized.") + } + + if meterProvider != nil { + otel.SetMeterProvider(meterProvider) + if provider, ok := meterProvider.(interface { + Shutdown(ctx context.Context) error + }); ok { + runOpts = append(runOpts, run.WithPostRun(provider.Shutdown)) + } + slog.Info("Meter provider has been initialized.") + } runner := run.New(append(runOpts, run.WithPreRun(cfg.Watch))...) if err := runner.Run(context.Background(), runners...); err != nil { diff --git a/run_test.go b/run_test.go index f6276c3..d91d555 100644 --- a/run_test.go +++ b/run_test.go @@ -56,7 +56,11 @@ func TestRun(t *testing.T) { assert.NoError(t, err) assert.Equal(t, true, started) - assert.Equal(t, `{"level":"INFO","msg":"info log","source":"fs"} + assert.Equal(t, `{"level":"INFO","msg":"Logger has been initialized."} +{"level":"INFO","msg":"Config has been initialized."} +{"level":"INFO","msg":"Trace provider has been initialized."} +{"level":"INFO","msg":"Meter provider has been initialized."} +{"level":"INFO","msg":"info log","source":"fs"} `, buf.String()) }