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-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
type Provider interface {
|
2019-11-25 19:46:07 +02:00
|
|
|
// Tracer creates a named tracer that implements Tracer interface.
|
2019-10-22 22:19:11 +02:00
|
|
|
// If the name is an empty string then provider uses default name.
|
2019-11-25 19:46:07 +02:00
|
|
|
Tracer(name string) Tracer
|
2019-10-22 22:19:11 +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-12-04 23:41:13 +02:00
|
|
|
Start(ctx context.Context, spanName string, opts ...StartOption) (context.Context, Span)
|
2019-06-28 06:26:16 +02:00
|
|
|
|
2019-11-14 20:50:20 +02:00
|
|
|
// WithSpan wraps the execution of the fn function with a span.
|
|
|
|
// It starts a new span, sets it as an active span in the context,
|
|
|
|
// executes the fn function and closes the span before returning the result of fn.
|
2019-06-28 06:26:16 +02:00
|
|
|
WithSpan(
|
|
|
|
ctx context.Context,
|
2019-11-14 20:50:20 +02:00
|
|
|
spanName string,
|
|
|
|
fn func(ctx context.Context) error,
|
2019-06-28 06:26:16 +02:00
|
|
|
) error
|
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-12-04 23:41:13 +02:00
|
|
|
// EndConfig provides options to set properties of span at the time of ending
|
|
|
|
// the span.
|
|
|
|
type EndConfig struct {
|
2019-09-27 19:48:10 +02:00
|
|
|
EndTime time.Time
|
2019-09-03 20:03:51 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 23:41:13 +02:00
|
|
|
// EndOption applies changes to EndConfig that sets options when the span is ended.
|
|
|
|
type EndOption func(*EndConfig)
|
2019-09-03 20:03:51 +02:00
|
|
|
|
2019-12-04 23:41:13 +02:00
|
|
|
// WithEndTime sets the end time of the span to provided time t, when it is ended.
|
|
|
|
func WithEndTime(t time.Time) EndOption {
|
|
|
|
return func(c *EndConfig) {
|
|
|
|
c.EndTime = t
|
2019-09-03 20:03:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-27 19:48:10 +02:00
|
|
|
// End completes the span. No updates are allowed to span after it
|
|
|
|
// ends. The only exception is setting status of the span.
|
|
|
|
End(options ...EndOption)
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-06-28 06:26:16 +02:00
|
|
|
// AddEvent adds an event to the span.
|
2019-12-18 20:13:05 +02:00
|
|
|
AddEvent(ctx context.Context, name string, attrs ...core.KeyValue)
|
2019-09-25 08:12:22 +02:00
|
|
|
// AddEventWithTimestamp adds an event with a custom timestamp
|
|
|
|
// to the span.
|
2019-12-18 20:13:05 +02:00
|
|
|
AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...core.KeyValue)
|
2019-06-25 22:50:46 +02:00
|
|
|
|
2019-10-11 03:07:35 +02:00
|
|
|
// IsRecording returns true if the span is active and recording events is enabled.
|
|
|
|
IsRecording() bool
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-09-27 19:48:10 +02:00
|
|
|
// SpanContext returns span context of the span. Returned SpanContext is usable
|
|
|
|
// even after the span ends.
|
2019-06-28 06:26:16 +02:00
|
|
|
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
|
2019-09-27 19:48:10 +02:00
|
|
|
// even after span ends.
|
2019-06-28 06:26:16 +02:00
|
|
|
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
|
|
|
|
SetAttributes(...core.KeyValue)
|
2019-06-28 06:26:16 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
2019-12-04 23:41:13 +02:00
|
|
|
// StartOption applies changes to StartConfig that sets options at span start time.
|
|
|
|
type StartOption func(*StartConfig)
|
2019-06-19 19:44:46 +02:00
|
|
|
|
2019-12-04 23:41:13 +02:00
|
|
|
// StartConfig provides options to set properties of span at the time of starting
|
2019-06-28 06:26:16 +02:00
|
|
|
// a new span.
|
2019-12-04 23:41:13 +02:00
|
|
|
type StartConfig struct {
|
2019-10-11 03:07:35 +02:00
|
|
|
Attributes []core.KeyValue
|
|
|
|
StartTime time.Time
|
2019-11-04 15:03:40 +02:00
|
|
|
Links []Link
|
2019-10-11 03:07:35 +02:00
|
|
|
Record bool
|
2020-02-04 18:55:03 +02:00
|
|
|
NewRoot bool
|
2019-10-24 01:25:14 +02:00
|
|
|
SpanKind SpanKind
|
2019-06-28 06:26:16 +02:00
|
|
|
}
|
2019-06-14 20:37:05 +02:00
|
|
|
|
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-10-24 01:25:14 +02:00
|
|
|
// SpanKind represents the role of a Span inside a Trace. Often, this defines how a Span
|
|
|
|
// will be processed and visualized by various backends.
|
2019-11-05 02:05:43 +02:00
|
|
|
type SpanKind int
|
2019-10-24 01:25:14 +02:00
|
|
|
|
|
|
|
const (
|
2019-11-05 02:05:43 +02:00
|
|
|
// As a convenience, these match the proto definition, see
|
|
|
|
// opentelemetry/proto/trace/v1/trace.proto
|
|
|
|
//
|
|
|
|
// The unspecified value is not a valid `SpanKind`. Use
|
|
|
|
// `ValidateSpanKind()` to coerce a span kind to a valid
|
|
|
|
// value.
|
|
|
|
SpanKindUnspecified SpanKind = 0
|
|
|
|
SpanKindInternal SpanKind = 1
|
|
|
|
SpanKindServer SpanKind = 2
|
|
|
|
SpanKindClient SpanKind = 3
|
|
|
|
SpanKindProducer SpanKind = 4
|
|
|
|
SpanKindConsumer SpanKind = 5
|
2019-10-24 01:25:14 +02:00
|
|
|
)
|
|
|
|
|
2019-11-05 02:05:43 +02:00
|
|
|
// ValidateSpanKind returns a valid span kind value. This will coerce
|
|
|
|
// invalid values into the default value, SpanKindInternal.
|
|
|
|
func ValidateSpanKind(spanKind SpanKind) SpanKind {
|
|
|
|
switch spanKind {
|
|
|
|
case SpanKindInternal,
|
|
|
|
SpanKindServer,
|
|
|
|
SpanKindClient,
|
|
|
|
SpanKindProducer,
|
|
|
|
SpanKindConsumer:
|
|
|
|
// valid
|
|
|
|
return spanKind
|
|
|
|
default:
|
|
|
|
return SpanKindInternal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the specified name of the SpanKind in lower-case.
|
|
|
|
func (sk SpanKind) String() string {
|
|
|
|
switch sk {
|
|
|
|
case SpanKindInternal:
|
|
|
|
return "internal"
|
|
|
|
case SpanKindServer:
|
|
|
|
return "server"
|
|
|
|
case SpanKindClient:
|
|
|
|
return "client"
|
|
|
|
case SpanKindProducer:
|
|
|
|
return "producer"
|
|
|
|
case SpanKindConsumer:
|
|
|
|
return "consumer"
|
|
|
|
default:
|
|
|
|
return "unspecified"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 07:03:09 +02:00
|
|
|
// WithStartTime sets the start time of the span to provided time t, when it is started.
|
2019-10-24 01:25:14 +02:00
|
|
|
// In absence of this option, wall clock time is used as start time.
|
2019-06-27 07:03:09 +02:00
|
|
|
// This option is typically used when starting of the span is delayed.
|
2019-12-04 23:41:13 +02:00
|
|
|
func WithStartTime(t time.Time) StartOption {
|
|
|
|
return func(c *StartConfig) {
|
|
|
|
c.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-11-14 20:03:23 +02:00
|
|
|
// Multiple `WithAttributes` options appends the attributes preserving the order.
|
2019-12-04 23:41:13 +02:00
|
|
|
func WithAttributes(attrs ...core.KeyValue) StartOption {
|
|
|
|
return func(c *StartConfig) {
|
|
|
|
c.Attributes = append(c.Attributes, attrs...)
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 03:07:35 +02:00
|
|
|
// WithRecord specifies that the span should be recorded.
|
|
|
|
// Note that the implementation may still override this preference,
|
|
|
|
// e.g., if the span is a child in an unsampled trace.
|
2019-12-04 23:41:13 +02:00
|
|
|
func WithRecord() StartOption {
|
|
|
|
return func(c *StartConfig) {
|
|
|
|
c.Record = true
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 18:55:03 +02:00
|
|
|
// WithNewRoot specifies that the current span or remote span context
|
|
|
|
// in context passed to `Start` should be ignored when deciding about
|
|
|
|
// a parent, which effectively means creating a span with new trace
|
|
|
|
// ID. The current span and the remote span context may be added as
|
|
|
|
// links to the span by the implementation.
|
|
|
|
func WithNewRoot() StartOption {
|
2019-12-04 23:41:13 +02:00
|
|
|
return func(c *StartConfig) {
|
2020-02-04 18:55:03 +02:00
|
|
|
c.NewRoot = true
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-24 01:25:14 +02:00
|
|
|
|
2019-11-04 15:03:40 +02:00
|
|
|
// LinkedTo allows instantiating a Span with initial Links.
|
2019-12-04 23:41:13 +02:00
|
|
|
func LinkedTo(sc core.SpanContext, attrs ...core.KeyValue) StartOption {
|
|
|
|
return func(c *StartConfig) {
|
|
|
|
c.Links = append(c.Links, Link{sc, attrs})
|
2019-11-04 15:03:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 01:25:14 +02:00
|
|
|
// WithSpanKind specifies the role a Span on a Trace.
|
2019-12-04 23:41:13 +02:00
|
|
|
func WithSpanKind(sk SpanKind) StartOption {
|
|
|
|
return func(c *StartConfig) {
|
|
|
|
c.SpanKind = sk
|
2019-10-24 01:25:14 +02:00
|
|
|
}
|
|
|
|
}
|