Skip to content

Commit 8b89e94

Browse files
Merge pull request #17 from logicmonitor/feature/add-versioning
feature/add-versioning: DEV-70290 : Adding '<platform>/<version>' as…
2 parents fc6623c + 692be00 commit 8b89e94

File tree

9 files changed

+71
-15
lines changed

9 files changed

+71
-15
lines changed

build.gradle

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group "com.logicmonitor"
7-
version "1.2"
7+
version "1.3"
88

99
def jerseyVersion = "2.31"
1010
def junitVersion = "5.6.2"
@@ -18,7 +18,7 @@ repositories {
1818
dependencies {
1919
implementation (
2020
"com.microsoft.azure.functions:azure-functions-java-library:1.4.0",
21-
"com.logicmonitor:lm-logs-sdk-java:1.1",
21+
"com.logicmonitor:lm-logs-sdk-java:1.2",
2222
"com.google.code.gson:gson:2.8.6"
2323
)
2424
testImplementation (
@@ -47,6 +47,13 @@ test {
4747
}
4848
}
4949

50+
jar {
51+
manifest {
52+
attributes 'Implementation-Title': project.getName(),
53+
'Implementation-Version': project.getVersion()
54+
}
55+
}
56+
5057
jar.dependsOn test
5158

5259
def azureAppName = System.properties["azureFunction"] ? System.properties["azureFunction"] : rootProject.name
@@ -78,4 +85,3 @@ task copyPackage(type: Copy) {
7885

7986
copyPackage.dependsOn azureFunctionsPackageZip
8087
build.finalizedBy copyPackage
81-

local.settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
"LogApiClientDebugging": "false",
1515
"LogRegexScrub": "<regex pattern>"
1616
}
17-
}
17+
}

package/lm-logs-azure.zip

579 Bytes
Binary file not shown.

src/main/java/com/logicmonitor/logs/azure/LogEventAdapter.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,16 @@ protected LogEntry createEntry(JsonObject json) {
109109
.map(Instant::getEpochSecond)
110110
.ifPresent(entry::setTimestamp);
111111

112+
// get properties from event if present
113+
Optional<LogEventProperties> properties = Optional.ofNullable(event.getProperties());
114+
112115
// message:
113116
// properties.Msg if present,
114117
// else properties.Description if present,
115118
// otherwise the whole JSON
116-
String message = Optional.ofNullable(event.getProperties().getMsg())
117-
.or(() -> Optional.ofNullable(event.getProperties().getDescription()))
118-
.orElseGet(() -> GSON.toJson(json));
119+
String message = properties.map(LogEventProperties::getMsg)
120+
.or(() -> properties.map(LogEventProperties::getDescription))
121+
.orElseGet(() -> GSON.toJson(json));
119122

120123
if (scrubPattern != null) {
121124
message = scrubPattern.matcher(message).replaceAll("");
@@ -125,4 +128,4 @@ protected LogEntry createEntry(JsonObject json) {
125128
return entry;
126129
}
127130

128-
}
131+
}

src/main/java/com/logicmonitor/logs/azure/LogEventForwarder.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ protected static LMLogsApi configureApi() {
126126
LMLogsApi.Builder builder = new LMLogsApi.Builder()
127127
.withCompany(System.getenv(PARAMETER_COMPANY_NAME))
128128
.withAccessId(System.getenv(PARAMETER_ACCESS_ID))
129-
.withAccessKey(System.getenv(PARAMETER_ACCESS_KEY));
129+
.withAccessKey(System.getenv(PARAMETER_ACCESS_KEY))
130+
.withUserAgentHeader(getUserAgent());
130131
setProperty(PARAMETER_CONNECT_TIMEOUT, Integer::valueOf, builder::withConnectTimeout);
131132
setProperty(PARAMETER_READ_TIMEOUT, Integer::valueOf, builder::withReadTimeout);
132133
setProperty(PARAMETER_DEBUGGING, Boolean::valueOf, builder::withDebugging);
134+
133135
return builder.build();
134136
}
135137

@@ -231,4 +233,27 @@ private static void logResponse(final ExecutionContext context, boolean success,
231233
() -> "Response body: " + response.getData());
232234
}
233235

236+
/**
237+
* gets the gradle 'Implementation-Version'.
238+
* @return the project version
239+
*/
240+
private static String getBuildVersion() {
241+
return LogEventForwarder.class.getPackage().getImplementationVersion();
242+
}
243+
244+
/**
245+
* gets the gradle 'Implementation-Title'.
246+
* @return the project name
247+
*/
248+
private static String getBuildName() {
249+
return LogEventForwarder.class.getPackage().getImplementationTitle();
250+
}
251+
252+
/**
253+
* generates user-agent as <buildname>/<buildversion>.
254+
* @return the user-agent
255+
*/
256+
public static String getUserAgent() {
257+
return getBuildName() + "/" + getBuildVersion();
258+
}
234259
}

