ft

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: 0BSD Imports: 11 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 defer ft.Trace(ctx, "<method name>").WithError(&err).Log() to the beginning of the function.

func Do(ctx context.Context) (err error) {
	defer ft.Trace(ctx, "main.Do").WithError(&err).Log()
	err = errors.New("unexpected error")

	return
}

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

2024/01/18 01:05:07 INFO action started action=main.Do
2024/01/18 01:05:07 ERROR action ended action=main.Do duration=472.583µs error=unexpected error

Tracing

Add the following to the main function to enable tracing:

func main() {
    ft.EnableTracing()
}

Metrics

Add the following to the main function to enable metrics:

func main() {
    ft.EnableMetrics()
}

It uses go-metrics package under the hood.

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

This section is empty.

Variables

This section is empty.

Functions

func DefaultLogger

func DefaultLogger() *slog.Logger

func DisableMetrics

func DisableMetrics()

func DisableTracing

func DisableTracing()

func EnableMetrics

func EnableMetrics()

func EnableTracing

func EnableTracing()

func LogLevel

func LogLevel() slog.Level

func SetDefaultLogger

func SetDefaultLogger(l *slog.Logger)

func SetLogLevel

func SetLogLevel(level slog.Level)

func TracingEnabled

func TracingEnabled() bool

Types

type Span

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

func Trace

func Trace(ctx context.Context, action string) Span

func (Span) Log

func (s Span) Log()

func (Span) WithError

func (s Span) WithError(err *error) Span

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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