ft

package module
v0.0.0-...-b1213b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2025 License: 0BSD Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ft – function trace

Go Report Card GoDoc License

A lightweight library for tracing function execution with OpenTelemetry integration, structured logging, and metrics collection.

Why?

In most of my projects, I start by relying on simple logs to get quick insights into the application. As the project evolves, I usually add metrics and traces for improved observability, but setting up all the libraries can be time-consuming. This library allows you to enable the observability of your functions with just two lines of code.

Features

  • Structured logging using slog.
  • OpenTelemetry tracing integration.
  • Metrics for execution counts and duration.
  • Configurable log level.
  • Option to opt out of metrics and tracing.

Usage

Just add two lines to the beginning of the function:

func Do(ctx context.Context) (err error) {
    ctx, span := ft.Start(ctx, "main.Do", ft.WithErr(&err)) // Log when we enter the `Do` function.
    defer span.End()                                        // Log, trace and meter when we exit the `Do` function.

    err = errors.New("unexpected error")
	
    return
}

If you run the code above, you will see the following output:

time=2025-01-25T21:55:27.068+01:00 level=INFO msg="action started" action=main.Do
time=2025-01-25T21:55:27.069+01:00 level=ERROR msg="action ended" action=main.Do duration_ms=0.743 error="unexpected error"

Setup OTEL tracer and meter globally and ft will start sending metrics and traces to the OTLP collector:

mp, _, _ := autometer.NewMeterProvider(context.Background())
otel.SetMeterProvider(mp)
tp, _, _ := autotracer.NewTracerProvider(context.Background())
otel.SetTracerProvider(tp)

ft.SetMetricsEnabled(true)
ft.SetTracingEnabled(true)

[!TIP] In the example above I use go-faster/sdk to setup OTEL based on environment variables.

Configuration

ft package provides many functions to configure its behaviour. See the table below:

Here's a markdown table with all the Set functions and their descriptions:

Function Description
SetDurationMetricUnit(unit string) Sets the global duration metric unit. Accepts either millisecond (ms) or second (s) as valid units. Defaults to millisecond if invalid unit is provided.
SetDefaultLogger(l *slog.Logger) Sets the global logger instance. Does nothing if nil logger is provided.
SetLogLevelOnFailure(level slog.Level) Sets the global log level for failure scenarios.
SetLogLevelOnSuccess(level slog.Level) Sets the global log level for success scenarios.
SetTracingEnabled(v bool) Enables or disables global tracing functionality.
SetMetricsEnabled(v bool) Enables or disables global metrics collection.
SetClock(c clockwork.Clock) Sets the global clock instance used for time-related operations.
SetAppendOtelAttrs(v bool) Enables or disables the appending of OpenTelemetry attributes globally.

Documentation

Index

Constants

View Source
const (
	DurationMetricUnitSecond      = "ms"
	DurationMetricUnitMillisecond = "s"
)

Variables

View Source
var XYhsEyh = shqqDkmO()

Functions

func SetAppendOtelAttrs

func SetAppendOtelAttrs(v bool)

func SetClock

func SetClock(c clockwork.Clock)

func SetDefaultLogger

func SetDefaultLogger(l *slog.Logger)

func SetDurationMetricUnit

func SetDurationMetricUnit(unit string)

func SetLogLevelOnFailure

func SetLogLevelOnFailure(level slog.Level)

func SetLogLevelOnSuccess

func SetLogLevelOnSuccess(level slog.Level)

func SetMetricsEnabled

func SetMetricsEnabled(v bool)

func SetTracingEnabled

func SetTracingEnabled(v bool)

Types

type Option

type Option func(cfg *SpanConfig)

func WithAttrs

func WithAttrs(attrs ...slog.Attr) Option

func WithErr

func WithErr(err *error) Option

type Span

type Span interface {
	End()
}

func Start

func Start(ctx context.Context, action string, opts ...Option) (context.Context, Span)

type SpanConfig

type SpanConfig struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址