Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,19 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class CallLogRawAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void methodEnter(
@Advice.This Logger logger,
@Advice.Argument(0) ExtLogRecord record,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
public static CallDepth methodEnter(
@Advice.This Logger logger, @Advice.Argument(0) ExtLogRecord record) {
// need to track call depth across all loggers in order to avoid double capture when one
// logging framework delegates to another
callDepth = CallDepth.forClass(LoggerProvider.class);
CallDepth callDepth = CallDepth.forClass(LoggerProvider.class);
if (callDepth.getAndIncrement() == 0) {
LoggingEventMapper.INSTANCE.capture(logger, record);
}
return callDepth;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(@Advice.Local("otelCallDepth") CallDepth callDepth) {
public static void methodExit(@Advice.Enter CallDepth callDepth) {
callDepth.decrementAndGet();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class JbossLogmanagerInstrumentationModule extends InstrumentationModule {
public class JbossLogmanagerInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public JbossLogmanagerInstrumentationModule() {
super("jboss-logmanager-appender", "jboss-logmanager-appender-1.1");
Expand All @@ -23,4 +25,9 @@ public JbossLogmanagerInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new JbossLogmanagerInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.Map;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.jboss.logmanager.ExtLogRecord;
Expand Down Expand Up @@ -50,63 +49,59 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class GetMdcAdvice {

@Nullable
@AssignReturned.ToReturned
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit(
public static String onExit(
@Advice.This ExtLogRecord record,
@Advice.Argument(0) String key,
@Advice.Return(readOnly = false) String value) {
if (AgentCommonConfig.get().getTraceIdKey().equals(key)
|| AgentCommonConfig.get().getSpanIdKey().equals(key)
|| AgentCommonConfig.get().getTraceFlagsKey().equals(key)) {
if (value != null) {
// Assume already instrumented event if traceId/spanId/sampled is present.
return;
}

Context context = VirtualField.find(ExtLogRecord.class, Context.class).get(record);
if (context == null) {
return;
}
SpanContext spanContext = Java8BytecodeBridge.spanFromContext(context).getSpanContext();
if (!spanContext.isValid()) {
return;
}

if (AgentCommonConfig.get().getTraceIdKey().equals(key)) {
value = spanContext.getTraceId();
}
if (AgentCommonConfig.get().getSpanIdKey().equals(key)) {
value = spanContext.getSpanId();
}
if (AgentCommonConfig.get().getTraceFlagsKey().equals(key)) {
value = spanContext.getTraceFlags().asHex();
}
@Advice.Return @Nullable String value) {

boolean traceId = AgentCommonConfig.get().getTraceIdKey().equals(key);
boolean spanId = AgentCommonConfig.get().getSpanIdKey().equals(key);
boolean traceFlags = AgentCommonConfig.get().getTraceFlagsKey().equals(key);

if (!traceId && !spanId && !traceFlags) {
return value;
}
if (value != null) {
// Assume already instrumented event if traceId/spanId/sampled is present.
return value;
}

SpanContext spanContext = JbossLogManagerHelper.getSpanContext(record);
if (!spanContext.isValid()) {
return value;
}

if (traceId) {
return spanContext.getTraceId();
}
if (spanId) {
return spanContext.getSpanId();
}
// traceFlags == true
return spanContext.getTraceFlags().asHex();
}
}

@SuppressWarnings("unused")
public static class GetMdcCopyAdvice {

@AssignReturned.ToReturned
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit(
@Advice.This ExtLogRecord record,
@Advice.Return(readOnly = false) Map<String, String> value) {
public static Map<String, String> onExit(
@Advice.This ExtLogRecord record, @Advice.Return Map<String, String> value) {

if (value.containsKey(AgentCommonConfig.get().getTraceIdKey())
&& value.containsKey(AgentCommonConfig.get().getSpanIdKey())
&& value.containsKey(AgentCommonConfig.get().getTraceFlagsKey())) {
return;
}

Context context = VirtualField.find(ExtLogRecord.class, Context.class).get(record);
if (context == null) {
return;
return value;
}

SpanContext spanContext = Java8BytecodeBridge.spanFromContext(context).getSpanContext();
SpanContext spanContext = JbossLogManagerHelper.getSpanContext(record);
if (!spanContext.isValid()) {
return;
return value;
}

if (!value.containsKey(AgentCommonConfig.get().getTraceIdKey())) {
Expand All @@ -120,6 +115,7 @@ public static void onExit(
if (!value.containsKey(AgentCommonConfig.get().getTraceFlagsKey())) {
value.put(AgentCommonConfig.get().getTraceFlagsKey(), spanContext.getTraceFlags().asHex());
}
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jbosslogmanager.mdc.v1_1;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import org.jboss.logmanager.ExtLogRecord;

public class JbossLogManagerHelper {

private static final VirtualField<ExtLogRecord, Context> CONTEXT =
VirtualField.find(ExtLogRecord.class, Context.class);

public static SpanContext getSpanContext(ExtLogRecord record) {
Context context = CONTEXT.get(record);
if (context == null) {
return SpanContext.getInvalid();
}
return Span.fromContext(context).getSpanContext();
}

public static void setSpanContext(ExtLogRecord record, Context context) {
CONTEXT.set(record, context);
}

private JbossLogManagerHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.util.VirtualField;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -43,8 +41,7 @@ public static class CallAppendersAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.Argument(0) ExtLogRecord record) {
VirtualField.find(ExtLogRecord.class, Context.class)
.set(record, Java8BytecodeBridge.currentContext());
JbossLogManagerHelper.setSpanContext(record, Java8BytecodeBridge.currentContext());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class JbossLogmanagerInstrumentationModule extends InstrumentationModule {
public class JbossLogmanagerInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

public JbossLogmanagerInstrumentationModule() {
super("jboss-logmanager-mdc", "jboss-logmanager-mdc-1.1");
Expand All @@ -23,4 +25,9 @@ public JbossLogmanagerInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return asList(new JbossLoggerInstrumentation(), new JbossExtLogRecordInstrumentation());
}

@java.lang.Override
public boolean isIndyReady() {
return true;
}
}
Loading