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

Allow retrieving span created in agent from otel, and store agent span in context when creating it with otel #1450

Merged
merged 3 commits into from
Jun 2, 2023
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: 2 additions & 0 deletions module/apmotel/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (t *tracer) Start(ctx context.Context, spanName string, opts ...trace.SpanS
Parent: tc,
Start: startTime,
})
ctx = apm.ContextWithSpan(ctx, s.span)
Copy link
Member

Choose a reason for hiding this comment

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

Rather than adding two span objects to context, should we be setting apm.OverrideSpanFromContext and apm.TransactionFromContext? The functions we set would look for a trace.Span in the context, type-asserting to *apmotel.span and extracting the transaction or span within.

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem with that is that if, someone is using both this module and the apmot one (it's unlikely I know), one will override the other.
I did it this way to prevent the module frome having to be exclusive with other ones using this wrapper.

Copy link
Member

Choose a reason for hiding this comment

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

OK. TBH I don't really care about that since OpenTracing is extremely deprecated, but your approach is fine. Happy to leave it as it is. Perhaps we should drop apmot in the next major version.

Copy link
Member Author

Choose a reason for hiding this comment

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

Another reason is that we have to care less about internals of the agent. We just let do its own stuff, and don't need to setup a context wrapper.

s.tx = parent.tx
s.spanContext = trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID(s.span.TraceContext().Trace),
Expand All @@ -105,6 +106,7 @@ func (t *tracer) Start(ctx context.Context, spanName string, opts ...trace.SpanS
}
}
s.tx = t.provider.apmTracer.StartTransactionOptions(spanName, "", tranOpts)
ctx = apm.ContextWithTransaction(ctx, s.tx)
s.spanContext = trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID(s.tx.TraceContext().Trace),
SpanID: trace.SpanID(s.tx.TraceContext().Span),
Expand Down
7 changes: 6 additions & 1 deletion module/apmotel/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestTracerStartTransaction(t *testing.T) {

assert.True(t, trace.SpanContextFromContext(ctx).IsValid())
assert.True(t, trace.SpanContextFromContext(ctx).IsSampled())

assert.True(t, apm.TransactionFromContext(ctx).Sampled())
assert.Nil(t, apm.SpanFromContext(ctx))
}

func TestTracerStartTransactionWithParentContext(t *testing.T) {
Expand Down Expand Up @@ -124,6 +127,9 @@ func TestTracerStartChildSpan(t *testing.T) {
assert.NotNil(t, cs.(*span).span)

assert.True(t, trace.SpanContextFromContext(ctx).IsValid())

assert.True(t, apm.TransactionFromContext(ctx).Sampled())
assert.False(t, apm.SpanFromContext(ctx).Dropped())
}

func TestTracerStartChildSpanFromTransactionInContext(t *testing.T) {
Expand Down Expand Up @@ -162,7 +168,6 @@ func TestTracerStartChildSpanFromTransactionInContext(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {

apmTracer, _ := transporttest.NewRecorderTracer()
tp, err := NewTracerProvider(WithAPMTracer(apmTracer))
assert.NoError(t, err)
Expand Down
63 changes: 63 additions & 0 deletions module/apmotel/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.

//go:build go1.18
// +build go1.18

package apmotel // import "go.elastic.co/apm/module/apmotel/v2"

import (
"context"

"go.opentelemetry.io/otel/trace"

"go.elastic.co/apm/v2"
)

var (
oldOverrideContextWithSpan func(context.Context, *apm.Span) context.Context
oldOverrideContextWithTransaction func(context.Context, *apm.Transaction) context.Context
)

func init() {
// We override the apm context functions so that transactions
// and spans started with the native API are wrapped and made
// available as OpenTelemetry spans.

oldOverrideContextWithSpan = apm.OverrideContextWithSpan
oldOverrideContextWithTransaction = apm.OverrideContextWithTransaction
apm.OverrideContextWithSpan = contextWithSpan
apm.OverrideContextWithTransaction = contextWithTransaction
}

func contextWithSpan(ctx context.Context, apmSpan *apm.Span) context.Context {
ctx = oldOverrideContextWithSpan(ctx, apmSpan)
return trace.ContextWithSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID(apmSpan.TraceContext().Trace),
SpanID: trace.SpanID(apmSpan.TraceContext().Span),
TraceFlags: trace.TraceFlags(0).WithSampled(!apmSpan.Dropped()),
}))
}

func contextWithTransaction(ctx context.Context, apmTransaction *apm.Transaction) context.Context {
ctx = oldOverrideContextWithTransaction(ctx, apmTransaction)
return trace.ContextWithSpanContext(ctx, trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID(apmTransaction.TraceContext().Trace),
SpanID: trace.SpanID(apmTransaction.TraceContext().Span),
TraceFlags: trace.TraceFlags(0).WithSampled(apmTransaction.Sampled()),
}))
}
63 changes: 63 additions & 0 deletions module/apmotel/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.

//go:build go1.18
// +build go1.18

package apmotel

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/trace"

"go.elastic.co/apm/v2"
"go.elastic.co/apm/v2/transport/transporttest"
)

func TestLinkAgentToOtel(t *testing.T) {
apmTracer, _ := transporttest.NewRecorderTracer()
_, err := NewTracerProvider(WithAPMTracer(apmTracer))
assert.NoError(t, err)

ctx := context.Background()
tx := apmTracer.StartTransaction("test1", "test")
ctx = apm.ContextWithTransaction(ctx, tx)

apmTx := apm.TransactionFromContext(ctx)
otelSpan := trace.SpanFromContext(ctx)

assert.Equal(t, [16]byte(apmTx.TraceContext().Trace), [16]byte(otelSpan.SpanContext().TraceID()))
assert.Equal(t, [8]byte(apmTx.TraceContext().Span), [8]byte(otelSpan.SpanContext().SpanID()))
}

func TestLinkOtelToAgent(t *testing.T) {
apmTracer, _ := transporttest.NewRecorderTracer()
tp, err := NewTracerProvider(WithAPMTracer(apmTracer))
assert.NoError(t, err)

ctx := context.Background()
ctx, _ = tp.Tracer("").Start(ctx, "test")

apmTx := apm.TransactionFromContext(ctx)
otelSpan := trace.SpanFromContext(ctx)

assert.Equal(t, [16]byte(apmTx.TraceContext().Trace), [16]byte(otelSpan.SpanContext().TraceID()))
assert.Equal(t, [8]byte(apmTx.TraceContext().Span), [8]byte(otelSpan.SpanContext().SpanID()))
}