Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Provide accessor to the span implementation #1240

Merged
merged 1 commit into from
Nov 2, 2020
Merged
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
5 changes: 5 additions & 0 deletions trace/trace_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ type Span struct {
internal SpanInterface
}

// Internal returns the underlying implementation of the Span
func (s *Span) Internal() SpanInterface {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why is this necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed when attempting to add an OpenCensus span to the OpenTelemetry context using otel.ContextWithSpan(context.Context, otel.Span) In order to do that, we need to be able to get the underlying Span implementation. The full implemnetation would look something like:

// This is the OpenCensus FromContext function, implemented using opentelemetry's SpanFromContext
func (o *otelTracer) FromContext(ctx context.Context) *octrace.Span {
	otSpan := otel.SpanFromContext(ctx)
	return // convert the opentelemetry span into the OC span implementation
}

// This is the OpenCensus NewContext function, implemented using opentelemetry's ContextWithSpan
func (o *otelTracer) NewContext(parent context.Context, s *octrace.Span) context.Context {
        // Below, I need to call s.Internal() in order to have an interface I can cast to my internal type.
        // For example, my internal type could wrap an opentelemetry Span, which I could use in ContextWithSpan
	if myspan, ok := s.Internal().(*myspanimplementation); ok {
		return otel.ContextWithSpan(parent, // get an opentelemetry Span from myspan)
	}
	// The user must have created the octrace Span using a different tracer, and we don't know how to store it.
	return parent
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see that makes sense.

return s.internal
}

// IsRecordingEvents returns true if events are being recorded for this span.
// Use this check to avoid computing expensive annotations when they will never
// be used.
Expand Down