From fd7db68852b91187412f2d77cc2f99f7a56643a7 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 28 Mar 2023 14:02:40 +0200 Subject: [PATCH] we can keep it simple --- .../apm/agent/ecs_logging/EcsLoggingUtils.java | 18 ++++++++++-------- .../JbossEcsServiceInstrumentation.java | 5 ++--- .../jul/JulEcsServiceInstrumentation.java | 5 ++--- .../log4j1/Log4jEcsServiceInstrumentation.java | 5 ++--- .../log4j2/Log4j2ServiceInstrumentation.java | 5 ++--- .../logback/LogBackServiceInstrumentation.java | 7 +++---- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java index 91e3571c650..9e5fa5e9483 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java @@ -25,6 +25,7 @@ import co.elastic.apm.agent.sdk.logging.LoggerFactory; import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent; import co.elastic.apm.agent.sdk.weakconcurrent.WeakSet; +import co.elastic.apm.agent.tracer.GlobalTracer; import javax.annotation.Nullable; import java.util.Objects; @@ -36,16 +37,17 @@ public class EcsLoggingUtils { private static final WeakSet nameChecked = WeakConcurrent.buildSet(); private static final WeakSet versionChecked = WeakConcurrent.buildSet(); + private static final ElasticApmTracer tracer = GlobalTracer.get().require(ElasticApmTracer.class); @Nullable - public static String getServiceName(ElasticApmTracer tracer) { + public static String getServiceName() { ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader()); String configuredServiceName = tracer.getConfig(CoreConfiguration.class).getServiceName(); return serviceInfo != null ? serviceInfo.getServiceName() : configuredServiceName; } @Nullable - public static String getServiceVersion(ElasticApmTracer tracer) { + public static String getServiceVersion() { ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader()); String configuredServiceVersion = tracer.getConfig(CoreConfiguration.class).getServiceVersion(); return serviceInfo != null ? serviceInfo.getServiceVersion() : configuredServiceVersion; @@ -58,27 +60,27 @@ private static void warnIfMisConfigured(String key, @Nullable String configuredV } @Nullable - public static String getOrWarnServiceVersion(ElasticApmTracer tracer,Object target, @Nullable String value) { + public static String getOrWarnServiceVersion(Object target, @Nullable String value) { if (!versionChecked.add(target)) { return value; } if (value == null) { - return getServiceVersion(tracer); + return getServiceVersion(); } else { - warnIfMisConfigured("service.version", value, getServiceVersion(tracer)); + warnIfMisConfigured("service.version", value, getServiceVersion()); return value; } } @Nullable - public static String getOrWarnServiceName(ElasticApmTracer tracer, Object target, @Nullable String value) { + public static String getOrWarnServiceName(Object target, @Nullable String value) { if (!nameChecked.add(target)) { return value; } if (value == null) { - return getServiceName(tracer); + return getServiceName(); } else { - warnIfMisConfigured("service.name", value, getServiceName(tracer)); + warnIfMisConfigured("service.name", value, getServiceName()); return value; } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java index f9861da7861..9f66b68dcd2 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation; import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils; -import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.logging.jboss.logmanager.EcsFormatter; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -59,7 +58,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceName") @Nullable String serviceName) { - return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), formatter, serviceName); + return EcsLoggingUtils.getOrWarnServiceName(formatter, serviceName); } } @@ -82,7 +81,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), formatter, serviceVersion); + return EcsLoggingUtils.getOrWarnServiceVersion(formatter, serviceVersion); } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java index f661ce93433..5ee50dff886 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation; import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils; -import co.elastic.apm.agent.impl.ElasticApmTracer; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; import net.bytebuddy.description.method.MethodDescription; @@ -59,7 +58,7 @@ public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceName") @Nullable String serviceName) { // setServiceName is protected in earlier versions, thus setting field directly - return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), formatter, serviceName); + return EcsLoggingUtils.getOrWarnServiceName(formatter, serviceName); } } @@ -82,7 +81,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), formatter, serviceVersion); + return EcsLoggingUtils.getOrWarnServiceVersion(formatter, serviceVersion); } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java index 7bddb84db0b..abc9860a5f7 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation; import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils; -import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.logging.log4j.EcsLayout; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -59,7 +58,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object layout, @Advice.FieldValue("serviceName") @Nullable String serviceName) { - return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), layout, serviceName); + return EcsLoggingUtils.getOrWarnServiceName(layout, serviceName); } } @@ -82,7 +81,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object layout, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), layout, serviceVersion); + return EcsLoggingUtils.getOrWarnServiceVersion(layout, serviceVersion); } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java index 2b61ce8ba82..68a13b34b82 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation; import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils; -import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.logging.log4j2.EcsLayout; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -58,7 +57,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object builder, @Advice.FieldValue("serviceName") @Nullable String serviceName) { - return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), builder, serviceName); + return EcsLoggingUtils.getOrWarnServiceName(builder, serviceName); } } } @@ -80,7 +79,7 @@ public static class AdviceClass { public static String onEnter(@Advice.This Object builder, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), builder, serviceVersion); + return EcsLoggingUtils.getOrWarnServiceVersion(builder, serviceVersion); } } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java index baf47417d92..e834a6b681f 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation; import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils; -import co.elastic.apm.agent.impl.ElasticApmTracer; import co.elastic.logging.logback.EcsEncoder; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; @@ -58,7 +57,7 @@ public static class AdviceClass { public static String onExit(@Advice.This Object encoder, @Advice.FieldValue("serviceName") @Nullable String serviceName) { - return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), encoder, serviceName); + return EcsLoggingUtils.getOrWarnServiceName(encoder, serviceName); } } } @@ -80,7 +79,7 @@ public static class AdviceClass { public static String onExit(@Advice.This Object encoder, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), encoder, serviceVersion); + return EcsLoggingUtils.getOrWarnServiceVersion(encoder, serviceVersion); } } @@ -94,7 +93,7 @@ public static class VersionAdvice { public static String onExit(@Advice.This Object encoder, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), encoder, serviceVersion); + return EcsLoggingUtils.getOrWarnServiceVersion(encoder, serviceVersion); } }