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-07-17 22:59:53 +02:00
|
|
|
package sdk
|
2019-06-14 20:37:05 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-03 20:03:51 +02:00
|
|
|
"time"
|
2019-06-14 20:37:05 +02:00
|
|
|
|
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-09-23 23:39:10 +02:00
|
|
|
"go.opentelemetry.io/api/trace"
|
|
|
|
"go.opentelemetry.io/experimental/streaming/exporter"
|
2019-06-14 20:37:05 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:34:57 +02:00
|
|
|
type span struct {
|
2019-07-17 22:59:53 +02:00
|
|
|
tracer *tracer
|
2019-09-23 23:39:10 +02:00
|
|
|
initial exporter.ScopeID
|
2019-06-27 22:34:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 19:44:46 +02:00
|
|
|
// SpancContext returns span context of the span. Return SpanContext is usable
|
|
|
|
// even after the span is finished.
|
|
|
|
func (sp *span) SpanContext() core.SpanContext {
|
2019-07-17 22:59:53 +02:00
|
|
|
return sp.initial.SpanContext
|
2019-06-19 19:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsRecordingEvents returns true is the span is active and recording events is enabled.
|
|
|
|
func (sp *span) IsRecordingEvents() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStatus sets the status of the span.
|
|
|
|
func (sp *span) SetStatus(status codes.Code) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
|
|
|
Type: exporter.SET_STATUS,
|
2019-07-17 22:59:53 +02:00
|
|
|
Scope: sp.ScopeID(),
|
|
|
|
Status: status,
|
2019-06-19 19:44:46 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (sp *span) ScopeID() exporter.ScopeID {
|
2019-07-17 22:59:53 +02:00
|
|
|
return sp.initial
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *span) SetAttribute(attribute core.KeyValue) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
|
|
|
Type: exporter.MODIFY_ATTR,
|
2019-07-17 22:59:53 +02:00
|
|
|
Scope: sp.ScopeID(),
|
2019-06-14 20:37:05 +02:00
|
|
|
Attribute: attribute,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *span) SetAttributes(attributes ...core.KeyValue) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
|
|
|
Type: exporter.MODIFY_ATTR,
|
2019-07-17 22:59:53 +02:00
|
|
|
Scope: sp.ScopeID(),
|
2019-06-14 20:37:05 +02:00
|
|
|
Attributes: attributes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-12 00:28:38 +02:00
|
|
|
func (sp *span) ModifyAttribute(mutator tag.Mutator) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
|
|
|
Type: exporter.MODIFY_ATTR,
|
2019-07-17 22:59:53 +02:00
|
|
|
Scope: sp.ScopeID(),
|
|
|
|
Mutator: mutator,
|
2019-06-14 20:37:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-12 00:28:38 +02:00
|
|
|
func (sp *span) ModifyAttributes(mutators ...tag.Mutator) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
|
|
|
Type: exporter.MODIFY_ATTR,
|
2019-07-17 22:59:53 +02:00
|
|
|
Scope: sp.ScopeID(),
|
2019-06-14 20:37:05 +02:00
|
|
|
Mutators: mutators,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (sp *span) Finish(options ...trace.FinishOption) {
|
2019-06-14 20:37:05 +02:00
|
|
|
recovered := recover()
|
2019-09-23 23:39:10 +02:00
|
|
|
opts := trace.FinishOptions{}
|
2019-09-03 20:03:51 +02:00
|
|
|
for _, opt := range options {
|
|
|
|
opt(&opts)
|
|
|
|
}
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
2019-09-03 20:03:51 +02:00
|
|
|
Time: opts.FinishTime,
|
2019-09-23 23:39:10 +02:00
|
|
|
Type: exporter.FINISH_SPAN,
|
2019-07-17 22:59:53 +02:00
|
|
|
Scope: sp.ScopeID(),
|
|
|
|
Recovered: recovered,
|
2019-06-14 20:37:05 +02:00
|
|
|
})
|
|
|
|
if recovered != nil {
|
|
|
|
panic(recovered)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (sp *span) Tracer() trace.Tracer {
|
2019-06-14 20:37:05 +02:00
|
|
|
return sp.tracer
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:03:51 +02:00
|
|
|
func (sp *span) AddEvent(ctx context.Context, msg string, attrs ...core.KeyValue) {
|
|
|
|
sp.addEventWithTime(ctx, time.Time{}, msg, attrs...)
|
2019-06-24 19:35:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-25 08:12:22 +02:00
|
|
|
func (sp *span) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) {
|
|
|
|
sp.addEventWithTime(ctx, timestamp, msg, attrs...)
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:03:51 +02:00
|
|
|
func (sp *span) addEventWithTime(ctx context.Context, timestamp time.Time, msg string, attrs ...core.KeyValue) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
2019-09-03 20:03:51 +02:00
|
|
|
Time: timestamp,
|
2019-09-23 23:39:10 +02:00
|
|
|
Type: exporter.ADD_EVENT,
|
2019-07-12 00:28:38 +02:00
|
|
|
String: msg,
|
|
|
|
Attributes: attrs,
|
|
|
|
Context: ctx,
|
|
|
|
})
|
2019-06-14 20:37:05 +02:00
|
|
|
}
|
2019-08-26 20:53:12 +02:00
|
|
|
|
|
|
|
func (sp *span) SetName(name string) {
|
2019-09-23 23:39:10 +02:00
|
|
|
sp.tracer.exporter.Record(exporter.Event{
|
|
|
|
Type: exporter.SET_NAME,
|
2019-08-26 20:53:12 +02:00
|
|
|
String: name,
|
|
|
|
})
|
|
|
|
}
|
2019-09-21 09:26:20 +02:00
|
|
|
|
2019-09-23 23:39:10 +02:00
|
|
|
func (sp *span) AddLink(link trace.Link) {
|
2019-09-21 09:26:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sp *span) Link(sc core.SpanContext, attrs ...core.KeyValue) {
|
|
|
|
}
|