Skip to content

Commit 2866def

Browse files
authored
AppEvents support (#68)
* Add AppEventsType objects and unit tests * Add AppEventsType to NLFPlugin.syslogMessage and test it * Use Fakes for AppEventsTypeTest.testIdealCase
1 parent 151b936 commit 2866def

6 files changed

Lines changed: 600 additions & 0 deletions

File tree

src/main/java/com/teragrep/nlf_01/NLFPlugin.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public List<SyslogMessage> syslogMessage(final ParsedEvent parsedEvent) throws P
103103
else if (jsonObject.getString("Type").equals("ADFPipelineRun")) {
104104
eventTypes.add(new ADFPipelineRunType(parsedEvent, realHostname));
105105
}
106+
else if (jsonObject.getString("Type").equals("AppEvents")) {
107+
eventTypes.add(new AppEventsType(parsedEvent, realHostname));
108+
}
106109
else if (jsonObject.getString("Type").equals("AppServiceConsoleLogs")) {
107110
eventTypes.add(new AppServiceConsoleLogsType(parsedEvent, realHostname));
108111
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* Teragrep Neon log format plugin for AKV_01
3+
* Copyright (C) 2025 Suomen Kanuuna Oy
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
*
19+
* Additional permission under GNU Affero General Public License version 3
20+
* section 7
21+
*
22+
* If you modify this Program, or any covered work, by linking or combining it
23+
* with other code, such other code is not for that reason alone subject to any
24+
* of the requirements of the GNU Affero GPL version 3 as long as this Program
25+
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
26+
* modifications.
27+
*
28+
* Supplemented terms under GNU Affero General Public License version 3
29+
* section 7
30+
*
31+
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
32+
* versions must be marked as "Modified version of" The Program.
33+
*
34+
* Names of the licensors and authors may not be used for publicity purposes.
35+
*
36+
* No rights are granted for use of trade names, trademarks, or service marks
37+
* which are in The Program if any.
38+
*
39+
* Licensee must indemnify licensors and authors for any liability that these
40+
* contractual assumptions impose on licensors and authors.
41+
*
42+
* To the extent this program is licensed as part of the Commercial versions of
43+
* Teragrep, the applicable Commercial License may apply to this file if you as
44+
* a licensee so wish it.
45+
*/
46+
package com.teragrep.nlf_01.types;
47+
48+
import com.teragrep.akv_01.event.ParsedEvent;
49+
import com.teragrep.akv_01.plugin.PluginException;
50+
import com.teragrep.nlf_01.PropertiesJson;
51+
import com.teragrep.nlf_01.util.ASCIIString;
52+
import com.teragrep.nlf_01.util.MD5Hash;
53+
import com.teragrep.nlf_01.util.ResourceId;
54+
import com.teragrep.nlf_01.util.ValidKey;
55+
import com.teragrep.nlf_01.util.ValidRFC5424AppName;
56+
import com.teragrep.nlf_01.util.ValidRFC5424Hostname;
57+
import com.teragrep.nlf_01.util.ValidRFC5424Timestamp;
58+
import com.teragrep.nlf_01.util.ValidStringKey;
59+
import com.teragrep.rlo_14.Facility;
60+
import com.teragrep.rlo_14.SDElement;
61+
import com.teragrep.rlo_14.Severity;
62+
import jakarta.json.JsonObject;
63+
import java.time.Instant;
64+
import java.util.HashSet;
65+
import java.util.Set;
66+
import java.util.UUID;
67+
68+
public final class AppEventsType implements EventType {
69+
70+
private final ParsedEvent parsedEvent;
71+
private final String realHostname;
72+
73+
public AppEventsType(final ParsedEvent parsedEvent, final String realHostname) {
74+
this.parsedEvent = parsedEvent;
75+
this.realHostname = realHostname;
76+
}
77+
78+
@Override
79+
public Severity severity() {
80+
return Severity.NOTICE;
81+
}
82+
83+
@Override
84+
public Facility facility() {
85+
return Facility.AUDIT;
86+
}
87+
88+
@Override
89+
public String hostname() throws PluginException {
90+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
91+
92+
final ValidKey<String> validKey = new ValidStringKey(record, "_ResourceId");
93+
94+
final String resourceId = validKey.value();
95+
96+
return new ValidRFC5424Hostname(
97+
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
98+
).hostnameWithInvalidCharsRemoved();
99+
100+
}
101+
102+
@Override
103+
public String appName() throws PluginException {
104+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
105+
106+
return new ValidRFC5424AppName(new ValidStringKey(record, "Type").value()).appName();
107+
}
108+
109+
@Override
110+
public long timestamp() throws PluginException {
111+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
112+
113+
final ValidKey<String> validKey = new ValidStringKey(record, "TimeGenerated");
114+
115+
return new ValidRFC5424Timestamp(validKey.value()).validTimestamp();
116+
}
117+
118+
@Override
119+
public Set<SDElement> sdElements() throws PluginException {
120+
final Set<SDElement> elems = new HashSet<>();
121+
final String time;
122+
if (!parsedEvent.enqueuedTimeUtc().isStub()) {
123+
time = parsedEvent.enqueuedTimeUtc().zonedDateTime().toString();
124+
}
125+
else {
126+
time = "";
127+
}
128+
129+
final String fullyQualifiedNamespace;
130+
final String eventHubName;
131+
final String partitionId;
132+
final String consumerGroup;
133+
if (!parsedEvent.partitionCtx().isStub()) {
134+
fullyQualifiedNamespace = String
135+
.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("FullyQualifiedNamespace", ""));
136+
eventHubName = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("EventHubName", ""));
137+
partitionId = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("PartitionId", ""));
138+
consumerGroup = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("ConsumerGroup", ""));
139+
}
140+
else {
141+
fullyQualifiedNamespace = "";
142+
eventHubName = "";
143+
partitionId = "";
144+
consumerGroup = "";
145+
}
146+
147+
elems
148+
.add(new SDElement("aer_02_partition@48577").addSDParam("fully_qualified_namespace", fullyQualifiedNamespace).addSDParam("eventhub_name", eventHubName).addSDParam("partition_id", partitionId).addSDParam("consumer_group", consumerGroup));
149+
150+
elems
151+
.add(new SDElement("event_id@48577").addSDParam("uuid", UUID.randomUUID().toString()).addSDParam("hostname", realHostname).addSDParam("unixtime", Instant.now().toString()).addSDParam("id_source", "aer_02"));
152+
153+
final String partitionKey;
154+
if (!parsedEvent.systemProperties().isStub()) {
155+
partitionKey = String.valueOf(parsedEvent.systemProperties().asMap().getOrDefault("PartitionKey", ""));
156+
}
157+
else {
158+
partitionKey = "";
159+
}
160+
161+
final String offset;
162+
if (!parsedEvent.offset().isStub()) {
163+
offset = parsedEvent.offset().value();
164+
}
165+
else {
166+
offset = "";
167+
}
168+
169+
elems
170+
.add(new SDElement("aer_02_event@48577").addSDParam("offset", offset).addSDParam("enqueued_time", time).addSDParam("partition_key", partitionKey).addSDParam("properties", new PropertiesJson(parsedEvent.properties()).toJsonObject().toString()));
171+
172+
elems
173+
.add(new SDElement("aer_02@48577").addSDParam("timestamp_source", time.isEmpty() ? "generated" : "timeEnqueued"));
174+
175+
elems.add(new SDElement("nlf_01@48577").addSDParam("eventType", this.getClass().getSimpleName()));
176+
177+
return elems;
178+
}
179+
180+
@Override
181+
public String msgId() throws PluginException {
182+
final String sequenceNumber;
183+
if (!parsedEvent.systemProperties().isStub()) {
184+
sequenceNumber = String.valueOf(parsedEvent.systemProperties().asMap().getOrDefault("SequenceNumber", ""));
185+
}
186+
else {
187+
sequenceNumber = "";
188+
}
189+
return sequenceNumber;
190+
}
191+
192+
@Override
193+
public String msg() throws PluginException {
194+
return parsedEvent.asString();
195+
}
196+
}

