tracer

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: Apache-2.0 Imports: 6 Imported by: 6

Documentation

Overview

Package tracer starts an opencensus span with a logger automatically signed with the current calling function.

An additional method is provided to explicitly sign anonymous functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterPrefix

func RegisterPrefix(custom string)

RegisterPrefix sets a package level prefix at initialization time.

The default value is "function".

func StartNamedSpan

func StartNamedSpan(ctx context.Context, rt Loggable, signature string, fields ...zap.Field) (context.Context, *trace.Span, log.Logger)

StartNamedSpan is used inside anonymous functions. The caller may specify a signature.

Example
package main

import (
	"context"

	"github.com/fredbi/go-trace/log"
	"github.com/fredbi/go-trace/tracer"
	"go.uber.org/zap"
)

type Runtime struct {
	logger log.Factory
}

func (r Runtime) Logger() log.Factory {
	return r.logger
}

func main() {
	lg, _ := zap.NewDevelopment()
	rt := Runtime{logger: log.NewFactory(lg)}
	ctx := context.Background()

	// StartNamedSpan should be used in anonymous functions like so.
	handleFunc := func() {
		_, span, logger := tracer.StartNamedSpan(ctx, rt, "signature", zap.String("field", "fred"))
		defer span.End()

		logger.Info("test")
	}

	handleFunc()
}
Output:

func StartSpan

func StartSpan(ctx context.Context, rt Loggable, fields ...zap.Field) (context.Context, *trace.Span, log.Logger)

StartSpan returns an opencensus span and logger that prepends the caller's signature.

This spares us the boiler plate of repeatedly adding the prefix and function signatures in trace spans and logger.

Example
package main

import (
	"context"

	"github.com/fredbi/go-trace/log"
	"github.com/fredbi/go-trace/tracer"
	"go.uber.org/zap"
)

type Runtime struct {
	logger log.Factory
}

func (r Runtime) Logger() log.Factory {
	return r.logger
}

func main() {
	lg, _ := zap.NewProduction()
	rt := Runtime{logger: log.NewFactory(lg)}
	ctx := context.Background()

	// Instantiate a span and its associated span logger.
	//
	// This span is automatically signed with the current function, annotated with the source file and line
	_, span, logger := tracer.StartSpan(ctx, rt, zap.String("field", "fred"))
	defer span.End()

	logger.Info("test")

	// Should get something like:
	// 2023-11-01T17:19:58.615+0100	INFO	log/logger.go:35	test	{
	//	"function": "tracer.TestStartSpan",
	//	"source_file": ".../github.com/fredbi/go-trace/tracer/tracer_test.go",
	//	"source_line": 28,
	//	"field": "fred"
	//	}
}
Output:

Types

type Loggable

type Loggable interface {
	Logger() log.Factory
}

Loggable is a log factory provider

Jump to

Keyboard shortcuts

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