Skip to content

Commit

Permalink
Fix and enhance resource detection logging. (#711)
Browse files Browse the repository at this point in the history
I learned (the hard way) today that jul doesn't expand numeric
placeholders in string templates if they are surrounded by single
quotes. Yup.

* `logger.log(INFO, "{0}", "test")` -> `test`
* `logger.log(INFO, "'{0}'", "test")` -> `{0}`

So this results in pretty useless logs here. I also used this as an
opportunity to enhance the details about when/why the resource provider
doesn't kick in.
  • Loading branch information
breedx-splk committed Jan 31, 2023
1 parent e8346d6 commit d54b9ea
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public String detect() throws Exception {
}

if (!dirTool.isDirectory(deploymentDir)) {
logger.log(FINE, "Deployment dir '{0}' doesn't exist.", deploymentDir);
logger.log(FINE, "Deployment dir {0} doesn't exist.", deploymentDir);
return null;
}

logger.log(FINE, "Looking for deployments in '{0}'.", deploymentDir);
logger.log(FINE, "Looking for deployments in {0}.", deploymentDir);
try (Stream<Path> stream = dirTool.list(deploymentDir)) {
return stream.map(this::detectName).filter(Objects::nonNull).findFirst().orElse(null);
}
Expand All @@ -62,11 +62,11 @@ public String detect() throws Exception {
@Nullable
private String detectName(Path path) {
if (!appServer.isValidAppName(path)) {
logger.log(FINE, "Skipping '{0}'.", path);
logger.log(FINE, "Skipping {0}.", path);
return null;
}

logger.log(FINE, "Attempting service name detection in '{0}'.", path);
logger.log(FINE, "Attempting service name detection in {0}.", path);
String name = path.getFileName().toString();
if (dirTool.isDirectory(path)) {
return parseBuddy.handleExplodedApp(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package io.opentelemetry.resourceproviders;

import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -37,11 +39,14 @@ public AppServerServiceNameProvider() {
@Override
public Resource createResource(ConfigProperties config) {
String serviceName = detectServiceName();
if (serviceName != null) {
logger.log(INFO, "Auto-detected service name '{0}'.", serviceName);
return Resource.create(Attributes.of(SERVICE_NAME, serviceName));
if (serviceName == null) {
logger.log(
WARNING,
"Service name could not be detected using common application server strategies.");
return Resource.empty();
}
return Resource.empty();
logger.log(INFO, "Auto-detected service name {0}.", serviceName);
return Resource.create(Attributes.of(SERVICE_NAME, serviceName));
}

@Nullable
Expand All @@ -58,10 +63,30 @@ private String detectServiceName() {
@Override
public boolean shouldApply(ConfigProperties config, Resource existing) {
String serviceName = config.getString("otel.service.name");
if (serviceName != null) {
logger.log(
FINE,
"Skipping AppServerServiceName detection, otel.service.name is already set to {0}",
serviceName);
return false;
}
Map<String, String> resourceAttributes = config.getMap("otel.resource.attributes");
return serviceName == null
&& !resourceAttributes.containsKey(SERVICE_NAME.getKey())
&& "unknown_service:java".equals(existing.getAttribute(SERVICE_NAME));
if (resourceAttributes.containsKey(SERVICE_NAME.getKey())) {
logger.log(
FINE,
"Skipping AppServerServiceName detection, otel.resource.attributes already contains {0}",
resourceAttributes.get(SERVICE_NAME.getKey()));
return false;
}
String existingName = existing.getAttribute(SERVICE_NAME);
if (!"unknown_service:java".equals(existingName)) {
logger.log(
FINE,
"Skipping AppServerServiceName detection, resource already contains {0}",
existingName);
return false;
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public Path getDeploymentDir() {
// would be located in /dir/webapps.

String programArguments = System.getProperty("sun.java.command");
logger.log(FINE, "Started with arguments '{0}'.", programArguments);
logger.log(FINE, "Started with arguments {0}.", programArguments);
if (programArguments != null) {
Path jettyBase = parseJettyBase(programArguments);
if (jettyBase != null) {
logger.log(FINE, "Using jetty.base '{0}'.", jettyBase);
logger.log(FINE, "Using jetty.base {0}.", jettyBase);
return jettyBase.resolve("webapps");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Path getDeploymentDir() {
if (logger.isLoggable(FINE)) {
logger.log(
FINE,
"Using WLP_USER_DIR '{0}', WLP_OUTPUT_DIR '{1}'.",
"Using WLP_USER_DIR {0}, WLP_OUTPUT_DIR {1}.",
new Object[] {wlpUserDir, wlpOutputDir});
}
if (wlpUserDir != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String detect() throws Exception {
}

String programArguments = System.getProperty("sun.java.command");
logger.log(FINE, "Started with arguments '{0}'.", programArguments);
logger.log(FINE, "Started with arguments {0}.", programArguments);
if (programArguments == null) {
return null;
}
Expand All @@ -53,7 +53,7 @@ public String detect() throws Exception {
// in docker image it is /opt/IBM/WebSphere/AppServer/profiles/AppSrv01/config
Path configDirectory = Paths.get(matcher.group(2));
if (!Files.isDirectory(configDirectory)) {
logger.log(FINE, "Missing configuration directory '{0}'.", configDirectory);
logger.log(FINE, "Missing configuration directory {0}.", configDirectory);
return null;
}

Expand All @@ -64,7 +64,7 @@ public String detect() throws Exception {
if (logger.isLoggable(FINE)) {
logger.log(
FINE,
"Parsed arguments: cell '{0}', node '{1}', server '{2}', configuration directory '{3}'.",
"Parsed arguments: cell {0}, node {1}, server {2}, configuration directory {3}.",
new Object[] {cell, node, server, configDirectory});
}

Expand All @@ -78,17 +78,17 @@ public String detect() throws Exception {
}
Path cellApplications = parent.resolve("installedApps").resolve(cell);
if (Files.isDirectory(cellApplications)) {
logger.log(FINE, "Looking for deployments in '{0}'.", cellApplications);
logger.log(FINE, "Looking for deployments in {0}.", cellApplications);

try (Stream<Path> stream = Files.list(cellApplications)) {
for (Path path : stream.collect(Collectors.toList())) {
String fullName = path.getFileName().toString();
// websphere deploys all applications as ear
if (!fullName.endsWith(".ear") || !appServer.isValidAppName(path)) {
logger.log(FINE, "Skipping '{0}'.", path);
logger.log(FINE, "Skipping {0}.", path);
continue;
}
logger.log(FINE, "Attempting service name detection in '{0}'.", path);
logger.log(FINE, "Attempting service name detection in {0}.", path);

// strip ear suffix
String name = fullName.substring(0, fullName.length() - 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WildflyAppServer implements AppServer {
@Override
public Path getDeploymentDir() throws URISyntaxException {
String programArguments = System.getProperty("sun.java.command");
logger.log(FINE, "Started with arguments '{0}'.", programArguments);
logger.log(FINE, "Started with arguments {0}.", programArguments);
if (programArguments == null) {
return null;
}
Expand All @@ -42,7 +42,7 @@ public Path getDeploymentDir() throws URISyntaxException {
// environment variable JBOSS_BASE_DIR to avoid parsing program arguments
String jbossBaseDir = System.getenv("JBOSS_BASE_DIR");
if (jbossBaseDir != null) {
logger.log(FINE, "Using JBOSS_BASE_DIR '{0}'.", jbossBaseDir);
logger.log(FINE, "Using JBOSS_BASE_DIR {0}.", jbossBaseDir);
return Paths.get(jbossBaseDir, "deployments");
}

Expand Down

0 comments on commit d54b9ea

Please sign in to comment.