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

set service name & version in ecs-logging #3064

Merged
merged 29 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
da6af11
migrate to junit5 + move a few things
SylvainJuge Mar 21, 2023
1c9fb76
make tests generic & reusable
SylvainJuge Mar 21, 2023
bc679e2
add jul service name+version
SylvainJuge Mar 21, 2023
d245ee0
slightly better msg on failure
SylvainJuge Mar 21, 2023
013f46a
a bit of extra cleanup
SylvainJuge Mar 21, 2023
a1bfdeb
add log4j 1.x service name & version
SylvainJuge Mar 21, 2023
39350e9
start jboss + simplify a bit
SylvainJuge Mar 21, 2023
f892a82
rename a bit
SylvainJuge Mar 21, 2023
098bdba
add tests+fix jboss impl.
SylvainJuge Mar 21, 2023
dd8f466
add logback
SylvainJuge Mar 21, 2023
dbbca85
attempt to run in isolation + narrow dependencies
SylvainJuge Mar 22, 2023
65a9117
fix generate doc cmd
SylvainJuge Mar 22, 2023
eae7867
update generated doc
SylvainJuge Mar 22, 2023
a3d6d09
use consistent instr. group naming
SylvainJuge Mar 22, 2023
a030ec0
simplify a bit log instrumentation groups
SylvainJuge Mar 22, 2023
8b64109
update generated doc
SylvainJuge Mar 22, 2023
aa321f5
minor cleanup
SylvainJuge Mar 22, 2023
6d92158
add warnings when mis-configured
SylvainJuge Mar 22, 2023
afa89f8
fix javadoc
SylvainJuge Mar 23, 2023
d0eeea4
remove advice duplication
SylvainJuge Mar 23, 2023
9693025
simplify instrumentation
SylvainJuge Mar 23, 2023
345e3e6
use direct field access for reliability
SylvainJuge Mar 23, 2023
c4b4f19
minor cleanup
SylvainJuge Mar 23, 2023
e2b841c
Merge branch 'main' of github.com:elastic/apm-agent-java into log-ser…
SylvainJuge Mar 28, 2023
e7c15f0
apply suggestion from review
SylvainJuge Mar 28, 2023
5b7bcdf
simplify instrumentation groups for logs
SylvainJuge Mar 28, 2023
252fe67
update changelog
SylvainJuge Mar 28, 2023
fe848b3
minor fixes
SylvainJuge Mar 28, 2023
49717f6
Merge remote-tracking branch 'upstream' into log-service-name
SylvainJuge Mar 28, 2023
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: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ See [`apm-agent-plugins/README.md`](apm-agent-plugins/README.md)

HTML Documentation is generated from text files stored in `docs` folder using [AsciiDoc](http://asciidoc.org/) format.
The `configuration.asciidoc` file is generated from running `co.elastic.apm.agent.configuration.ConfigurationExporter`
(e.g. via `./mvnw test -Dtest="ConfigurationExporterTest" -DfailIfNoTests=false`). All the other asciidoc text files
(e.g. via `./mvnw -Dsurefire.failIfNoTests=false -Dtest=ConfigurationExporterTest -pl apm-agent -am clean test`). All the other asciidoc text files
are written manually.
JonasKunz marked this conversation as resolved.
Show resolved Hide resolved

A preview of the documentation is generated for each pull-request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public void run() {
System.out.println(failure);
failure.getException().printStackTrace();
}
assertThat(listener.getSummary().getTestsFailedCount()).isZero();
assertThat(listener.getSummary().getTestsFailedCount())
.describedAs("at least one test failure reported, see stack trace for investigation")
.isZero();
}

}
58 changes: 52 additions & 6 deletions apm-agent-plugins/apm-ecs-logging-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,82 @@
</properties>

<dependencies>

<!-- log4j 1.x -->
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>log4j-ecs-layout</artifactId>
<version>${version.ecs.logging}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<!-- log4j 2.x -->
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>log4j2-ecs-layout</artifactId>
<version>${version.ecs.logging}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<!-- JUL -->
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>jul-ecs-formatter</artifactId>
<version>${version.ecs.logging}</version>
<scope>provided</scope>
</dependency>

<!-- Jboss logmanager -->
<dependency>
<groupId>co.elastic.apm</groupId>
<artifactId>apm-logging-plugin-common</artifactId>
<version>${project.version}</version>
<groupId>co.elastic.logging</groupId>
<artifactId>jboss-logmanager-ecs-formatter</artifactId>
<version>${version.ecs.logging}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<version>2.1.15.Final</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<!-- logback -->
<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>logback-ecs-encoder</artifactId>
<version>${version.ecs.logging}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.8</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>co.elastic.apm</groupId>
<artifactId>apm-logging-plugin-common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@
*/
package co.elastic.apm.agent.ecs_logging;

import co.elastic.apm.agent.testutils.JUnit4TestClassWithDependencyRunner;
import org.junit.jupiter.api.Test;
import co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers;
import co.elastic.apm.agent.loginstr.AbstractLogIntegrationInstrumentation;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.List;
import static net.bytebuddy.matcher.ElementMatchers.not;

