Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tracing code to trace package #1307

Merged
merged 2 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Changed

- Move the `go.opentelemetry.io/otel/api/trace` package into `go.opentelemetry.io/otel` with the following changes. (#1229)
- Move the `go.opentelemetry.io/otel/api/trace` package into `go.opentelemetry.io/otel/trace` with the following changes. (#1229) (#1307)
- `ID` has been renamed to `TraceID`.
- `IDFromHex` has been renamed to `TraceIDFromHex`.
- `ErrorOption` has been changed to an interface to conform with project design standards which included adding a `NewErrorConfig` function.
Expand Down
67 changes: 34 additions & 33 deletions bridge/opentracing/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ import (
"go.opentelemetry.io/otel/internal/trace/noop"
otelparent "go.opentelemetry.io/otel/internal/trace/parent"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/otel/bridge/opentracing/migration"
)

type bridgeSpanContext struct {
baggageItems baggage.Map
otelSpanContext otel.SpanContext
otelSpanContext trace.SpanContext
}

var _ ot.SpanContext = &bridgeSpanContext{}

func newBridgeSpanContext(otelSpanContext otel.SpanContext, parentOtSpanContext ot.SpanContext) *bridgeSpanContext {
func newBridgeSpanContext(otelSpanContext trace.SpanContext, parentOtSpanContext ot.SpanContext) *bridgeSpanContext {
bCtx := &bridgeSpanContext{
baggageItems: baggage.NewEmptyMap(),
otelSpanContext: otelSpanContext,
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *bridgeSpanContext) baggageItem(restrictedKey string) string {
}

type bridgeSpan struct {
otelSpan otel.Span
otelSpan trace.Span
ctx *bridgeSpanContext
tracer *BridgeTracer
skipDeferHook bool
Expand All @@ -84,7 +85,7 @@ type bridgeSpan struct {

var _ ot.Span = &bridgeSpan{}

func newBridgeSpan(otelSpan otel.Span, bridgeSC *bridgeSpanContext, tracer *BridgeTracer) *bridgeSpan {
func newBridgeSpan(otelSpan trace.Span, bridgeSC *bridgeSpanContext, tracer *BridgeTracer) *bridgeSpan {
return &bridgeSpan{
otelSpan: otelSpan,
ctx: bridgeSC,
Expand All @@ -99,10 +100,10 @@ func (s *bridgeSpan) Finish() {
}

func (s *bridgeSpan) FinishWithOptions(opts ot.FinishOptions) {
var otelOpts []otel.SpanOption
var otelOpts []trace.SpanOption

if !opts.FinishTime.IsZero() {
otelOpts = append(otelOpts, otel.WithTimestamp(opts.FinishTime))
otelOpts = append(otelOpts, trace.WithTimestamp(opts.FinishTime))
}
for _, record := range opts.LogRecords {
s.logRecord(record)
Expand All @@ -116,8 +117,8 @@ func (s *bridgeSpan) FinishWithOptions(opts ot.FinishOptions) {
func (s *bridgeSpan) logRecord(record ot.LogRecord) {
s.otelSpan.AddEvent(
"",
otel.WithTimestamp(record.Timestamp),
otel.WithAttributes(otLogFieldsToOTelLabels(record.Fields)...),
trace.WithTimestamp(record.Timestamp),
trace.WithAttributes(otLogFieldsToOTelLabels(record.Fields)...),
)
}

Expand Down Expand Up @@ -147,7 +148,7 @@ func (s *bridgeSpan) SetTag(key string, value interface{}) ot.Span {
func (s *bridgeSpan) LogFields(fields ...otlog.Field) {
s.otelSpan.AddEvent(
"",
otel.WithAttributes(otLogFieldsToOTelLabels(fields)...),
trace.WithAttributes(otLogFieldsToOTelLabels(fields)...),
)
}

Expand Down Expand Up @@ -265,13 +266,13 @@ func (s *bridgeSpan) Log(data ot.LogData) {

type bridgeSetTracer struct {
isSet bool
otelTracer otel.Tracer
otelTracer trace.Tracer

warningHandler BridgeWarningHandler
warnOnce sync.Once
}

func (s *bridgeSetTracer) tracer() otel.Tracer {
func (s *bridgeSetTracer) tracer() trace.Tracer {
if !s.isSet {
s.warnOnce.Do(func() {
s.warningHandler("The OpenTelemetry tracer is not set, default no-op tracer is used! Call SetOpenTelemetryTracer to set it up.\n")
Expand Down Expand Up @@ -323,7 +324,7 @@ func (t *BridgeTracer) SetWarningHandler(handler BridgeWarningHandler) {
// SetWarningHandler overrides the underlying OpenTelemetry
// tracer. The passed tracer should know how to operate in the
// environment that uses OpenTracing API.
func (t *BridgeTracer) SetOpenTelemetryTracer(tracer otel.Tracer) {
func (t *BridgeTracer) SetOpenTelemetryTracer(tracer trace.Tracer) {
t.setTracer.otelTracer = tracer
t.setTracer.isSet = true
}
Expand Down Expand Up @@ -394,16 +395,16 @@ func (t *BridgeTracer) StartSpan(operationName string, opts ...ot.StartSpanOptio
attributes, kind, hadTrueErrorTag := otTagsToOTelAttributesKindAndError(sso.Tags)
checkCtx := migration.WithDeferredSetup(context.Background())
if parentBridgeSC != nil {
checkCtx = otel.ContextWithRemoteSpanContext(checkCtx, parentBridgeSC.otelSpanContext)
checkCtx = trace.ContextWithRemoteSpanContext(checkCtx, parentBridgeSC.otelSpanContext)
}
checkCtx2, otelSpan := t.setTracer.tracer().Start(
checkCtx,
operationName,
otel.WithAttributes(attributes...),
otel.WithTimestamp(sso.StartTime),
otel.WithLinks(links...),
otel.WithRecord(),
otel.WithSpanKind(kind),
trace.WithAttributes(attributes...),
trace.WithTimestamp(sso.StartTime),
trace.WithLinks(links...),
trace.WithRecord(),
trace.WithSpanKind(kind),
)
if checkCtx != checkCtx2 {
t.warnOnce.Do(func() {
Expand Down Expand Up @@ -435,7 +436,7 @@ func (t *BridgeTracer) StartSpan(operationName string, opts ...ot.StartSpanOptio
// This function should be used by the OpenTelemetry tracers that want
// to be aware how to operate in the environment using OpenTracing
// API.
func (t *BridgeTracer) ContextWithBridgeSpan(ctx context.Context, span otel.Span) context.Context {
func (t *BridgeTracer) ContextWithBridgeSpan(ctx context.Context, span trace.Span) context.Context {
var otSpanContext ot.SpanContext
if parentSpan := ot.SpanFromContext(ctx); parentSpan != nil {
otSpanContext = parentSpan.Context()
Expand Down Expand Up @@ -465,8 +466,8 @@ func (t *BridgeTracer) ContextWithSpanHook(ctx context.Context, span ot.Span) co
return ctx
}

func otTagsToOTelAttributesKindAndError(tags map[string]interface{}) ([]label.KeyValue, otel.SpanKind, bool) {
kind := otel.SpanKindInternal
func otTagsToOTelAttributesKindAndError(tags map[string]interface{}) ([]label.KeyValue, trace.SpanKind, bool) {
kind := trace.SpanKindInternal
err := false
var pairs []label.KeyValue
for k, v := range tags {
Expand All @@ -475,13 +476,13 @@ func otTagsToOTelAttributesKindAndError(tags map[string]interface{}) ([]label.Ke
if s, ok := v.(string); ok {
switch strings.ToLower(s) {
case "client":
kind = otel.SpanKindClient
kind = trace.SpanKindClient
case "server":
kind = otel.SpanKindServer
kind = trace.SpanKindServer
case "producer":
kind = otel.SpanKindProducer
kind = trace.SpanKindProducer
case "consumer":
kind = otel.SpanKindConsumer
kind = trace.SpanKindConsumer
}
}
case string(otext.Error):
Expand Down Expand Up @@ -527,10 +528,10 @@ func otTagToOTelLabelKey(k string) label.Key {
return label.Key(k)
}

func otSpanReferencesToParentAndLinks(references []ot.SpanReference) (*bridgeSpanContext, []otel.Link) {
func otSpanReferencesToParentAndLinks(references []ot.SpanReference) (*bridgeSpanContext, []trace.Link) {
var (
parent *bridgeSpanContext
links []otel.Link
links []trace.Link
)
for _, reference := range references {
bridgeSC, ok := reference.ReferencedContext.(*bridgeSpanContext)
Expand All @@ -556,8 +557,8 @@ func otSpanReferencesToParentAndLinks(references []ot.SpanReference) (*bridgeSpa
return parent, links
}

func otSpanReferenceToOTelLink(bridgeSC *bridgeSpanContext, refType ot.SpanReferenceType) otel.Link {
return otel.Link{
func otSpanReferenceToOTelLink(bridgeSC *bridgeSpanContext, refType ot.SpanReferenceType) trace.Link {
return trace.Link{
SpanContext: bridgeSC.otelSpanContext,
Attributes: otSpanReferenceTypeToOTelLinkAttributes(refType),
}
Expand Down Expand Up @@ -586,11 +587,11 @@ func otSpanReferenceTypeToString(refType ot.SpanReferenceType) string {
// fakeSpan is just a holder of span context, nothing more. It's for
// propagators, so they can get the span context from Go context.
type fakeSpan struct {
otel.Span
sc otel.SpanContext
trace.Span
sc trace.SpanContext
}

func (s fakeSpan) SpanContext() otel.SpanContext {
func (s fakeSpan) SpanContext() trace.SpanContext {
return s.sc
}

Expand Down Expand Up @@ -618,7 +619,7 @@ func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier int
Span: noop.Span,
sc: bridgeSC.otelSpanContext,
}
ctx := otel.ContextWithSpan(context.Background(), fs)
ctx := trace.ContextWithSpan(context.Background(), fs)
ctx = baggage.ContextWithMap(ctx, bridgeSC.baggageItems)
t.getPropagator().Inject(ctx, header)
return nil
Expand Down
Loading