okit/api_impl.go

79 lines
2.6 KiB
Go
Raw Normal View History

// Copyright (C) 2022 The OKit Authors
2022-11-01 16:21:43 +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
//
// 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
//
// 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 (
"bufio"
2022-11-01 16:21:43 +00:00
"context"
"os"
"github.com/golang/protobuf/jsonpb"
2022-11-01 16:21:43 +00:00
)
// DefaultClient provides a default client implementation.
var DefaultClient = NewClient()
2022-11-01 16:21:43 +00:00
// TODO: make this configurable
var sink = bufio.NewWriter(os.Stdout)
var marshaler = &jsonpb.Marshaler{}
2022-11-01 16:21:43 +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
// SpanKey identifies a span on a given context.
var SpanKey = ContextKey("okit.span")
2022-11-01 16:21:43 +00:00
// With returns a client containing additional tags.
2022-11-01 16:21:43 +00:00
func With(tags ...Tag) Client {
return DefaultClient.WithCallerSkip(3).With(tags...)
2022-11-01 16:21:43 +00:00
}
// Emit emits an event with the provided tags.
2022-11-01 16:21:43 +00:00
func Emit(event string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Emit(event, tags...)
2022-11-01 16:21:43 +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) {
DefaultClient.WithCallerSkip(3).Observe(metric, value, tags...)
2022-11-01 16:21:43 +00:00
}
// Trace traces a function call. It implements distributed tracing and bookend events.
2022-11-01 16:21:43 +00:00
func Trace(ctx context.Context, tags ...Tag) (context.Context, DoneFunc) {
return DefaultClient.WithCallerSkip(3).Trace(ctx, tags...)
2022-11-01 16:21:43 +00:00
}
func Debug(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Debug(msg, tags...)
2022-11-01 16:21:43 +00:00
}
func Info(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Info(msg, tags...)
2022-11-01 16:21:43 +00:00
}
func Warn(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Warn(msg, tags...)
2022-11-01 16:21:43 +00:00
}
func Error(msg string, tags ...Tag) {
DefaultClient.WithCallerSkip(3).Error(msg, tags...)
2022-11-01 16:21:43 +00:00
}