klogga

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: MIT Imports: 20 Imported by: 0

README

Introduction

Opinionated logging-audit-tracing library. Data collected via klogga can be configured to be exported to different sources, including traditional text logs, but with emphasis on structured storages, primarily time-series databases and Open Telemetry.

Getting Started

import klogga configure your root tracer, don't forget to use batcher for buffered traces see examples/tracers_tree.go

Documentation

Overview

Package klogga is a generated GoMock package.

Package klogga is a generated GoMock package.

Index

Constants

View Source
const SpanIDSize = 8
View Source
const TimestampLayout = "2006-01-02 15:04:05.000"
View Source
const TraceIDSize = 16

Variables

This section is empty.

Functions

func InitHostname

func InitHostname()

InitHostname call this in your app initialization if you want to have host name set automatically on span start

func RoundDur

func RoundDur(d time.Duration) string

RoundDur converts duration to rounded a string value to be used in the index

func SetHostname

func SetHostname(hn string)

SetHostname set custom hostname that will appear on the span from the start

Types

type ComponentName

type ComponentName string

func (ComponentName) String

func (c ComponentName) String() string

type Enricher

type Enricher interface {
	Enrich(span *Span) *Span
}

type Exporter

type Exporter interface {
	Write(ctx context.Context, spans []*Span) error
	Shutdown(ctx context.Context) error
}

Exporter generic tracer interface, should not be used outside implementations to be more generic accepts batches right away

type ExportersSlice

type ExportersSlice []Exporter

func (ExportersSlice) Shutdown

func (t ExportersSlice) Shutdown(ctx context.Context) error

func (ExportersSlice) Write

func (t ExportersSlice) Write(ctx context.Context, spans []*Span) error

type Factory

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

Factory combines different exporters constructs tracers with proper names

func NewFactory

func NewFactory(exporters ...Exporter) *Factory

func (*Factory) AddExporter

func (tf *Factory) AddExporter(exporter Exporter) *Factory

AddExporter adds another exporter to the factory. All previously created tracers as well as new tracers will write to all exporters. Do not use for concurrently executing goroutines that write spans. Intended to be used in the sequential app initialization.

func (*Factory) Named

func (tf *Factory) Named(componentName ComponentName) Tracer

Named creates a named tracer for specified component

func (*Factory) NamedPkg

func (tf *Factory) NamedPkg() Tracer

NamedPkg creates a named tracer with the name as package name, where this constructor is called

func (*Factory) Shutdown

func (tf *Factory) Shutdown(ctx context.Context) error

type LogLevel

type LogLevel int

LogLevel log levels simplify compatibility with some logging systems

const (
	Info  LogLevel = 0
	Warn  LogLevel = 1
	Error LogLevel = 2
	Fatal LogLevel = 3
)

func (LogLevel) String

func (l LogLevel) String() string

type MockExporter

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

MockExporter is a mock of Exporter interface.

func NewMockExporter

func NewMockExporter(ctrl *gomock.Controller) *MockExporter

NewMockExporter creates a new mock instance.

func (*MockExporter) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockExporter) Shutdown

func (m *MockExporter) Shutdown(ctx context.Context) error

Shutdown mocks base method.

func (*MockExporter) Write

func (m *MockExporter) Write(ctx context.Context, spans []*Span) error

Write mocks base method.

type MockExporterMockRecorder

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

MockExporterMockRecorder is the mock recorder for MockExporter.

func (*MockExporterMockRecorder) Shutdown

func (mr *MockExporterMockRecorder) Shutdown(ctx interface{}) *gomock.Call

Shutdown indicates an expected call of Shutdown.

func (*MockExporterMockRecorder) Write

func (mr *MockExporterMockRecorder) Write(ctx, spans interface{}) *gomock.Call

Write indicates an expected call of Write.

type MockTracer

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

MockTracer is a mock of Tracer interface.

func NewMockTracer

func NewMockTracer(ctrl *gomock.Controller) *MockTracer

