1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-05 00:28:58 +02:00

Golang opentelemetry-go draft API (incomplete) (#9)

* Work in progress from https://github.com/lightstep/opentelemetry-golang-prototype

* Renames

* Rename

* Finish rename

* Rename packages

* README
This commit is contained in:
Joshua MacDonald
2019-06-14 11:37:05 -07:00
committed by rghetia
parent 1429272864
commit e17f4468a6
43 changed files with 2888 additions and 0 deletions

144
api/trace/api.go Normal file
View File

@ -0,0 +1,144 @@
package trace
import (
"context"
"time"
"github.com/open-telemetry/opentelemetry-go/api/core"
"github.com/open-telemetry/opentelemetry-go/api/log"
"github.com/open-telemetry/opentelemetry-go/api/scope"
"github.com/open-telemetry/opentelemetry-go/api/stats"
"github.com/open-telemetry/opentelemetry-go/api/tag"
)
type (
Tracer interface {
Start(context.Context, string, ...Option) (context.Context, Span)
WithSpan(
ctx context.Context,
operation string,
body func(ctx context.Context) error,
) error
WithService(name string) Tracer
WithComponent(name string) Tracer
WithResources(res ...core.KeyValue) Tracer
// Note: see https://github.com/opentracing/opentracing-go/issues/127
Inject(context.Context, Span, Injector)
// ScopeID returns the resource scope of this tracer.
scope.Scope
}
Span interface {
scope.Mutable
log.Interface
stats.Interface
SetError(bool)
Tracer() Tracer
Finish()
}
Injector interface {
Inject(core.SpanContext, tag.Map)
}
Option struct {
attribute core.KeyValue
attributes []core.KeyValue
startTime time.Time
reference Reference
}
Reference struct {
core.SpanContext
RelationshipType
}
RelationshipType int
)
const (
ChildOfRelationship RelationshipType = iota
FollowsFromRelationship
)
func GlobalTracer() Tracer {
if t := global.Load(); t != nil {
return t.(Tracer)
}
return empty
}
func SetGlobalTracer(t Tracer) {
global.Store(t)
}
func Start(ctx context.Context, name string, opts ...Option) (context.Context, Span) {
return GlobalTracer().Start(ctx, name, opts...)
}
func Active(ctx context.Context) Span {
span, _ := scope.Active(ctx).(*span)
return span
}
func WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
return GlobalTracer().WithSpan(ctx, name, body)
}
func SetError(ctx context.Context, v bool) {
Active(ctx).SetError(v)
}
func Inject(ctx context.Context, injector Injector) {
span := Active(ctx)
if span == nil {
return
}
span.Tracer().Inject(ctx, span, injector)
}
func WithStartTime(t time.Time) Option {
return Option{
startTime: t,
}
}
func WithAttributes(attrs ...core.KeyValue) Option {
return Option{
attributes: attrs,
}
}
func WithAttribute(attr core.KeyValue) Option {
return Option{
attribute: attr,
}
}
func ChildOf(sc core.SpanContext) Option {
return Option{
reference: Reference{
SpanContext: sc,
RelationshipType: ChildOfRelationship,
},
}
}
func FollowsFrom(sc core.SpanContext) Option {
return Option{
reference: Reference{
SpanContext: sc,
RelationshipType: FollowsFromRelationship,
},
}
}