Skip to content

Commit

Permalink
[#11971] Extract micrometer module
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 17, 2025
1 parent 572f309 commit 4733cc2
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 60 deletions.
1 change: 1 addition & 0 deletions agent-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

<module>profiler</module>
<module>profiler-logging</module>
<module>profiler-micrometer</module>
<module>profiler-optional</module>
<module>profiler-test</module>

Expand Down
58 changes: 58 additions & 0 deletions agent-module/profiler-micrometer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-agent-module</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-profiler-micrometer</artifactId>
<packaging>jar</packaging>

<properties>
<jdk.version>1.8</jdk.version>
<jdk.home>${env.JAVA_8_HOME}</jdk.home>
</properties>

<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-commons</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-commons-config</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-otlp</artifactId>
</dependency>

<!-- Logging dependencies -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
</dependencies>

<build>
<plugins>

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ public class AgentOtlpConfig {
public static OtlpConfig getOtlpConfig(String url, String step, String batchSize,
String serviceName, String applicationName, String agentId) {
Properties propertiesConfig = new Properties();
propertiesConfig.put("otlp.url", url);
propertiesConfig.put("otlp.step", String.valueOf(step));
propertiesConfig.put("otlp.batchSize", String.valueOf(batchSize));
propertiesConfig.setProperty("otlp.url", url);
propertiesConfig.setProperty("otlp.step", step);
propertiesConfig.setProperty("otlp.batchSize", batchSize);

String attribute = String.format("service.namespace=%s,service.name=%s,pinpoint.agentId=%s", serviceName, applicationName, agentId);
propertiesConfig.put("otlp.resourceAttributes", attribute);
OtlpConfig otlpConfig = (key -> (String) propertiesConfig.get(key));
return otlpConfig;
propertiesConfig.setProperty("otlp.resourceAttributes", attribute);
return propertiesConfig::getProperty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.navercorp.pinpoint.profiler.micrometer;

import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.navercorp.pinpoint.profiler.micrometer.config.MicrometerConfig;
import io.micrometer.registry.otlp.OtlpConfig;
import org.apache.logging.log4j.LogManager;
Expand All @@ -33,8 +35,9 @@ public class DefaultMicrometerMonitor implements MicrometerMonitor {

private final AgentOtlpMeterRegistry registry;

public DefaultMicrometerMonitor(String applicationName,
String agentId,
@Inject
public DefaultMicrometerMonitor(@Named("pinpoint.applicationName") String applicationName,
@Named("pinpoint.agentId") String agentId,
MicrometerConfig config) {
Objects.requireNonNull(applicationName, "applicationName");
Objects.requireNonNull(agentId, "agentId");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.navercorp.pinpoint.profiler.micrometer;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.navercorp.pinpoint.common.config.util.ValueAnnotationProcessor;
import com.navercorp.pinpoint.profiler.micrometer.config.DefaultMicrometerConfig;
import com.navercorp.pinpoint.profiler.micrometer.config.MicrometerConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;
import java.util.function.Function;

public class MicrometerModule extends AbstractModule {

private final Logger logger = LogManager.getLogger(this.getClass());

private final Function<String, String> properties;

public MicrometerModule(Function<String, String> properties) {
this.properties = Objects.requireNonNull(properties, "properties");
}

@Override
protected void configure() {
logger.info("load {}", this.getClass().getSimpleName());

final ValueAnnotationProcessor process = new ValueAnnotationProcessor();

MicrometerConfig micrometerConfig = new DefaultMicrometerConfig();
process.process(micrometerConfig, properties);

logger.info("{}", micrometerConfig);

if (micrometerConfig.isEnable()) {
bind(MicrometerConfig.class).toInstance(micrometerConfig);
bind(MicrometerMonitor.class).to(DefaultMicrometerMonitor.class).in(Singleton.class);
} else {
bind(MicrometerMonitor.class).to(DisableMicrometerMonitor.class).in(Singleton.class);;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.navercorp.pinpoint.profiler.micrometer;

import io.micrometer.registry.otlp.OtlpConfig;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AgentOtlpConfigTest {

@Test
void getOtlpConfig() {
String url = "http://localhost:8080";
String step = "10s";
String batchSize = "100";
String serviceName = "serviceName";
String applicationName = "applicationName";
String agentId = "agentId";

OtlpConfig otlpConfig = AgentOtlpConfig.getOtlpConfig(url, step, batchSize, serviceName, applicationName, agentId);
String attribute = otlpConfig.get("otlp.resourceAttributes");
assertEquals("service.namespace=serviceName,service.name=applicationName,pinpoint.agentId=agentId", attribute);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.navercorp.pinpoint.profiler.micrometer;

import com.navercorp.pinpoint.profiler.micrometer.config.DefaultMicrometerConfig;
import com.navercorp.pinpoint.profiler.micrometer.config.MicrometerConfig;
import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

class DefaultMicrometerMonitorTest {

@Test
void micrometerMonitor() {
MicrometerConfig config = spy(DefaultMicrometerConfig.class);
when(config.isEnable()).thenReturn(true);
when(config.getUrl()).thenReturn("http://localhost:8080");
when(config.getStep()).thenReturn("10s");
when(config.getBatchSize()).thenReturn("100");

DefaultMicrometerMonitor monitor = new DefaultMicrometerMonitor("applicationName", "agentId", config);

monitor.stop();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Configuration status="INFO">
<Properties>
<Property name="console_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %clr{%-5level} %clr{%-40.40logger{1.}}{cyan}:%3L -- %msg%n</Property>
<Property name="file_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%3L -- %msg%n</Property>
</Properties>

<Appenders>
<Console name="console" target="system_out">
<PatternLayout pattern="${file_message_pattern}"/>
</Console>
</Appenders>

<Loggers>
<Logger name="com.navercorp.pinpoint" level="DEBUG" additivity="false">
<AppenderRef ref="console"/>
</Logger>

<Root level="DEBUG">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Configuration:
status: INFO

Properties:
Property:
- name: "console_message_pattern"
value: "%d{MM-dd HH:mm:ss.sss} [%15.15t] %clr{%-5level} %clr{%-40.40logger{1.}}{cyan}:%3L -- %msg%n"
- name: "file_message_pattern"
value: "%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%3L -- %msg%n"

Appenders:
Console:
name: console
target: system_out
PatternLayout:
pattern: ${file_message_pattern}

Loggers:
Logger:
- name: com.navercorp.pinpoint
level: DEBUG
additivity: false
AppenderRef:
- ref: console

Root:
level: DEBUG
AppenderRef:
- ref: console
11 changes: 7 additions & 4 deletions agent-module/profiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-profiler-micrometer</artifactId>
<version>${project.version}</version>
</dependency>

<!-- required java9 java.xml -->
<!--<dependency>
<groupId>xerces</groupId>
Expand All @@ -177,10 +183,7 @@
<artifactId>grpc-inprocess</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-otlp</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@
import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService;
import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService;
import com.navercorp.pinpoint.profiler.metadata.StringMetaDataService;
import com.navercorp.pinpoint.profiler.micrometer.MicrometerMonitor;
import com.navercorp.pinpoint.profiler.micrometer.MicrometerMonitorProvider;
import com.navercorp.pinpoint.profiler.monitor.AgentStatMonitor;
import com.navercorp.pinpoint.profiler.monitor.DeadlockMonitor;
import com.navercorp.pinpoint.profiler.monitor.DeadlockThreadRegistry;
Expand Down Expand Up @@ -216,7 +214,6 @@ protected void configure() {
bind(DeadlockMonitor.class).toProvider(DeadlockMonitorProvider.class).in(Scopes.SINGLETON);
bind(AgentInfoSender.class).toProvider(AgentInfoSenderProvider.class).in(Scopes.SINGLETON);
bind(AgentStatMonitor.class).to(DefaultAgentStatMonitor.class).in(Scopes.SINGLETON);
bind(MicrometerMonitor.class).toProvider(MicrometerMonitorProvider.class).in(Scopes.SINGLETON);
}

private void bindTraceComponent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.navercorp.pinpoint.profiler.context.module.config.ConfigurationLoader;
import com.navercorp.pinpoint.profiler.context.monitor.config.DefaultExceptionTraceConfig;
import com.navercorp.pinpoint.profiler.context.monitor.config.ExceptionTraceConfig;
import com.navercorp.pinpoint.profiler.micrometer.MicrometerModule;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -45,12 +46,20 @@ public Module newModule(AgentContextOption agentOption) {
final Module thriftStatsModule = new ThriftStatsModule();

final ProfilerConfig properties = agentOption.getProfilerConfig();
final Module micrometerModule = new MicrometerModule(properties::readString);

final Module exceptionTraceModule = newExceptionTraceModule(properties.getProperties());

return Modules.combine(config, pluginModule, applicationContextModule,


return Modules.combine(config,
pluginModule,
applicationContextModule,
rpcModule,
statsModule, thriftStatsModule,
exceptionTraceModule);
statsModule,
thriftStatsModule,
exceptionTraceModule,
micrometerModule);
}

protected Module newExceptionTraceModule(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.profiler.AgentContextOption;
Expand Down Expand Up @@ -49,8 +50,6 @@
import com.navercorp.pinpoint.profiler.instrument.config.InstrumentConfig;
import com.navercorp.pinpoint.profiler.instrument.config.InstrumentMatcherCacheConfig;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.micrometer.config.DefaultMicrometerConfig;
import com.navercorp.pinpoint.profiler.micrometer.config.MicrometerConfig;
import com.navercorp.pinpoint.profiler.plugin.PluginJar;
import com.navercorp.pinpoint.profiler.plugin.config.DefaultPluginLoadingConfig;
import com.navercorp.pinpoint.profiler.plugin.config.PluginLoadingConfig;
Expand Down Expand Up @@ -118,11 +117,6 @@ protected void configure() {
logger.info("{}", monitorConfig);
bind(MonitorConfig.class).toInstance(monitorConfig);

MicrometerConfig micrometerConfig = new DefaultMicrometerConfig();
configurationLoader.load(micrometerConfig);
logger.info("{}", micrometerConfig);
bind(MicrometerConfig.class).toInstance(micrometerConfig);

bind(Instrumentation.class).toInstance(agentOption.getInstrumentation());

bind(InterceptorRegistryBinder.class).toProvider(InterceptorRegistryBinderProvider.class).in(Scopes.SINGLETON);
Expand Down Expand Up @@ -165,6 +159,10 @@ private void bindAgentInformation(AgentContextOption agentOption) {
bind(String.class).annotatedWith(AgentName.class).toInstance(agentOption.getAgentName());
bind(String.class).annotatedWith(ApplicationName.class).toInstance(agentOption.getApplicationName());

bind(String.class).annotatedWith(Names.named("pinpoint.agentId")).toInstance(agentOption.getAgentId());
bind(String.class).annotatedWith(Names.named("pinpoint.agentName")).toInstance(agentOption.getAgentName());
bind(String.class).annotatedWith(Names.named("pinpoint.applicationName")).toInstance(agentOption.getApplicationName());

final ContainerResolver containerResolver = new ContainerResolver();
final boolean isContainer = containerResolver.isContainer();
bind(Boolean.class).annotatedWith(Container.class).toInstance(isContainer);
Expand Down
Loading

0 comments on commit 4733cc2

Please sign in to comment.