Skip to content

Commit

Permalink
Log current thread id and name as span attribute
Browse files Browse the repository at this point in the history
* Bump opentelementry-java versions
* Fix all compilation issues
* Add a new `SpanProcessor` that appends thread attributes to each span
  • Loading branch information
mateuszrzeszutek committed Aug 25, 2020
1 parent d58eca2 commit 71ba52f
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 10 deletions.
4 changes: 2 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ configurations.all {
ext {
versions = [
// Check https://oss.jfrog.org/libs-snapshot/io/opentelemetry/ for latest snapshot version.
opentelemetry : '0.8.0-20200811.154243-20',
opentelemetry : '0.8.0-20200824.150114-44',
// 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',
opentelemetryOther: '0.8.0-20200824.150114-44',

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
Expand Up @@ -37,6 +37,11 @@ public void add(double delta, Labels labels) {
agentDoubleCounter.add(delta, LabelBridging.toAgent(labels));
}

@Override
public void add(double delta) {
agentDoubleCounter.add(delta);
}

@Override
public BoundDoubleCounter bind(Labels labels) {
return new BoundInstrument(agentDoubleCounter.bind(LabelBridging.toAgent(labels)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void add(double delta, Labels labels) {
agentDoubleUpDownCounter.add(delta, LabelBridging.toAgent(labels));
}

@Override
public void add(double delta) {
agentDoubleUpDownCounter.add(delta);
}

@Override
public BoundDoubleUpDownCounter bind(Labels labels) {
return new BoundInstrument(agentDoubleUpDownCounter.bind(LabelBridging.toAgent(labels)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void record(double delta, Labels labels) {
agentDoubleValueRecorder.record(delta, LabelBridging.toAgent(labels));
}

@Override
public void record(double delta) {
agentDoubleValueRecorder.record(delta);
}

@Override
public BoundDoubleValueRecorder bind(Labels labels) {
return new BoundInstrument(agentDoubleValueRecorder.bind(LabelBridging.toAgent(labels)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public void add(long delta, Labels labels) {
agentLongCounter.add(delta, LabelBridging.toAgent(labels));
}

@Override
public void add(long delta) {
agentLongCounter.add(delta);
}

@Override
public BoundLongCounter bind(Labels labels) {
return new BoundInstrument(agentLongCounter.bind(LabelBridging.toAgent(labels)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public void add(long delta, Labels labels) {
agentLongUpDownCounter.add(delta, LabelBridging.toAgent(labels));
}

@Override
public void add(long delta) {
agentLongUpDownCounter.add(delta);
}

@Override
public BoundLongUpDownCounter bind(Labels labels) {
return new BoundInstrument(agentLongUpDownCounter.bind(LabelBridging.toAgent(labels)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void record(long delta, Labels labels) {
agentLongValueRecorder.record(delta, LabelBridging.toAgent(labels));
}

@Override
public void record(long delta) {
agentLongValueRecorder.record(delta);
}

@Override
public BoundLongValueRecorder bind(Labels labels) {
return new BoundInstrument(agentLongValueRecorder.bind(LabelBridging.toAgent(labels)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.common.ReadableKeyValuePairs.KeyValueConsumer;
import io.opentelemetry.sdk.common.export.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.util.Collection;
Expand All @@ -30,7 +31,7 @@ public LoggingExporter(String prefix) {
}

@Override
public ResultCode export(Collection<SpanData> list) {
public CompletableResultCode export(Collection<SpanData> list) {
for (SpanData span : list) {
System.out.print(
prefix + " " + span.getName() + " " + span.getSpanId().toLowerBase16() + " ");
Expand Down Expand Up @@ -59,12 +60,12 @@ public void consume(String key, AttributeValue value) {
});
}
System.out.println();
return ResultCode.SUCCESS;
return CompletableResultCode.ofSuccess();
}

@Override
public ResultCode flush() {
return ResultCode.SUCCESS;
public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.javaagent.tooling;

import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.trace.attributes.SemanticAttributes;

public class AddThreadDetailsSpanProcessor implements SpanProcessor {
@Override
public void onStart(ReadWriteSpan span) {
Thread currentThread = Thread.currentThread();
SemanticAttributes.THREAD_ID.set(span, currentThread.getId());
SemanticAttributes.THREAD_NAME.set(span, currentThread.getName());
}

@Override
public boolean isStartRequired() {
return true;
}

@Override
public void onEnd(ReadableSpan span) {}

@Override
public boolean isEndRequired() {
return false;
}

@Override
public void shutdown() {}

@Override
public void forceFlush() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ private static <F> F getExporterFactory(Class<F> service, ExporterClassLoader ex
}

private static void configure() {
TracerSdkProvider tracerSdkProvider = OpenTelemetrySdk.getTracerProvider();

// Register additional thread details logging span processor
tracerSdkProvider.addSpanProcessor(new AddThreadDetailsSpanProcessor());

// Execute any user-provided (usually vendor-provided) configuration logic.
ServiceLoader<TracerCustomizer> serviceLoader =
ServiceLoader.load(TracerCustomizer.class, TracerInstaller.class.getClassLoader());
TracerSdkProvider tracerSdkProvider = OpenTelemetrySdk.getTracerProvider();
for (TracerCustomizer customizer : serviceLoader) {
customizer.configure(tracerSdkProvider);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.javaagent.tooling

import io.opentelemetry.sdk.trace.ReadWriteSpan
import io.opentelemetry.trace.attributes.SemanticAttributes
import spock.lang.Specification

class AddThreadDetailsSpanProcessorTest extends Specification {
def span = Mock(ReadWriteSpan)

def processor = new AddThreadDetailsSpanProcessor()

def "should require onStart call"() {
when:
def startRequired = processor.isStartRequired()

then:
assert startRequired
}

def "should set thread attributes on span start"() {
given:
def currentThreadName = Thread.currentThread().name
def currentThreadId = Thread.currentThread().id

when:
processor.onStart(span)

then:
1 * span.setAttribute(SemanticAttributes.THREAD_ID.key(), currentThreadId)
1 * span.setAttribute(SemanticAttributes.THREAD_NAME.key(), currentThreadName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.primitives.Longs;
import io.opentelemetry.common.AttributeValue;
import io.opentelemetry.common.ReadableKeyValuePairs.KeyValueConsumer;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
Expand Down Expand Up @@ -62,8 +63,8 @@ public class InMemoryExporter implements SpanProcessor {
private final AtomicInteger nextSpanOrder = new AtomicInteger();

@Override
public void onStart(ReadableSpan readableSpan) {
SpanData sd = readableSpan.toSpanData();
public void onStart(ReadWriteSpan readWriteSpan) {
SpanData sd = readWriteSpan.toSpanData();
log.debug(
">>> SPAN START: {} id={} traceid={} parent={}, library={}",
sd.getName(),
Expand All @@ -72,7 +73,7 @@ public void onStart(ReadableSpan readableSpan) {
sd.getParentSpanId().toLowerBase16(),
sd.getInstrumentationLibraryInfo());
synchronized (tracesLock) {
spanOrders.put(readableSpan.getSpanContext().getSpanId(), nextSpanOrder.getAndIncrement());
spanOrders.put(readWriteSpan.getSpanContext().getSpanId(), nextSpanOrder.getAndIncrement());
}
}

Expand Down

0 comments on commit 71ba52f

Please sign in to comment.