Skip to content

Commit

Permalink
[#11758] Separate banner into module
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Nov 21, 2024
1 parent 692bbf5 commit f4c92e5
Show file tree
Hide file tree
Showing 19 changed files with 276 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.navercorp.pinpoint.ProductInfo;
import com.navercorp.pinpoint.bootstrap.agentdir.AgentDirectory;
import com.navercorp.pinpoint.bootstrap.agentdir.LogDirCleaner;
import com.navercorp.pinpoint.bootstrap.banner.PinpointBannerImpl;
import com.navercorp.pinpoint.bootstrap.classloader.PinpointClassLoaderFactory;
import com.navercorp.pinpoint.bootstrap.classloader.ProfilerLibs;
import com.navercorp.pinpoint.bootstrap.config.DefaultProfilerConfig;
Expand All @@ -26,7 +25,6 @@
import com.navercorp.pinpoint.bootstrap.config.PropertyLoader;
import com.navercorp.pinpoint.bootstrap.config.PropertyLoaderFactory;
import com.navercorp.pinpoint.common.Version;
import com.navercorp.pinpoint.common.banner.PinpointBanner;
import com.navercorp.pinpoint.common.util.OsEnvSimpleProperty;
import com.navercorp.pinpoint.common.util.PropertySnapshot;
import com.navercorp.pinpoint.common.util.SimpleProperty;
Expand Down Expand Up @@ -152,11 +150,6 @@ boolean start() {
pinpointAgent.registerStopHandler();

logger.info("pinpoint agent started normally.");

final PinpointBannerImpl banner = new PinpointBannerImpl(profilerConfig.readList("pinpoint.banner.configs"), logger);
banner.setPinpointBannerMode(PinpointBanner.Mode.valueOf(profilerConfig.readString("pinpoint.banner.mode", "CONSOLE").toUpperCase()));
banner.setPinpointBannerProperty(properties);
banner.printBanner();
} catch (Exception e) {
// unexpected exception that did not be checked above
logger.warn(ProductInfo.NAME + " start failed.", e);
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions agent-module/profiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-banner</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.navercorp.pinpoint.profiler;

import com.navercorp.pinpoint.ProductInfo;
import com.navercorp.pinpoint.banner.Banner;
import com.navercorp.pinpoint.banner.Mode;
import com.navercorp.pinpoint.banner.PinpointBanner;
import com.navercorp.pinpoint.bootstrap.Agent;
import com.navercorp.pinpoint.bootstrap.AgentOption;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
Expand All @@ -37,6 +40,7 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
Expand Down Expand Up @@ -158,8 +162,18 @@ public void start() {
return;
}
}

logger.info("Starting {} Agent.", ProductInfo.NAME);
this.applicationContext.start();
printBanner();
}

Check warning on line 169 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java#L168-L169

Added lines #L168 - L169 were not covered by tests

private void printBanner() {
List<String> dumpKeys = profilerConfig.readList("pinpoint.banner.configs");
Mode mode = Mode.valueOf(profilerConfig.readString("pinpoint.banner.mode", "CONSOLE").toUpperCase());
Properties properties = profilerConfig.getProperties();
final Banner banner = new PinpointBanner(mode, dumpKeys, properties::getProperty, logger::info);
banner.printBanner();

Check warning on line 176 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/DefaultAgent.java#L172-L176

Added lines #L172 - L176 were not covered by tests
}

@Override
Expand Down
1 change: 1 addition & 0 deletions banner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# pinpoint-banner
39 changes: 39 additions & 0 deletions banner/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint</artifactId>
<version>3.1.0-SNAPSHOT</version>
</parent>

<artifactId>pinpoint-banner</artifactId>
<packaging>jar</packaging>

<dependencies>

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


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.navercorp.pinpoint.banner;

