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

Add Netty versions to the self-diagnostics #2716

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public void init() {
if (startupLogger.isDebugEnabled()) {
startupLogger.debug("OS: " + System.getProperty("os.name"));
logJavaInfo();
startupLogger.debug("Netty versions: " + NettyVersions.extract());
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.applicationinsights.agent.internal.init;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

class NettyVersions {

// Example: **19** **Group:** `io.netty` **Name:** `netty-codec-dns` **Version:** `4.1.79.Final`
private static final Pattern DEPENDENCY_COORDINATE_PATTERN =
Pattern.compile(".*`(.*)`.*`(.*)`.*`(.*)`");

private NettyVersions() {}

static String extract() {
String moreLicenseResource = "/META-INF/licenses/more-licenses.md";
InputStream moreLicenseAsStream = NettyVersions.class.getResourceAsStream(moreLicenseResource);
if (moreLicenseAsStream == null) {
return moreLicenseResource + " notFound";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when customer is not using netty, i think it's better not to log it at all. 'not found' for something that they don't use is a bit confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

META-INF/licenses/more-licenses.md file is supposed to always exist in Application Insights jar. It contains the versions of some libraries used by Application Insights.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about NettyVersions.class.getResourceAsStream(moreLicenseResource)?

}
return extractNettyVersions(moreLicenseAsStream);
}

private static String extractNettyVersions(InputStream moreLicenseAsStream) {
try (InputStreamReader inputStreamReader =
new InputStreamReader(moreLicenseAsStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
return bufferedReader
.lines()
.filter(line -> line.contains("netty") && line.contains("Group"))
.map(line -> extractDependency(line.trim()))
.collect(Collectors.joining(", "));
} catch (IOException e) {
return e.getMessage();
}
}

private static String extractDependency(String line) {
Matcher matcher = DEPENDENCY_COORDINATE_PATTERN.matcher(line);
boolean matched = matcher.find();
if (!matched) {
return "(" + line + ")";
}
try {
return "(" + matcher.group(1) + ", " + matcher.group(2) + ", " + matcher.group(3) + ")";
} catch (IllegalStateException | IndexOutOfBoundsException e) {
return "(" + e.getMessage() + " for " + line + ")";
}
}
}