2022-11-02 20:16:48 +00:00
|
|
|
// Copyright (C) 2022 The OKit Authors
|
2022-11-01 16:21:43 +00:00
|
|
|
//
|
2022-11-02 20:16:48 +00:00
|
|
|
// 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:
|
2022-11-01 16:21:43 +00:00
|
|
|
//
|
2022-11-02 20:16:48 +00:00
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
2022-11-01 16:21:43 +00:00
|
|
|
//
|
2022-11-02 20:16:48 +00:00
|
|
|
// 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.
|
2022-11-01 16:21:43 +00:00
|
|
|
|
|
|
|
package okit
|
|
|
|
|
|
|
|
import (
|
2022-11-02 20:16:48 +00:00
|
|
|
"bufio"
|
2022-11-01 16:21:43 +00:00
|
|
|
"context"
|
2022-11-02 20:16:48 +00:00
|
|
|
"os"
|
2022-11-01 16:21:43 +00:00
|
|
|
)
|
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// DefaultClient provides a default client implementation.
|
|
|
|
var DefaultClient = NewClient()
|
2022-11-01 16:21:43 +00:00
|
|
|
|
2022-11-06 17:59:39 +00:00
|
|
|
// DefaultFormat is used to format how data is written to stdout.
|
|
|
|
var DefaultFormat Format = TextFormat{}
|
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// TODO: make this configurable
|
|
|
|
var sink = bufio.NewWriter(os.Stdout)
|
2022-11-01 16:21:43 +00:00
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// ContextKey is a holder structure that allows data to be attached to contexts.
|
|
|
|
type ContextKey string
|
2022-11-01 16:21:43 +00:00
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// SpanKey identifies a span on a given context.
|
|
|
|
var SpanKey = ContextKey("okit.span")
|
2022-11-01 16:21:43 +00:00
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// With returns a client containing additional tags.
|
2022-11-01 16:21:43 +00:00
|
|
|
func With(tags ...Tag) Client {
|
2022-11-02 20:16:48 +00:00
|
|
|
return DefaultClient.WithCallerSkip(3).With(tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// Emit emits an event with the provided tags.
|
2022-11-01 16:21:43 +00:00
|
|
|
func Emit(event string, tags ...Tag) {
|
2022-11-02 20:16:48 +00:00
|
|
|
DefaultClient.WithCallerSkip(3).Emit(event, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// Observe reports a metric value with the provided tags.
|
2022-11-01 16:21:43 +00:00
|
|
|
func Observe(metric string, value float64, tags ...Tag) {
|
2022-11-02 20:16:48 +00:00
|
|
|
DefaultClient.WithCallerSkip(3).Observe(metric, value, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 20:16:48 +00:00
|
|
|
// Trace traces a function call. It implements distributed tracing and bookend events.
|
2022-12-14 16:23:55 +00:00
|
|
|
func Trace(ctxp *context.Context, tags ...Tag) ActiveTrace {
|
|
|
|
return DefaultClient.WithCallerSkip(3).Trace(ctxp, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 16:23:55 +00:00
|
|
|
// Debug writes a message at a debug level.
|
2022-11-01 16:21:43 +00:00
|
|
|
func Debug(msg string, tags ...Tag) {
|
2022-11-02 20:16:48 +00:00
|
|
|
DefaultClient.WithCallerSkip(3).Debug(msg, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 16:23:55 +00:00
|
|
|
// Info writes a message at a informational level.
|
2022-11-01 16:21:43 +00:00
|
|
|
func Info(msg string, tags ...Tag) {
|
2022-11-02 20:16:48 +00:00
|
|
|
DefaultClient.WithCallerSkip(3).Info(msg, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 16:23:55 +00:00
|
|
|
// Warn writes a message at a warning level.
|
2022-11-01 16:21:43 +00:00
|
|
|
func Warn(msg string, tags ...Tag) {
|
2022-11-02 20:16:48 +00:00
|
|
|
DefaultClient.WithCallerSkip(3).Warn(msg, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 16:23:55 +00:00
|
|
|
// Error writes a message at a error level.
|
2022-11-01 16:21:43 +00:00
|
|
|
func Error(msg string, tags ...Tag) {
|
2022-11-02 20:16:48 +00:00
|
|
|
DefaultClient.WithCallerSkip(3).Error(msg, tags...)
|
2022-11-01 16:21:43 +00:00
|
|
|
}
|