NewMockTracer creates a new mock instance.

func (*MockTracer) EXPECT

func (m *MockTracer) EXPECT() *MockTracerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockTracer) Finish

func (m *MockTracer) Finish(span *Span)

Finish mocks base method.

func (*MockTracer) Name

func (m *MockTracer) Name() ComponentName

Name mocks base method.

type MockTracerMockRecorder

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

MockTracerMockRecorder is the mock recorder for MockTracer.

func (*MockTracerMockRecorder) Finish

func (mr *MockTracerMockRecorder) Finish(span interface{}) *gomock.Call

Finish indicates an expected call of Finish.

func (*MockTracerMockRecorder) Name

func (mr *MockTracerMockRecorder) Name() *gomock.Call

Name indicates an expected call of Name.

type NilExporterTracer

type NilExporterTracer struct{}

func (NilExporterTracer) Finish

func (NilExporterTracer) Finish(*Span)

func (NilExporterTracer) Name

func (NilExporterTracer) Shutdown

func (NilExporterTracer) Write

type ObjectVal

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

func ValJson

func ValJson(v string) *ObjectVal

ValJson Use to directly inform klogga that is value is a valid json and no conversion is needed

func ValObject

func ValObject(obj interface{}) *ObjectVal

ValObject explicitly indicates that the underlying value is a nested object, to be stored in a complex field like jsonb

func (ObjectVal) MarshalJSON

func (o ObjectVal) MarshalJSON() ([]byte, error)

func (ObjectVal) String

func (o ObjectVal) String() string

type Span

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

Span describes a structured unit of log (tracing) with an interval Span is a (TODO) serializable and independent of the way it is exported (traced) to any storage

func CreateErrSpanFrom

func CreateErrSpanFrom(ctx context.Context, span *Span) *Span

CreateErrSpanFrom creates span describing an error in a flat way

func CtxActiveSpan

func CtxActiveSpan(ctx context.Context) *Span

CtxActiveSpan returns current active span from context (for new spans, current span is a parent span) careful no to Write this span, parent call should do it

func Message

func Message(message string, opts ...SpanOption) *Span

Message is the simplest way to start a span, in the shortest way possible it doesn't use context, and doesn't return one. It is strongly discouraged to use Message unless for testing purposes.

func Start

func Start(ctx1 context.Context, opts ...SpanOption) (span *Span, ctx context.Context)

Start preferred way to start a new span, automatically sets basic span fields like class, name, host

func StartFromParentID

func StartFromParentID(ctx context.Context, parentSpanID SpanID, traceID TraceID) (*Span, context.Context)

StartFromParentID starts new span with externally defined parent span ID Deprecated: use SpanOptions

func StartLeaf

func StartLeaf(ctx context.Context, opts ...SpanOption) (span *Span)

StartLeaf start new span without returning resulting context i.e. no child spans possibility

func (*Span) Class

func (s *Span) Class() string

func (*Span) Component

func (s *Span) Component() ComponentName

func (*Span) DeferErr

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

DeferErr adds defer errors to span. Not the same as Err!

func (*Span) DeferErrs

func (s *Span) DeferErrs() error

func (*Span) Duration

func (s *Span) Duration() time.Duration

Duration returns current duration for running span returns total duration for stopped span

func (*Span) EWState

func (s *Span) EWState() string

func (*Span) EnrichFrom

func (s *Span) EnrichFrom(e Enricher) *Span

func (*Span) Err

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

Err adds error to the span, subsequent call combined errors

func (*Span) ErrRecover

func (s *Span) ErrRecover(rec interface{}, stackBytes []byte) *Span

ErrRecover convenience method to be used with recover() calls

func (*Span) ErrSpan

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

ErrSpan Convenience method for Err that returns the Span, for chaining

func (*Span) ErrVoid

func (s *Span) ErrVoid(err error)

ErrVoid convenience method to Err

func (*Span) ErrWrapf

