Skip to content

Commit a0b6427

Browse files
authored
Add ADFActivityRun support (#41)
* Add ADFActivityRunType and unit test it * Implement ADFActivityRun to NLFPlugin.syslogMessage * Close InputStream and JsonReader in ADFActivityRunTypeTest * Fix bad resource names in example adfactivityrun JSON file * Spotless:apply
1 parent 2fdde35 commit a0b6427

6 files changed

Lines changed: 566 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public List<SyslogMessage> syslogMessage(final ParsedEvent parsedEvent) throws P
9797
if (
9898
jsonObject.containsKey("Type") && jsonObject.get("Type").getValueType().equals(JsonValue.ValueType.STRING)
9999
) {
100-
if (jsonObject.getString("Type").equals("ADFPipelineRun")) {
100+
if (jsonObject.getString("Type").equals("ADFActivityRun")) {
101+
eventTypes.add(new ADFActivityRunType(parsedEvent, realHostname));
102+
}
103+
else if (jsonObject.getString("Type").equals("ADFPipelineRun")) {
101104
eventTypes.add(new ADFPipelineRunType(parsedEvent, realHostname));
102105
}
103106
else if (jsonObject.getString("Type").equals("AppTraces")) {
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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.ValidRFC5424AppName;
55+
import com.teragrep.nlf_01.util.ValidRFC5424Hostname;
56+
import com.teragrep.nlf_01.util.ValidRFC5424Timestamp;
57+
import com.teragrep.rlo_14.Facility;
58+
import com.teragrep.rlo_14.SDElement;
59+
import com.teragrep.rlo_14.Severity;
60+
import jakarta.json.JsonObject;
61+
import jakarta.json.JsonValue;
62+
import java.time.Instant;
63+
import java.util.HashSet;
64+
import java.util.Set;
65+
import java.util.UUID;
66+
67+
public final class ADFActivityRunType implements EventType {
68+
69+
private final ParsedEvent parsedEvent;
70+
private final String realHostname;
71+
72+
public ADFActivityRunType(final ParsedEvent parsedEvent, final String realHostname) {
73+
this.parsedEvent = parsedEvent;
74+
this.realHostname = realHostname;
75+
}
76+
77+
private void assertKey(final JsonObject obj, final String key, final JsonValue.ValueType type)
78+
throws PluginException {
79+
if (!obj.containsKey(key)) {
80+
throw new PluginException(new IllegalArgumentException("Key " + key + " does not exist"));
81+
}
82+
83+
if (!obj.get(key).getValueType().equals(type)) {
84+
throw new PluginException(new IllegalArgumentException("Key " + key + " is not of type " + type));
85+
}
86+
}
87+
88+
@Override
89+
public Severity severity() {
90+
return Severity.NOTICE;
91+
}
92+
93+
@Override
94+
public Facility facility() {
95+
return Facility.AUDIT;
96+
}
97+
98+
@Override
99+
public String hostname() throws PluginException {
100+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
101+
102+
assertKey(record, "_ResourceId", JsonValue.ValueType.STRING);
103+
final String resourceId = record.getString("_ResourceId");
104+
105+
return new ValidRFC5424Hostname(
106+
"md5-".concat(new MD5Hash(resourceId).md5().concat("-").concat(new ASCIIString(new ResourceId(resourceId).resourceName()).withNonAsciiCharsRemoved()))
107+
).hostnameWithInvalidCharsRemoved();
108+
109+
}
110+
111+
@Override
112+
public String appName() throws PluginException {
113+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
114+
115+
assertKey(record, "PipelineName", JsonValue.ValueType.STRING);
116+
117+
return new ValidRFC5424AppName(record.getString("PipelineName")).validAppName();
118+
119+
}
120+
121+
@Override
122+
public long timestamp() throws PluginException {
123+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
124+
125+
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
126+
127+
return new ValidRFC5424Timestamp(record.getString("TimeGenerated")).validTimestamp();
128+
}
129+
130+
@Override
131+
public Set<SDElement> sdElements() {
132+
final Set<SDElement> elems = new HashSet<>();
133+
String time = "";
134+
if (!parsedEvent.enqueuedTimeUtc().isStub()) {
135+
time = parsedEvent.enqueuedTimeUtc().zonedDateTime().toString();
136+
}
137+
138+
String fullyQualifiedNamespace = "";
139+
String eventHubName = "";
140+
String partitionId = "";
141+
String consumerGroup = "";
142+
if (!parsedEvent.partitionCtx().isStub()) {
143+
fullyQualifiedNamespace = String
144+
.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("FullyQualifiedNamespace", ""));
145+
eventHubName = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("EventHubName", ""));
146+
partitionId = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("PartitionId", ""));
147+
consumerGroup = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("ConsumerGroup", ""));
148+
}
149+
150+
elems
151+
.add(new SDElement("aer_02_partition@48577").addSDParam("fully_qualified_namespace", fullyQualifiedNamespace).addSDParam("eventhub_name", eventHubName).addSDParam("partition_id", partitionId).addSDParam("consumer_group", consumerGroup));
152+
153+
elems
154+
.add(new SDElement("event_id@48577").addSDParam("uuid", UUID.randomUUID().toString()).addSDParam("hostname", realHostname).addSDParam("unixtime", Instant.now().toString()).addSDParam("id_source", "aer_02"));
155+
156+
String partitionKey = "";
157+
if (!parsedEvent.systemProperties().isStub()) {
158+
partitionKey = String.valueOf(parsedEvent.systemProperties().asMap().getOrDefault("PartitionKey", ""));
159+
}
160+
161+
String offset = "";
162+
if (!parsedEvent.offset().isStub()) {
163+
offset = parsedEvent.offset().value();
164+
}
165+
166+
elems
167+
.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()));
168+
169+
elems
170+
.add(new SDElement("aer_02@48577").addSDParam("timestamp_source", time.isEmpty() ? "generated" : "timeEnqueued"));
171+
172+
elems.add(new SDElement("nlf_01@48577").addSDParam("eventType", this.getClass().getSimpleName()));
173+
174+
return elems;
175+
}
176+
177+
@Override
178+
public String msgId() {
179+
String sequenceNumber = "";
180+
if (!parsedEvent.systemProperties().isStub()) {
181+
sequenceNumber = String.valueOf(parsedEvent.systemProperties().asMap().getOrDefault("SequenceNumber", ""));
182+
}
183+
return sequenceNumber;
184+
}
185+
186+
@Override
187+
public String msg() {
188+
return parsedEvent.asString();
189+
}
190+
}

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import com.teragrep.akv_01.plugin.PluginException;
6262
import com.teragrep.nlf_01.fakes.EmptySourceable;
6363
import com.teragrep.nlf_01.fakes.FakeSourceable;
64-
import com.teragrep.nlf_01.types.ADFPipelineRunType;
6564
import com.teragrep.nlf_01.types.*;
6665
import com.teragrep.rlo_14.*;
6766
import org.junit.jupiter.api.Assertions;
@@ -346,6 +345,37 @@ void syslogType() {
346345
Assertions.assertTrue(sdElementMap.get("aer_02_event@48577").containsKey("properties"));
347346
}
348347

