| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal // import "go.opentelemetry.io/otel/bridge/opencensus/internal" |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
|
| 9 |
octrace "go.opencensus.io/trace" |
| 10 |
|
| 11 |
"go.opentelemetry.io/otel/attribute" |
| 12 |
"go.opentelemetry.io/otel/bridge/opencensus/internal/oc2otel" |
| 13 |
"go.opentelemetry.io/otel/bridge/opencensus/internal/otel2oc" |
| 14 |
"go.opentelemetry.io/otel/codes" |
| 15 |
"go.opentelemetry.io/otel/trace" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
// MessageSendEvent is the name of the message send event. |
| 20 |
MessageSendEvent = "message send" |
| 21 |
// MessageReceiveEvent is the name of the message receive event. |
| 22 |
MessageReceiveEvent = "message receive" |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
// UncompressedKey is used for the uncompressed byte size attribute. |
| 27 |
UncompressedKey = attribute.Key("uncompressed byte size") |
| 28 |
// CompressedKey is used for the compressed byte size attribute. |
| 29 |
CompressedKey = attribute.Key("compressed byte size") |
| 30 |
) |
| 31 |
|
| 32 |
// Span is an OpenCensus SpanInterface wrapper for an OpenTelemetry Span. |
| 33 |
type Span struct { |
| 34 |
otelSpan trace.Span |
| 35 |
} |
| 36 |
|
| 37 |
// NewSpan returns an OpenCensus Span wrapping an OpenTelemetry Span. |
| 38 |
func NewSpan(s trace.Span) *octrace.Span { |
| 39 |
return octrace.NewSpan(&Span{otelSpan: s}) |
| 40 |
} |
| 41 |
|
| 42 |
// IsRecordingEvents reports whether events are being recorded for this span. |
| 43 |
func (s *Span) IsRecordingEvents() bool { |
| 44 |
return s.otelSpan.IsRecording() |
| 45 |
} |
| 46 |
|
| 47 |
// End ends this span. |
| 48 |
func (s *Span) End() { |
| 49 |
s.otelSpan.End() |
| 50 |
} |
| 51 |
|
| 52 |
// SpanContext returns the SpanContext of this span. |
| 53 |
func (s *Span) SpanContext() octrace.SpanContext { |
| 54 |
return otel2oc.SpanContext(s.otelSpan.SpanContext()) |
| 55 |
} |
| 56 |
|
| 57 |
// SetName sets the name of this span, if it is recording events. |
| 58 |
func (s *Span) SetName(name string) { |
| 59 |
s.otelSpan.SetName(name) |
| 60 |
} |
| 61 |
|
| 62 |
// SetStatus sets the status of this span, if it is recording events. |
| 63 |
func (s *Span) SetStatus(status octrace.Status) { |
| 64 |
s.otelSpan.SetStatus(codes.Code(max(0, status.Code)), status.Message) // nolint:gosec // Overflow checked. |
| 65 |
} |
| 66 |
|
| 67 |
// AddAttributes sets attributes in this span. |
| 68 |
func (s *Span) AddAttributes(attributes ...octrace.Attribute) { |
| 69 |
s.otelSpan.SetAttributes(oc2otel.Attributes(attributes)...) |
| 70 |
} |
| 71 |
|
| 72 |
// Annotate adds an annotation with attributes to this span. |
| 73 |
func (s *Span) Annotate(attributes []octrace.Attribute, str string) { |
| 74 |
s.otelSpan.AddEvent(str, trace.WithAttributes(oc2otel.Attributes(attributes)...)) |
| 75 |
} |
| 76 |
|
| 77 |
// Annotatef adds a formatted annotation with attributes to this span. |
| 78 |
func (s *Span) Annotatef(attributes []octrace.Attribute, format string, a ...any) { |
| 79 |
s.Annotate(attributes, fmt.Sprintf(format, a...)) |
| 80 |
} |
| 81 |
|
| 82 |
// AddMessageSendEvent adds a message send event to this span. |
| 83 |
func (s *Span) AddMessageSendEvent(_, uncompressedByteSize, compressedByteSize int64) { |
| 84 |
s.otelSpan.AddEvent(MessageSendEvent, |
| 85 |
trace.WithAttributes( |
| 86 |
attribute.KeyValue{ |
| 87 |
Key: UncompressedKey, |
| 88 |
Value: attribute.Int64Value(uncompressedByteSize), |
| 89 |
}, |
| 90 |
attribute.KeyValue{ |
| 91 |
Key: CompressedKey, |
| 92 |
Value: attribute.Int64Value(compressedByteSize), |
| 93 |
}), |
| 94 |
) |
| 95 |
} |
| 96 |
|
| 97 |
// AddMessageReceiveEvent adds a message receive event to this span. |
| 98 |
func (s *Span) AddMessageReceiveEvent(_, uncompressedByteSize, compressedByteSize int64) { |
| 99 |
s.otelSpan.AddEvent(MessageReceiveEvent, |
| 100 |
trace.WithAttributes( |
| 101 |
attribute.KeyValue{ |
| 102 |
Key: UncompressedKey, |
| 103 |
Value: attribute.Int64Value(uncompressedByteSize), |
| 104 |
}, |
| 105 |
attribute.KeyValue{ |
| 106 |
Key: CompressedKey, |
| 107 |
Value: attribute.Int64Value(compressedByteSize), |
| 108 |
}), |
| 109 |
) |
| 110 |
} |
| 111 |
|
| 112 |
// AddLink adds a link to this span. |
| 113 |
// This drops the OpenCensus LinkType because there is no such concept in OpenTelemetry. |
| 114 |
func (s *Span) AddLink(l octrace.Link) { |
| 115 |
s.otelSpan.AddLink(trace.Link{ |
| 116 |
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{ |
| 117 |
TraceID: trace.TraceID(l.TraceID), |
| 118 |
SpanID: trace.SpanID(l.SpanID), |
| 119 |
// We don't know if this was sampled or not. |
| 120 |
// Mark it as sampled, since sampled means |
| 121 |
// "the caller may have recorded trace data": |
| 122 |
// https://www.w3.org/TR/trace-context/#sampled-flag |
| 123 |
TraceFlags: trace.FlagsSampled, |
| 124 |
}), |
| 125 |
Attributes: oc2otel.AttributesFromMap(l.Attributes), |
| 126 |
}) |
| 127 |
} |
| 128 |
|
| 129 |
// String prints a string representation of this span. |
| 130 |
func (s *Span) String() string { |
| 131 |
return "span " + s.otelSpan.SpanContext().SpanID().String() |
| 132 |
} |