func (s *Span) ErrWrapf(err error, format string, args ...interface{}) error

ErrWrapf shorthand for errors wrap

func (*Span) Errs

func (s *Span) Errs() error

func (*Span) FlushTo

func (s *Span) FlushTo(trs Tracer)

FlushTo accept tracer and call trs.Finish, shorthand for chaining

func (*Span) GlobalTag

func (s *Span) GlobalTag(key string, value interface{}) *Span

GlobalTag set the tag that is also propagated to all child spans not thread safe

func (*Span) HasDeferErr

func (s *Span) HasDeferErr() bool

func (*Span) HasErr

func (s *Span) HasErr() bool

func (*Span) HasWarn

func (s *Span) HasWarn() bool

func (*Span) Host

func (s *Span) Host() string

func (*Span) ID

func (s *Span) ID() SpanID

func (*Span) IsFinished

func (s *Span) IsFinished() bool

func (*Span) Json

func (s *Span) Json() ([]byte, error)

func (*Span) Level

func (s *Span) Level(level LogLevel) *Span

Level for compatibility with some logging systems overridden by errors and warns in the Span

func (*Span) Message

func (s *Span) Message(message string) *Span

Message shorthand for generic Val("message", ... ) value overwrites previous message usage of specific tags and values is preferred!

func (*Span) Name

func (s *Span) Name() string

func (*Span) OverrideName

func (s *Span) OverrideName(newName string) *Span

func (*Span) Package

func (s *Span) Package() string

func (*Span) PackageClass

func (s *Span) PackageClass() string

func (*Span) Parent

func (s *Span) Parent() *Span

func (*Span) ParentID

func (s *Span) ParentID() SpanID

func (*Span) SetComponent

func (s *Span) SetComponent(name ComponentName)

SetComponent should be used only in custom and special cases NamedTracer should decide the component name for the span

func (*Span) Stack

func (s *Span) Stack() []*Span

Stack get all parent spans

func (*Span) StartedTs

func (s *Span) StartedTs() time.Time

func (*Span) Stop

func (s *Span) Stop()

func (*Span) Stringify

func (s *Span) Stringify(endWith ...string) string

Stringify full span data string to be used in text tracers deliberately ignores host field

func (*Span) Tag

func (s *Span) Tag(key string, value interface{}) *Span

Tag not thread safe

func (*Span) Tags

func (s *Span) Tags() map[string]interface{}

Tags get a copy of span tags

func (*Span) TraceID

func (s *Span) TraceID() TraceID

func (*Span) Val

func (s *Span) Val(key string, value interface{}) *Span

Val not thread safe

func (*Span) ValAsJson

func (s *Span) ValAsJson(key string, value string) *Span

ValAsJson shorthand for .Val(key, klogga.ValJson(value))

func (*Span) ValAsObj

func (s *Span) ValAsObj(key string, value interface{}) *Span

ValAsObj shorthand for .Val(key, klogga.ValObject(value))

func (*Span) Vals

func (s *Span) Vals() map[string]interface{}

Vals get a copy of span vals

func (*Span) Warn

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

func (*Span) WarnWith

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

func (*Span) Warns

func (s *Span) Warns() error

type SpanID

type SpanID [8]byte

SpanID like in OpenTelemetry

func NewSpanID

func NewSpanID() (res SpanID)

func ParseSpanID

func ParseSpanID(spanID string) (res SpanID, err error)

func SpanIDFromBytes

func SpanIDFromBytes(bb []byte) (res SpanID, err error)

func SpanIDFromBytesOrZero

func SpanIDFromBytesOrZero(bb []byte) SpanID

func SpanIDFromString

func SpanIDFromString(s string) (res SpanID, err error)

func (SpanID) AsNullableBytes

func (s SpanID) AsNullableBytes() []byte

func (SpanID) Bytes

func (s SpanID) Bytes() []byte

func (SpanID) IsZero

func (s SpanID) IsZero() bool

func (SpanID) MarshalJSON