348+
@Test
349+
void adfActivityRunType() {
350+
final String json = Assertions
351+
.assertDoesNotThrow(() -> Files.readString(Paths.get("src/test/resources/adfactivityrun.json")));
352+
final ParsedEvent parsedEvent = new ParsedEventFactory(
353+
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"))
354+
).parsedEvent();
355+
356+
final NLFPlugin plugin = new NLFPlugin(new FakeSourceable());
357+
final List<SyslogMessage> syslogMessages = Assertions
358+
.assertDoesNotThrow(() -> plugin.syslogMessage(parsedEvent));
359+
Assertions.assertEquals(1, syslogMessages.size());
360+
361+
final SyslogMessage syslogMessage = syslogMessages.get(0);
362+
Assertions.assertEquals("md5-0ded52ef915af563e25778bf26b0f129-resourceName", syslogMessage.getHostname());
363+
Assertions.assertEquals("Pipeline-1", syslogMessage.getAppName());
364+
Assertions.assertEquals("2025-10-06T00:00:00Z", syslogMessage.getTimestamp());
365+
Assertions.assertEquals(json, syslogMessage.getMsg());
366+
final Map<String, Map<String, String>> sdElementMap = syslogMessage
367+
.getSDElements()
368+
.stream()
369+
.collect(Collectors.toMap((SDElement::getSdID), (sdElem) -> sdElem.getSdParams().stream().collect(Collectors.toMap(SDParam::getParamName, SDParam::getParamValue))));
370+
371+
Assertions.assertEquals(1, sdElementMap.get("nlf_01@48577").size());
372+
Assertions
373+
.assertEquals(ADFActivityRunType.class.getSimpleName(), sdElementMap.get("nlf_01@48577").get("eventType"));
374+
375+
Assertions.assertEquals(4, sdElementMap.get("aer_02_event@48577").size());
376+
Assertions.assertTrue(sdElementMap.get("aer_02_event@48577").containsKey("properties"));
377+
}
378+
349379
@Test
350380
void testPostgreSQLType() {
351381
final String json = Assertions

0 commit comments

Comments
 (0)