src/test/java/com/logicmonitor/logs/azure/LogEventAdapterTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class LogEventAdapterTest {
3636
"vm_catalina.json, 1",
3737
"vm_syslog.json, 1",
3838
"windows_vm_log.json, 1",
39+
"resource_metrics.json, 1"
3940
})
4041
public void testApply(String resourceName, int expectedEntriesCount) {
4142
String events = TestJsonUtils.getFirstJsonString(resourceName);
@@ -54,6 +55,7 @@ public void testApply(String resourceName, int expectedEntriesCount) {
5455
"vm_catalina.json, Msg, . ",
5556
"vm_syslog.json, Msg, \\d ",
5657
"windows_vm_log.json, Description, ",
58+
"resource_metrics.json, , "
5759
})
5860
public void testCreateEntry(String resourceName, String propertyName, String regexScrub) {
5961
JsonObject event = TestJsonUtils.getFirstLogEvent(resourceName);

src/test/java/com/logicmonitor/logs/azure/LogEventForwarderIntegrationTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class LogEventForwarderIntegrationTest extends JerseyTest {
4949
protected static final String TEST_KEY = "testKey";
5050
protected static final String TEST_REQUEST_ID = "testRequestId";
5151
protected static final Pattern TEST_SCRUB_PATTERN = Pattern.compile("\\d");
52-
5352
protected static ExecutionContext mockExecutionContext;
5453

5554
@Path("/rest")

src/test/java/com/logicmonitor/logs/azure/LogEventForwarderTest.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public class LogEventForwarderTest {
3131
@ParameterizedTest
3232
@CsvSource({
3333
", 1, 2, true, . ",
34-
"company, , 0, false, \\d ",
35-
"company, 0, , true, [\\w-.#]+@[\\w-.]+ ",
36-
"company, 333, 4444, false, \\d+\\.\\d+\\.\\d+\\.\\d+ ",
37-
"company, 55555, 666666, false, ",
34+
"company, , 0, false, \\d ",
35+
"company, 0, , true, [\\w-.#]+@[\\w-.]+ ",
36+
"company, 333, 4444, false, \\d+\\.\\d+\\.\\d+\\.\\d+ ",
37+
"company, 55555, 666666, false, ",
3838
})
3939
public void testConfigurationParameters(String companyName, Integer connectTimeout,
4040
Integer readTimeout, Boolean debugging, String regexScrub) throws Exception {
@@ -50,6 +50,7 @@ public void testConfigurationParameters(String companyName, Integer connectTimeo
5050
debugging != null ? debugging.toString() : null)
5151
.and(LogEventForwarder.PARAMETER_REGEX_SCRUB, regexScrub)
5252
.execute(() -> {
53+
5354
LMLogsApi api = LogEventForwarder.configureApi();
5455
LogEventAdapter adapter = LogEventForwarder.configureAdapter();
5556
assertAll(
@@ -62,7 +63,10 @@ public void testConfigurationParameters(String companyName, Integer connectTimeo
6263
() -> assertEquals(debugging != null ? debugging : false,
6364
api.getApiClient().isDebugging()),
6465
() -> assertEquals(regexScrub,
65-
regexScrub != null ? adapter.getScrubPattern().pattern() : adapter.getScrubPattern())
66+
regexScrub != null ? adapter.getScrubPattern().pattern() : adapter.getScrubPattern()),
67+
() -> assertEquals(LogEventForwarder.getUserAgent(),
68+
api.getApiClient().getUserAgent())
69+
6670
);
6771
}
6872
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"records": [
4+
{
5+
"count": 2,
6+
"total": 3,
7+
"minimum": 1,
8+
"maximum": 2,
9+
"average": 1.5,
10+
"resourceId": "/SUBSCRIPTIONS/318382E3-A165-4F0D-8906-01FB4CD06B74/RESOURCEGROUPS/LM-LOGS-WESTINDIA-GROUP/PROVIDERS/MICROSOFT.EVENTHUB/NAMESPACES/LM-LOGS-WESTINDIA",
11+
"time": "2021-03-04T17:30:00.0000000Z",
12+
"metricName": "IncomingRequests",
13+
"timeGrain": "PT1M"
14+
}
15+
]
16+
}
17+
]

0 commit comments

Comments
 (0)