func (s SpanID) MarshalJSON() ([]byte, error)

func (SpanID) MarshalText

func (s SpanID) MarshalText() ([]byte, error)

func (SpanID) String

func (s SpanID) String() string

default string format for SpanID is base64!

func (*SpanID) UnmarshalJSON

func (s *SpanID) UnmarshalJSON(bb []byte) error

type SpanOption

type SpanOption interface {
	// contains filtered or unexported methods
}

func WithDone

func WithDone(ts time.Time, duration time.Duration) SpanOption

WithDone make already finished span

func WithName

func WithName(name string) SpanOption

func WithPackageClass

func WithPackageClass(p, c string) SpanOption

WithPackageClass overrides reflection-retrieved package and class

func WithParentSpanID

func WithParentSpanID(parentSpanID SpanID) SpanOption

func WithTimestamp

func WithTimestamp(ts time.Time) SpanOption

WithTimestamp make span to have started with custom timestamp to create a done span, use WithDone

func WithTraceID

func WithTraceID(traceID TraceID) SpanOption

type SpanSlice

type SpanSlice []*Span

SpanSlice shorthand for exporters input

type StringAsJSONVal

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

func (StringAsJSONVal) MarshalJSON

func (o StringAsJSONVal) MarshalJSON() ([]byte, error)

func (StringAsJSONVal) String

func (o StringAsJSONVal) String() string

type TraceID

type TraceID uuid.UUID

func NewTraceID

func NewTraceID() TraceID

func TraceIDFromBytes

func TraceIDFromBytes(bb []byte) (TraceID, error)

func TraceIDFromBytesOrZero

func TraceIDFromBytesOrZero(bb []byte) TraceID

func TraceIDFromString

func TraceIDFromString(traceID string) (res TraceID, err error)

func (TraceID) AsNullableUUID

func (t TraceID) AsNullableUUID() *uuid.UUID

func (TraceID) AsUUID

func (t TraceID) AsUUID() uuid.UUID

func (TraceID) Bytes

func (t TraceID) Bytes() []byte

func (TraceID) IsZero

func (t TraceID) IsZero() bool

func (TraceID) MarshalJSON

func (t TraceID) MarshalJSON() ([]byte, error)

func (TraceID) MarshalText

func (t TraceID) MarshalText() ([]byte, error)

func (TraceID) String

func (t TraceID) String() string

default storage and string format for TraceID is base64!

func (*TraceID) UnmarshalJSON

func (t *TraceID) UnmarshalJSON(bb []byte) error

type Tracer

type Tracer interface {
	Finish(span *Span)
	Name() ComponentName
}

func NewTestErrTracker

func NewTestErrTracker(t *testing.T, trs Tracer) Tracer

NewTestErrTracker wraps tracer to fail test on the span error.

type TracerProvider

type TracerProvider interface {
	NamedPkg() Tracer
	Named(componentName ComponentName) Tracer
}

TracerProvider use to allow components/adapters to have name overrides implemented by Factory

type WriterExporter

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

func NewWriterExporter

func NewWriterExporter(writer io.Writer) *WriterExporter

func (WriterExporter) Shutdown

func (w WriterExporter) Shutdown(context.Context) error

func (WriterExporter) Write

func (w WriterExporter) Write(ctx context.Context, spans []*Span) error

type WriterTracer

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

WriterTracer adapts tracer to a Writer interface

func NewWriterTracer

func NewWriterTracer(trs Tracer) *WriterTracer

func (WriterTracer) Printf

func (t WriterTracer) Printf(ctx context.Context, format string, v ...interface{})

Printf for compatibility with new redis and other logging systems

func (WriterTracer) Write

func (t WriterTracer) Write(p []byte) (n int, err error)

Directories

Path Synopsis
adapters
fx
exporters
influxdb18
Package influx_exporter is a generated GoMock package.
Package influx_exporter is a generated GoMock package.
util

Jump to

Keyboard shortcuts

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