src/test/java/com/teragrep/nlf_01/NLFPluginTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,66 @@ void containerAppConsoleLogsTypeWithJobName() {
544544
Assertions.assertTrue(sdElementMap.get("aer_02_event@48577").containsKey("properties"));
545545
}
546546

547+
@Test
548+
void appEventsTypeTest() {
549+
final String json = Assertions
550+
.assertDoesNotThrow(() -> Files.readString(Paths.get("src/test/resources/appevents.json")));
551+
final ParsedEvent parsedEvent = new ParsedEventFactory(
552+
new UnparsedEventImpl(json, new EventPartitionContextImpl(new HashMap<>()), new EventPropertiesImpl(new HashMap<>()), new EventSystemPropertiesImpl(new HashMap<>()), new EnqueuedTimeImpl("2020-01-01T00:00:00"), new EventOffsetImpl("0"))
553+
).parsedEvent();
554+
555+
final NLFPlugin plugin = new NLFPlugin(new FakeSourceable());
556+
final List<SyslogMessage> syslogMessages = Assertions
557+
.assertDoesNotThrow(() -> plugin.syslogMessage(parsedEvent));
558+
Assertions.assertEquals(1, syslogMessages.size());
559+
560+
final SyslogMessage syslogMessage = syslogMessages.get(0);
561+
Assertions
562+
.assertEquals(
563+
"{\n" + " \"AppRoleInstance\": \"app-role-instance\",\n"
564+
+ " \"AppRoleName\": \"app-role-name\",\n" + " \"ClientBrowser\": \"Browser\",\n"
565+
+ " \"ClientCity\": \"City\",\n" + " \"ClientCountryOrRegion\": \"Country\",\n"
566+
+ " \"ClientIP\": \"192.168.1.2\",\n" + " \"ClientModel\": \"CModel\",\n"
567+
+ " \"ClientOS\": \"OperatingSystem\",\n" + " \"ClientStateOrProvince\": \"State\",\n"
568+
+ " \"ClientType\": \"client-type\",\n" + " \"IKey\": \"i-key\",\n"
569+
+ " \"ItemCount\": 1,\n" + " \"Measurements\": \"{}\",\n"
570+
+ " \"Name\": \"Human Readable Name\",\n" + " \"OperationId\": \"123\",\n"
571+
+ " \"ParentId\": \"456\",\n" + " \"Properties\": {\n"
572+
+ " \"ProcessId\":\"1234\",\n" + " \"HostInstanceId\":\"123456\",\n"
573+
+ " \"prop__{OriginalFormat}\":\"abc\",\n" + " \"prop__RouteName\":\"xyz\",\n"
574+
+ " \"LogLevel\":\"Debug\",\n" + " \"EventId\":\"1\",\n"
575+
+ " \"prop__RouteTemplate\":\"route/template\",\n"
576+
+ " \"Category\":\"192.168.3.1\",\n" + " \"EventName\":\"event-name\"\n"
577+
+ " },\n" + " \"ResourceGUID\": \"123456789\",\n"
578+
+ " \"SDKVersion\": \"12: 192.168.x.x\",\n" + " \"SessionId\": \"12: 192.168.x.x\",\n"
579+
+ " \"SourceSystem\": \"Azure\",\n" + " \"SyntheticSource\": \"Source\",\n"
580+
+ " \"TenantId\": \"bb41a487-309b-4d21-9ab8-2a8b948b2d18\",\n"
581+
+ " \"TimeGenerated\": \"2020-01-01T01:02:34.5678999Z\",\n"
582+
+ " \"Type\": \"AppEvents\",\n"
583+
+ " \"UserAccountId\": \"bb41a487-309b-4d21-9ab8-2a8b948b2d18\",\n"
584+
+ " \"UserAuthenticatedId\": \"bb41a487-309b-4d21-9ab8-2a8b948b2d18\",\n"
585+
+ " \"UserId\": \"bb41a487-309b-4d21-9ab8-2a8b948b2d18\",\n"
586+
+ " \"_BilledSize\": 1,\n" + " \"_ItemId\": \"12-34-56-78\",\n"
587+
+ " \"_Internal_WorkspaceResourceId\": \"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}\",\n"
588+
+ " \"_ResourceId\": \"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}\"\n"
589+
+ "}",
590+
syslogMessage.getMsg()
591+
);
592+
Assertions.assertEquals("md5-0ded52ef915af563e25778bf26b0f129-resourceName", syslogMessage.getHostname());
593+
Assertions.assertEquals("AppEvents", syslogMessage.getAppName());
594+
Assertions.assertEquals("2020-01-01T01:02:34.567Z", syslogMessage.getTimestamp());
595+
596+
final Map<String, Map<String, String>> sdElementMap = syslogMessage
597+
.getSDElements()
598+
.stream()
599+
.collect(Collectors.toMap((SDElement::getSdID), (sdElem) -> sdElem.getSdParams().stream().collect(Collectors.toMap(SDParam::getParamName, SDParam::getParamValue))));
600+
601+
Assertions.assertEquals(1, sdElementMap.get("nlf_01@48577").size());
602+
Assertions.assertEquals(AppEventsType.class.getSimpleName(), sdElementMap.get("nlf_01@48577").get("eventType"));
603+
604+
Assertions.assertTrue(sdElementMap.get("aer_02_event@48577").containsKey("properties"));
605+
}
606+
547607
@Test
548608
void appServiceConsoleLogsType() {
549609
final String json = Assertions

0 commit comments

Comments
 (0)