2020-11-12 20:51:52 +02:00
// Copyright The 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.
package opencensus
import (
"context"
"fmt"
octrace "go.opencensus.io/trace"
2020-11-16 19:30:54 +02:00
"go.opentelemetry.io/otel"
2021-02-18 19:59:37 +02:00
"go.opentelemetry.io/otel/attribute"
2020-11-16 20:35:25 +02:00
"go.opentelemetry.io/otel/bridge/opencensus/utils"
2020-11-12 20:51:52 +02:00
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
// NewTracer returns an implementation of the OpenCensus Tracer interface which
// uses OpenTelemetry APIs. Using this implementation of Tracer "upgrades"
// libraries that use OpenCensus to OpenTelemetry to facilitate a migration.
func NewTracer ( tracer trace . Tracer ) octrace . Tracer {
return & otelTracer { tracer : tracer }
}
type otelTracer struct {
tracer trace . Tracer
}
var _ octrace . Tracer = ( * otelTracer ) ( nil )
func ( o * otelTracer ) StartSpan ( ctx context . Context , name string , s ... octrace . StartOption ) ( context . Context , * octrace . Span ) {
ctx , sp := o . tracer . Start ( ctx , name , convertStartOptions ( s , name ) ... )
return ctx , octrace . NewSpan ( & span { otSpan : sp } )
}
func convertStartOptions ( optFns [ ] octrace . StartOption , name string ) [ ] trace . SpanOption {
var ocOpts octrace . StartOptions
for _ , fn := range optFns {
fn ( & ocOpts )
}
otOpts := [ ] trace . SpanOption { }
switch ocOpts . SpanKind {
case octrace . SpanKindClient :
otOpts = append ( otOpts , trace . WithSpanKind ( trace . SpanKindClient ) )
case octrace . SpanKindServer :
otOpts = append ( otOpts , trace . WithSpanKind ( trace . SpanKindServer ) )
case octrace . SpanKindUnspecified :
otOpts = append ( otOpts , trace . WithSpanKind ( trace . SpanKindUnspecified ) )
}
if ocOpts . Sampler != nil {
2020-11-16 19:30:54 +02:00
otel . Handle ( fmt . Errorf ( "ignoring custom sampler for span %q created by OpenCensus because OpenTelemetry does not support creating a span with a custom sampler" , name ) )
2020-11-12 20:51:52 +02:00
}
return otOpts
}
func ( o * otelTracer ) StartSpanWithRemoteParent ( ctx context . Context , name string , parent octrace . SpanContext , s ... octrace . StartOption ) ( context . Context , * octrace . Span ) {
// make sure span context is zero'd out so we use the remote parent
ctx = trace . ContextWithSpan ( ctx , nil )
2020-11-16 20:35:25 +02:00
ctx = trace . ContextWithRemoteSpanContext ( ctx , utils . OCSpanContextToOTel ( parent ) )
2020-11-12 20:51:52 +02:00
return o . StartSpan ( ctx , name , s ... )
}
func ( o * otelTracer ) FromContext ( ctx context . Context ) * octrace . Span {
otSpan := trace . SpanFromContext ( ctx )
return octrace . NewSpan ( & span { otSpan : otSpan } )
}
func ( o * otelTracer ) NewContext ( parent context . Context , s * octrace . Span ) context . Context {
if otSpan , ok := s . Internal ( ) . ( * span ) ; ok {
return trace . ContextWithSpan ( parent , otSpan . otSpan )
}
2020-11-16 19:30:54 +02:00
otel . Handle ( fmt . Errorf ( "unable to create context with span %q, since it was created using a different tracer" , s . String ( ) ) )
2020-11-12 20:51:52 +02:00
return parent
}
type span struct {
otSpan trace . Span
}
func ( s * span ) IsRecordingEvents ( ) bool {
return s . otSpan . IsRecording ( )
}
func ( s * span ) End ( ) {
s . otSpan . End ( )
}
func ( s * span ) SpanContext ( ) octrace . SpanContext {
2020-11-16 20:35:25 +02:00
return utils . OTelSpanContextToOC ( s . otSpan . SpanContext ( ) )
2020-11-12 20:51:52 +02:00
}
func ( s * span ) SetName ( name string ) {
s . otSpan . SetName ( name )
}
func ( s * span ) SetStatus ( status octrace . Status ) {
s . otSpan . SetStatus ( codes . Code ( status . Code ) , status . Message )
}
func ( s * span ) AddAttributes ( attributes ... octrace . Attribute ) {
s . otSpan . SetAttributes ( convertAttributes ( attributes ) ... )
}
2021-02-18 19:59:37 +02:00
func convertAttributes ( attributes [ ] octrace . Attribute ) [ ] attribute . KeyValue {
otAttributes := make ( [ ] attribute . KeyValue , len ( attributes ) )
2020-11-12 20:51:52 +02:00
for i , a := range attributes {
2021-02-18 19:59:37 +02:00
otAttributes [ i ] = attribute . KeyValue {
Key : attribute . Key ( a . Key ( ) ) ,
2020-11-12 20:51:52 +02:00
Value : convertValue ( a . Value ( ) ) ,
}
}
return otAttributes
}
2021-02-18 19:59:37 +02:00
func convertValue ( ocval interface { } ) attribute . Value {
2020-11-12 20:51:52 +02:00
switch v := ocval . ( type ) {
case bool :
2021-02-18 19:59:37 +02:00
return attribute . BoolValue ( v )
2020-11-12 20:51:52 +02:00
case int64 :
2021-02-18 19:59:37 +02:00
return attribute . Int64Value ( v )
2020-11-12 20:51:52 +02:00
case float64 :
2021-02-18 19:59:37 +02:00
return attribute . Float64Value ( v )
2020-11-12 20:51:52 +02:00
case string :
2021-02-18 19:59:37 +02:00
return attribute . StringValue ( v )
2020-11-12 20:51:52 +02:00
default :
2021-02-18 19:59:37 +02:00
return attribute . StringValue ( "unknown" )
2020-11-12 20:51:52 +02:00
}
}
func ( s * span ) Annotate ( attributes [ ] octrace . Attribute , str string ) {
s . otSpan . AddEvent ( str , trace . WithAttributes ( convertAttributes ( attributes ) ... ) )
}
func ( s * span ) Annotatef ( attributes [ ] octrace . Attribute , format string , a ... interface { } ) {
s . Annotate ( attributes , fmt . Sprintf ( format , a ... ) )
}
var (
2021-02-18 19:59:37 +02:00
uncompressedKey = attribute . Key ( "uncompressed byte size" )
compressedKey = attribute . Key ( "compressed byte size" )
2020-11-12 20:51:52 +02:00
)
func ( s * span ) AddMessageSendEvent ( messageID , uncompressedByteSize , compressedByteSize int64 ) {
s . otSpan . AddEvent ( "message send" ,
trace . WithAttributes (
2021-02-18 19:59:37 +02:00
attribute . KeyValue {
2020-11-12 20:51:52 +02:00
Key : uncompressedKey ,
2021-02-18 19:59:37 +02:00
Value : attribute . Int64Value ( uncompressedByteSize ) ,
2020-11-12 20:51:52 +02:00
} ,
2021-02-18 19:59:37 +02:00
attribute . KeyValue {
2020-11-12 20:51:52 +02:00
Key : compressedKey ,
2021-02-18 19:59:37 +02:00
Value : attribute . Int64Value ( compressedByteSize ) ,
2020-11-12 20:51:52 +02:00
} ) ,
)
}
func ( s * span ) AddMessageReceiveEvent ( messageID , uncompressedByteSize , compressedByteSize int64 ) {
s . otSpan . AddEvent ( "message receive" ,
trace . WithAttributes (
2021-02-18 19:59:37 +02:00
attribute . KeyValue {
2020-11-12 20:51:52 +02:00
Key : uncompressedKey ,
2021-02-18 19:59:37 +02:00
Value : attribute . Int64Value ( uncompressedByteSize ) ,
2020-11-12 20:51:52 +02:00
} ,
2021-02-18 19:59:37 +02:00
attribute . KeyValue {
2020-11-12 20:51:52 +02:00
Key : compressedKey ,
2021-02-18 19:59:37 +02:00
Value : attribute . Int64Value ( compressedByteSize ) ,
2020-11-12 20:51:52 +02:00
} ) ,
)
}
func ( s * span ) AddLink ( l octrace . Link ) {
2020-11-16 19:30:54 +02:00
otel . Handle ( fmt . Errorf ( "ignoring OpenCensus link %+v for span %q because OpenTelemetry doesn't support setting links after creation" , l , s . String ( ) ) )
2020-11-12 20:51:52 +02:00
}
func ( s * span ) String ( ) string {
return fmt . Sprintf ( "span %s" , s . otSpan . SpanContext ( ) . SpanID . String ( ) )
}