class BannerVersionTemplate {

Check warning on line 3 in banner/src/main/java-templates/com/navercorp/pinpoint/banner/BannerVersionTemplate.java

View check run for this annotation

Codecov / codecov/patch

banner/src/main/java-templates/com/navercorp/pinpoint/banner/BannerVersionTemplate.java#L3

Added line #L3 was not covered by tests
static final String VERSION = "${project.version}";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.navercorp.pinpoint.banner;

public interface Banner {
void printBanner();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.navercorp.pinpoint.banner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

class BannerUtils {

Check warning on line 11 in banner/src/main/java/com/navercorp/pinpoint/banner/BannerUtils.java

View check run for this annotation

Codecov / codecov/patch

banner/src/main/java/com/navercorp/pinpoint/banner/BannerUtils.java#L11

Added line #L11 was not covered by tests

static String readAllString(InputStream inputStream, Charset charset) throws IOException {
try (Reader inputStreamReader = new InputStreamReader(inputStream, charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
StringBuilder buffer = new StringBuilder(64);
String line;
while ((line = bufferedReader.readLine()) != null) {
buffer.append(line);
buffer.append(System.lineSeparator());
}
return buffer.toString();
}
}

static String banner() {
return banner("/pinpoint-banner/banner.txt");
}

private static String banner(String bannerFile) {
try (InputStream inputStream = BannerUtils.class.getResourceAsStream(bannerFile)) {
return BannerUtils.readAllString(inputStream, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("banner IO failed", e);

Check warning on line 34 in banner/src/main/java/com/navercorp/pinpoint/banner/BannerUtils.java

View check run for this annotation

Codecov / codecov/patch

banner/src/main/java/com/navercorp/pinpoint/banner/BannerUtils.java#L33-L34

Added lines #L33 - L34 were not covered by tests
}
}
}
7 changes: 7 additions & 0 deletions banner/src/main/java/com/navercorp/pinpoint/banner/Mode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.navercorp.pinpoint.banner;

public enum Mode {
OFF,
CONSOLE,
LOG;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.navercorp.pinpoint.banner;

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

public class PinpointBanner implements Banner {
private final Mode bannerMode;
private final Collection<String> dumpKeys;
private final Function<String, String> properties;
private final Consumer<String> consoleWriter = System.out::println;
private final Consumer<String> loggerWriter;

public PinpointBanner(Mode bannerMode,
Collection<String> dumpKeys,
Function<String, String> properties,
Consumer<String> loggerWriter) {
this.bannerMode = Objects.requireNonNull(bannerMode, "bannerMode");
this.dumpKeys = Objects.requireNonNull(dumpKeys, "dumpKeys");
this.properties = Objects.requireNonNull(properties, "properties");
this.loggerWriter = Objects.requireNonNull(loggerWriter, "loggerWriter");
}


@Override
public void printBanner() {
switch (bannerMode) {
case OFF:
return;

Check warning on line 30 in banner/src/main/java/com/navercorp/pinpoint/banner/PinpointBanner.java

View check run for this annotation

Codecov / codecov/patch

banner/src/main/java/com/navercorp/pinpoint/banner/PinpointBanner.java#L30

Added line #L30 was not covered by tests
case LOG:
loggerWriter.accept(buildBannerString());
return;
default:
consoleWriter.accept(buildBannerString());

Check warning on line 35 in banner/src/main/java/com/navercorp/pinpoint/banner/PinpointBanner.java

View check run for this annotation

Codecov / codecov/patch

banner/src/main/java/com/navercorp/pinpoint/banner/PinpointBanner.java#L35

Added line #L35 was not covered by tests
}
}

Check warning on line 37 in banner/src/main/java/com/navercorp/pinpoint/banner/PinpointBanner.java

View check run for this annotation

Codecov / codecov/patch

banner/src/main/java/com/navercorp/pinpoint/banner/PinpointBanner.java#L37

Added line #L37 was not covered by tests

private String buildBannerString() {
StringBuilder banner = new StringBuilder(128);
banner.append(BannerUtils.banner());
banner.append(format("Pinpoint Version", BannerVersionTemplate.VERSION));
banner.append(System.lineSeparator());

for (String key : dumpKeys) {
String value = properties.apply(key);
if (value != null) {
banner.append(format(key, value));
banner.append(System.lineSeparator());
}
}
return banner.toString();
}

protected String format(String key, String value) {
return String.format(" :: %55s :: %35s", key, value);
}

}
9 changes: 9 additions & 0 deletions banner/src/main/resources/pinpoint-banner/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

88888888ba 88 888b 88 88888888ba ,ad8888ba, 88 888b 88 888888888888
88 ,8P 88 88 `8b 88 88 ,8P d8' `8b 88 88 `8b 88 88
88aaaaaa8P' 88 88 `8b 88 88aaaaaa8P' 88 da 88 88 88 `8b 88 88
88 88 88 `8b 88 88 Y8, ,8P 88 88 `8b 88 88
88 88 88 `888 88 `"Y8888Y"' 88 88 `888 88

https://github.com/pinpoint-apm/pinpoint

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.navercorp.pinpoint.banner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;


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

@Test
void banner() {
String banner = BannerUtils.banner();
logger.debug("--------");
logger.debug(banner);
logger.debug("--------");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.navercorp.pinpoint.banner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

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

@Test
void printBanner() {
Properties properties = new Properties();
properties.put("aaa", "111");
properties.put("bbb", "222");
List<String> dumpKeys = keyList(properties);

Banner banner = new PinpointBanner(Mode.LOG, dumpKeys,
properties::getProperty, logger::info);
banner.printBanner();
}

private List<String> keyList(Properties properties) {
@SuppressWarnings("unchecked")
Enumeration<String> enumeration = (Enumeration<String>) properties.propertyNames();
return Collections.list(enumeration);
}
}
23 changes: 23 additions & 0 deletions banner/src/test/resources/log4j2-test.xml
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{nolookups}%n</Property>
<Property name="file_message_pattern">%d{MM-dd HH:mm:ss.sss} [%15.15t] %-5level %-40.40logger{1.}:%3L -- %msg{nolookups}%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>
Loading

0 comments on commit f4c92e5

Please sign in to comment.