Skip to content

Commit

Permalink
Instrument DefaultSpan.create to return a bridged span. (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuraag Agrawal authored Aug 12, 2020
1 parent e268014 commit e9d456d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
7 changes: 4 additions & 3 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ configurations.all {
ext {
versions = [
// Check https://oss.jfrog.org/libs-snapshot/io/opentelemetry/ for latest snapshot version.
opentelemetry : '0.7.0',
// Snapshot versions are getting split into two suffixes
opentelemetryOther: '0.7.0',
opentelemetry : '0.8.0-20200811.154243-20',
// Snapshot versions are often split into two suffixes based on jfrog's whims, best to keep
// these separate until GA
opentelemetryOther: '0.8.0-20200811.154243-20',

slf4j : "1.7.30",
guava : "20.0", // Last version to support Java 7
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 io.opentelemetry.auto.instrumentation.opentelemetryapi;

import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.auto.service.AutoService;
import io.opentelemetry.auto.instrumentation.opentelemetryapi.trace.Bridging;
import io.opentelemetry.auto.tooling.Instrumenter;
import io.opentelemetry.trace.DefaultSpan;
import java.util.Collections;
import java.util.Map;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

@AutoService(Instrumenter.class)
public class DefaultSpanInstrumentation extends AbstractInstrumentation {
@Override
public ElementMatcher<? super TypeDescription> typeMatcher() {
return named("unshaded.io.opentelemetry.trace.DefaultSpan");
}

@Override
public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
return Collections.singletonMap(
isMethod().and(isPublic()).and(isStatic()).and(named("create")),
DefaultSpanInstrumentation.class.getName() + "$CreateAdvice");
}

public static class CreateAdvice {
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Return(readOnly = false) unshaded.io.opentelemetry.trace.Span span) {
span = Bridging.toUnshaded(DefaultSpan.create(Bridging.toShaded(span.getContext())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import io.opentelemetry.trace.attributes.SemanticAttributes
import unshaded.io.grpc.Context
import unshaded.io.opentelemetry.OpenTelemetry
import unshaded.io.opentelemetry.context.Scope
import unshaded.io.opentelemetry.trace.DefaultSpan
import unshaded.io.opentelemetry.trace.Span
import unshaded.io.opentelemetry.trace.Status

Expand Down Expand Up @@ -357,4 +358,18 @@ class TracerTest extends AgentTestRunner {
}
}
}
def "add DefaultSpan to context"() {
when:
// Lazy way to get a span context
def tracer = OpenTelemetry.getTracerProvider().get("test")
def testSpan = tracer.spanBuilder("test").setSpanKind(PRODUCER).startSpan()
testSpan.end()
def span = DefaultSpan.create(testSpan.getContext())
def context = withSpan(span, Context.current())
then:
getSpan(context).getContext().getSpanId() == span.getContext().getSpanId()
}
}

0 comments on commit e9d456d

Please sign in to comment.