okit/okit.go

83 lines
2.8 KiB
Go

// Copyright (C) 2022 The OKit Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
package okit
import (
"bufio"
"context"
"os"
)
// DefaultClient provides a default client implementation.
var DefaultClient = NewClient()
// DefaultFormat is used to format how data is written to stdout.
var DefaultFormat Format = TextFormat{}
// TODO: make this configurable
var sink = bufio.NewWriter(os.Stdout)
// ContextKey is a holder structure that allows data to be attached to contexts.
type ContextKey string
// SpanKey identifies a span on a given context.
var SpanKey = ContextKey("okit.span")
// With returns a client containing additional tags.
func With(tags ...Tag) Client {
return DefaultClient.WithCallerSkip(3).With(tags...)
}
// Emit emits an event with the provided tags.
func Emit(event string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Emit(event, tags...)
}
// Observe reports a metric value with the provided tags.
func Observe(metric string, value float64, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Observe(metric, value, tags...)
}
// Trace traces a function call. It implements distributed tracing and bookend events.
func Trace(ctxp *context.Context, tags ...Tag) ActiveTrace {
return DefaultClient.WithCallerSkip(3).Trace(ctxp, tags...)
}
// Debug writes a message at a debug level.
func Debug(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Debug(msg, tags...)
}
// Info writes a message at a informational level.
func Info(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Info(msg, tags...)
}
// Warn writes a message at a warning level.
func Warn(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Warn(msg, tags...)
}
// Error writes a message at a error level.
func Error(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Error(msg, tags...)
}