public class Log4j2_17_1ServiceNameInstrumentationTest {
private final JUnit4TestClassWithDependencyRunner runner;
public abstract class EcsLoggingInstrumentation extends AbstractLogIntegrationInstrumentation {

public Log4j2_17_1ServiceNameInstrumentationTest() throws Exception {
List<String> dependencies = List.of("co.elastic.logging:log4j2-ecs-layout:1.3.2");
runner = new JUnit4TestClassWithDependencyRunner(dependencies, Log4j2ServiceNameInstrumentationTest.class);
}

@Test
public void testVersions() {
runner.run();
@Override
public ElementMatcher.Junction<ClassLoader> getClassLoaderMatcher() {
// ECS formatter that is loaded within the agent should not be instrumented
return not(CustomElementMatchers.isInternalPluginClassLoader());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.
*/
package co.elastic.apm.agent.ecs_logging;

import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.configuration.ServiceInfo;
import co.elastic.apm.agent.impl.ElasticApmTracer;

import javax.annotation.Nullable;

public class EcsLoggingUtils {

@Nullable
public static String getServiceName(ElasticApmTracer tracer){
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){
ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader());
String configuredServiceVersion = tracer.getConfig(CoreConfiguration.class).getServiceVersion();
return serviceInfo != null ? serviceInfo.getServiceVersion() : configuredServiceVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.
*/
package co.elastic.apm.agent.ecs_logging.jboss;

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.apm.agent.impl.GlobalTracer;
import co.elastic.logging.jboss.logmanager.EcsFormatter;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class JbossEcsServiceNameInstrumentation extends EcsLoggingInstrumentation {

@Override
public ElementMatcher.Junction<? super TypeDescription> getTypeMatcher() {
return named("co.elastic.logging.jboss.logmanager.EcsFormatter");
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return isConstructor();
}

@Override
protected String getLoggingInstrumentationGroupName() {
return "jboss-logging-ecs";
}

public static class AdviceClass {

private static final ElasticApmTracer tracer = GlobalTracer.requireTracerImpl();

@Advice.OnMethodExit(inline = false)
public static void onExit(@Advice.This EcsFormatter ecsFormatter) {
ecsFormatter.setServiceName(EcsLoggingUtils.getServiceName(tracer));
}
}
}
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.
*/
package co.elastic.apm.agent.ecs_logging.jboss;

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.apm.agent.impl.GlobalTracer;
import co.elastic.logging.jboss.logmanager.EcsFormatter;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class JbossEcsServiceVersionInstrumentation extends EcsLoggingInstrumentation {

@Override
public ElementMatcher.Junction<? super TypeDescription> getTypeMatcher() {
return named("co.elastic.logging.jboss.logmanager.EcsFormatter")
// setServiceVersion introduced in 1.4.0
.and(declaresMethod(named("setServiceVersion")));
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return isConstructor();
}

@Override
protected String getLoggingInstrumentationGroupName() {
return "jboss-logging-ecs";
}

public static class AdviceClass {

private static final ElasticApmTracer tracer = GlobalTracer.requireTracerImpl();

@Advice.OnMethodExit(inline = false)
public static void onExit(@Advice.This EcsFormatter ecsFormatter) {
ecsFormatter.setServiceVersion(EcsLoggingUtils.getServiceVersion(tracer));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,27 @@
* specific language governing permissions and limitations
* under the License.
*/
package co.elastic.apm.agent.ecs_logging;
package co.elastic.apm.agent.ecs_logging.jul;

import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
import co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers;
import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation;
import co.elastic.apm.agent.loginstr.correlation.CorrelationIdMapAdapter;
import co.elastic.logging.jul.EcsFormatter;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.not;

/**
* Instruments {@link EcsFormatter#getMdcEntries()} to provide correlation IDs at runtime.
* Application(s) copies of ecs-logging and the one in the agent will be instrumented, hence providing log correlation
* for all of them.
*/
@SuppressWarnings("JavadocReference")
public class JulEcsFormatterInstrumentation extends TracerAwareInstrumentation {

@Override
public ElementMatcher.Junction<ClassLoader> getClassLoaderMatcher() {
// ECS formatter that is loaded within the agent should not be instrumented
return not(CustomElementMatchers.isInternalPluginClassLoader());
}

@Override
public Collection<String> getInstrumentationGroupNames() {
return Arrays.asList("logging", "jul-ecs");
}
public class JulEcsMdcInstrumentation extends EcsLoggingInstrumentation {

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
Expand All @@ -63,6 +48,11 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return named("getMdcEntries");
}

@Override
protected String getLoggingInstrumentationGroupName() {
return "jul-correlation";
}

public static class AdviceClass {

@Advice.AssignReturned.ToReturned
Expand All @@ -72,6 +62,4 @@ public static Map<String, String> onExit() {
}

}


}
Loading