Skip to content

Commit

Permalink
add start logs (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored May 5, 2024
1 parent 372bfb0 commit f70f29a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
6 changes: 2 additions & 4 deletions gcp/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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...)
Expand All @@ -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...)
Expand Down
4 changes: 2 additions & 2 deletions gcp/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
64 changes: 38 additions & 26 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down

0 comments on commit f70f29a

Please sign in to comment.