Skip to content

Commit

Permalink
refactor: Telemetry instead of Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 6, 2025
1 parent 2eb964a commit 7185f03
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
11 changes: 9 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ const (
watchdogTimeout = shutdownTimeout + time.Second*5
)

// Go runs f until interrupt.
func Go(f func(ctx context.Context, t *Telemetry) error, op ...Option) {
Run(func(ctx context.Context, _ *zap.Logger, t *Telemetry) error {
return f(ctx, t)
}, op...)
}

// Run f until interrupt.
//
// If errors.Is(err, ctx.Err()) is valid for returned error, shutdown is considered graceful.
// Context is cancelled on SIGINT. After watchdogTimeout application is forcefully terminated
// with exitCodeWatchdog.
func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Option) {
func Run(f func(ctx context.Context, lg *zap.Logger, m *Telemetry) error, op ...Option) {
// Apply options.
opts := options{
zapConfig: zap.NewProductionConfig(),
Expand Down Expand Up @@ -77,7 +84,7 @@ func Run(f func(ctx context.Context, lg *zap.Logger, m *Metrics) error, op ...Op
panic(fmt.Sprintf("failed to get resource: %v", err))
}

m, err := newMetrics(ctx, lg.Named("metrics"), res, opts.meterOptions, opts.tracerOptions, opts.loggerOptions)
m, err := newTelemetry(ctx, lg.Named("metrics"), res, opts.meterOptions, opts.tracerOptions, opts.loggerOptions)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion app/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/go-faster/sdk/profiler"
)

func (m *Metrics) registerProfiler(mux *http.ServeMux) {
func (m *Telemetry) registerProfiler(mux *http.ServeMux) {
var routes []string
if v := os.Getenv("PPROF_ROUTES"); v != "" {
routes = strings.Split(v, ",")
Expand Down
29 changes: 16 additions & 13 deletions app/metrics.go → app/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ type httpEndpoint struct {
addr string
}

// Metrics implement common basic metrics and infrastructure to it.
type Metrics struct {
// Deprecated: use Telemetry.
type Metrics = Telemetry

// Telemetry implement common basic metrics and infrastructure to it.
type Telemetry struct {
lg *zap.Logger

prom *promClient.Registry
Expand All @@ -54,7 +57,7 @@ type Metrics struct {
shutdowns []shutdown
}

func (m *Metrics) registerShutdown(name string, fn func(ctx context.Context) error) {
func (m *Telemetry) registerShutdown(name string, fn func(ctx context.Context) error) {
m.shutdowns = append(m.shutdowns, shutdown{name: name, fn: fn})
}

Expand All @@ -63,11 +66,11 @@ type shutdown struct {
fn func(ctx context.Context) error
}

func (m *Metrics) String() string {
func (m *Telemetry) String() string {
return "metrics"
}

func (m *Metrics) run(ctx context.Context) error {
func (m *Telemetry) run(ctx context.Context) error {
defer m.lg.Debug("Stopped metrics")
wg, ctx := errgroup.WithContext(ctx)

Expand Down Expand Up @@ -101,7 +104,7 @@ func (m *Metrics) run(ctx context.Context) error {
return wg.Wait()
}

func (m *Metrics) shutdown(ctx context.Context) {
func (m *Telemetry) shutdown(ctx context.Context) {
var wg sync.WaitGroup

// Launch shutdowns in parallel.
Expand All @@ -127,28 +130,28 @@ func (m *Metrics) shutdown(ctx context.Context) {
wg.Wait()
}

func (m *Metrics) MeterProvider() metric.MeterProvider {
func (m *Telemetry) MeterProvider() metric.MeterProvider {
if m.meterProvider == nil {
return otel.GetMeterProvider()
}
return m.meterProvider
}

func (m *Metrics) TracerProvider() trace.TracerProvider {
func (m *Telemetry) TracerProvider() trace.TracerProvider {
if m.tracerProvider == nil {
return otel.GetTracerProvider()
}
return m.tracerProvider
}

func (m *Metrics) LoggerProvider() log.LoggerProvider {
func (m *Telemetry) LoggerProvider() log.LoggerProvider {
if m.loggerProvider == nil {
return noop.NewLoggerProvider()
}
return m.loggerProvider
}

func (m *Metrics) TextMapPropagator() propagation.TextMapPropagator {
func (m *Telemetry) TextMapPropagator() propagation.TextMapPropagator {
return m.propagator
}

Expand All @@ -172,21 +175,21 @@ func (z zapErrorHandler) Handle(err error) {
z.lg.Error("Error", zap.Error(err))
}

func newMetrics(
func newTelemetry(
ctx context.Context,
lg *zap.Logger,
res *resource.Resource,
meterOptions []autometer.Option,
tracerOptions []autotracer.Option,
logsOptions []autologs.Option,
) (*Metrics, error) {
) (*Telemetry, error) {
{
// Setup global OTEL logger and error handler.
logger := lg.Named("otel")
otel.SetLogger(zapr.NewLogger(logger))
otel.SetErrorHandler(zapErrorHandler{lg: logger})
}
m := &Metrics{
m := &Telemetry{
lg: lg,
resource: res,
}
Expand Down

0 comments on commit 7185f03

Please sign in to comment.