ft

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: 0BSD Imports: 16 Imported by: 0

README

ft – function trace

Go Report Card GoDoc License

A very simple utility library written in Go to log and trace a function lifecycle.

Why?

Because I'm tired of adding the same boilerplate code to every function I write, that usually requires three or more lines of code to log and trace a function and some metrics.

Usage

Just add these two lines to the beginning of the function:

ctx, span := ft.Start(ctx, "package.Function", ft.WithErr(&err))
defer span.End()
func Do(ctx context.Context) (err error) {
	ctx, span := ft.Start(ctx, "main.Do", ft.WithErr(&err))
	defer span.End()
	
	err := callAnotherFn()
	if err != nil {
		return err
    }   

	return nil
}

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

time=2025-01-20T00:32:20.025+01:00 level=INFO msg="action started" action=main.Do
time=2025-01-20T00:32:20.330+01:00 level=ERROR msg="action ended" action=main.Do duration_ms=137.546 error="error from callAnotherFn"

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.

License

BSD Zero Clause License

Copyright (c) 2024 Amanbolat Balabekov

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func SetAppendOtelAttrs added in v0.2.0

func SetAppendOtelAttrs(v bool)

func SetClock added in v0.2.0

func SetClock(c clockwork.Clock)

func SetDefaultLogger

func SetDefaultLogger(l *slog.Logger)

func SetDurationMetricUnit added in v0.2.0

func SetDurationMetricUnit(unit string)

func SetLogLevelOnFailure added in v0.2.0

func SetLogLevelOnFailure(level slog.Level)

func SetLogLevelOnSuccess added in v0.2.0

func SetLogLevelOnSuccess(level slog.Level)

func SetMetricsEnabled added in v0.2.0

func SetMetricsEnabled(v bool)

func SetTracingEnabled added in v0.2.0

func SetTracingEnabled(v bool)

Types

type Option added in v0.2.0

type Option func(cfg *SpanConfig)

func WithAttrs added in v0.2.0

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

func WithErr added in v0.2.0

func WithErr(err *error) Option

type Span

type Span interface {
	End()
}

func Start added in v0.2.0

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

type SpanConfig added in v0.2.0

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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