2019-06-14 22:09:41 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-14 20:37:05 +02:00
|
|
|
package trace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-06-19 19:44:46 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
2019-07-15 23:49:21 +02:00
|
|
|
"go.opentelemetry.io/api/core"
|
|
|
|
"go.opentelemetry.io/api/tag"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
type Tracer interface {
|
2019-07-12 00:28:38 +02:00
|
|
|
// Start a span.
|
2019-06-28 06:26:16 +02:00
|
|
|
Start(context.Context, string, ...SpanOption) (context.Context, Span)
|
|
|
|
|
|
|
|
// WithSpan wraps the execution of the function body with a span.
|
|
|
|
// It starts a new span and sets it as an active span in the context.
|
|
|
|
// It then executes the body. It closes the span before returning the execution result.
|
|
|
|
WithSpan(
|
|
|
|
ctx context.Context,
|
|
|
|
operation string,
|
|
|
|
body func(ctx context.Context) error,
|
|
|
|
) error
|
|
|
|
|
|
|
|
// TODO: Do we need WithService and WithComponent?
|
2019-07-12 00:28:38 +02:00
|
|
|
// TODO: Can we make these helpers (based on WithResources)?
|
2019-06-28 06:26:16 +02:00
|
|
|
WithService(name string) Tracer
|
|
|
|
WithComponent(name string) Tracer
|
|
|
|
|
|
|
|
// WithResources attaches resource attributes to the Tracer.
|
|
|
|
WithResources(res ...core.KeyValue) Tracer
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-09-03 20:03:51 +02:00
|
|
|
type FinishOptions struct {
|
|
|
|
FinishTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type FinishOption func(*FinishOptions)
|
|
|
|
|
|
|
|
func WithFinishTime(finishTime time.Time) FinishOption {
|
|
|
|
return func(opts *FinishOptions) {
|
|
|
|
opts.FinishTime = finishTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
type Span interface {
|
|
|
|
// Tracer returns tracer used to create this span. Tracer cannot be nil.
|
|
|
|
Tracer() Tracer
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// Finish completes the span. No updates are allowed to span after it
|
|
|
|
// finishes. The only exception is setting status of the span.
|
2019-09-03 20:03:51 +02:00
|
|
|
Finish(options ...FinishOption)
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// AddEvent adds an event to the span.
|
2019-09-03 20:03:51 +02:00
|
|
|
AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue)
|
2019-06-25 22:50:46 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// IsRecordingEvents returns true if the span is active and recording events is enabled.
|
|
|
|
IsRecordingEvents() bool
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-09-21 09:26:20 +02:00
|
|
|
// AddLink adds a link to the span.
|
|
|
|
AddLink(link Link)
|
|
|
|
|
|
|
|
// Link creates a link between this span and the other span specified by the SpanContext.
|
|
|
|
// It then adds the newly created Link to the span.
|
|
|
|
Link(sc core.SpanContext, attrs ...core.KeyValue)
|
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// SpancContext returns span context of the span. Return SpanContext is usable
|
|
|
|
// even after the span is finished.
|
|
|
|
SpanContext() core.SpanContext
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// SetStatus sets the status of the span. The status of the span can be updated
|
|
|
|
// even after span is finished.
|
|
|
|
SetStatus(codes.Code)
|
2019-07-12 00:28:38 +02:00
|
|
|
|
2019-08-26 20:53:12 +02:00
|
|
|
// SetName sets the name of the span.
|
|
|
|
SetName(name string)
|
|
|
|
|
2019-07-12 00:28:38 +02:00
|
|
|
// Set span attributes
|
|
|
|
SetAttribute(core.KeyValue)
|
|
|
|
SetAttributes(...core.KeyValue)
|
|
|
|
|
|
|
|
// Modify and delete span attributes
|
|
|
|
ModifyAttribute(tag.Mutator)
|
|
|
|
ModifyAttributes(...tag.Mutator)
|
2019-06-28 06:26:16 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// SpanOption apply changes to SpanOptions.
|
|
|
|
type SpanOption func(*SpanOptions)
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// SpanOptions provides options to set properties of span at the time of starting
|
|
|
|
// a new span.
|
|
|
|
type SpanOptions struct {
|
|
|
|
Attributes []core.KeyValue
|
|
|
|
StartTime time.Time
|
|
|
|
Reference Reference
|
|
|
|
RecordEvent bool
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// Reference is used to establish relationship between newly created span and the
|
|
|
|
// other span. The other span could be related as a parent or linked or any other
|
|
|
|
// future relationship type.
|
|
|
|
type Reference struct {
|
|
|
|
core.SpanContext
|
|
|
|
RelationshipType
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
type RelationshipType int
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-07-01 20:25:40 +02:00
|
|
|
const (
|
|
|
|
ChildOfRelationship RelationshipType = iota
|
|
|
|
FollowsFromRelationship
|
|
|
|
)
|
|
|
|
|
2019-09-21 09:26:20 +02:00
|
|
|
// Link is used to establish relationship between two spans within the same Trace or
|
|
|
|
// across different Traces. Few examples of Link usage.
|
|
|
|
// 1. Batch Processing: A batch of elements may contain elements associated with one
|
|
|
|
// or more traces/spans. Since there can only be one parent SpanContext, Link is
|
|
|
|
// used to keep reference to SpanContext of all elements in the batch.
|
|
|
|
// 2. Public Endpoint: A SpanContext in incoming client request on a public endpoint
|
|
|
|
// is untrusted from service provider perspective. In such case it is advisable to
|
|
|
|
// start a new trace with appropriate sampling decision.
|
|
|
|
// However, it is desirable to associate incoming SpanContext to new trace initiated
|
|
|
|
// on service provider side so two traces (from Client and from Service Provider) can
|
|
|
|
// be correlated.
|
|
|
|
type Link struct {
|
|
|
|
core.SpanContext
|
|
|
|
Attributes []core.KeyValue
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// Start starts a new span using registered global tracer.
|
2019-06-19 19:44:46 +02:00
|
|
|
func Start(ctx context.Context, name string, opts ...SpanOption) (context.Context, Span) {
|
2019-06-14 20:37:05 +02:00
|
|
|
return GlobalTracer().Start(ctx, name, opts...)
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// WithStartTime sets the start time of the span to provided time t, when it is started.
|
|
|
|
// In absensce of this option, wall clock time is used as start time.
|
|
|
|
// This option is typically used when starting of the span is delayed.
|
2019-06-19 19:44:46 +02:00
|
|
|
func WithStartTime(t time.Time) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
2019-06-27 07:03:09 +02:00
|
|
|
o.StartTime = t
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// WithAttributes sets attributes to span. These attributes provides additional
|
|
|
|
// data about the span.
|
2019-06-19 19:44:46 +02:00
|
|
|
func WithAttributes(attrs ...core.KeyValue) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
2019-06-27 07:03:09 +02:00
|
|
|
o.Attributes = attrs
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// WithRecordEvents enables recording of the events while the span is active.
|
|
|
|
// In the absence of this option, RecordEvent is set to false, disabling any recording of
|
|
|
|
// the events.
|
2019-06-19 19:44:46 +02:00
|
|
|
func WithRecordEvents() SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
2019-06-27 07:03:09 +02:00
|
|
|
o.RecordEvent = true
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// ChildOf. TODO: do we need this?.
|
2019-06-19 19:44:46 +02:00
|
|
|
func ChildOf(sc core.SpanContext) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
2019-06-27 07:03:09 +02:00
|
|
|
o.Reference = Reference{
|
2019-06-14 20:37:05 +02:00
|
|
|
SpanContext: sc,
|
|
|
|
RelationshipType: ChildOfRelationship,
|
2019-06-19 19:44:46 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// FollowsFrom. TODO: do we need this?.
|
2019-06-19 19:44:46 +02:00
|
|
|
func FollowsFrom(sc core.SpanContext) SpanOption {
|
|
|
|
return func(o *SpanOptions) {
|
2019-06-27 07:03:09 +02:00
|
|
|
o.Reference = Reference{
|
2019-06-14 20:37:05 +02:00
|
|
|
SpanContext: sc,
|
|
|
|
RelationshipType: FollowsFromRelationship,
|
2019-06-19 19:44:46 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|