Skip to content

Commit 1ffc70d

Browse files
authored
Function app logs support (#36)
* Implement FunctionAppLogsType and test it - tests based in AppInsightsType's tests * Add FunctionAppLogsType to NLFPlugin.syslogMessage * Make elems HashSet final in FunctionAppLogsType.sdElements * Add FunctionAppLogsType test to NLFPluginTest * Remove try-catch from FunctionAppLogsTypeTest * Run Spotless:apply * Assert closure of InputStream and JsonReader in FunctionAppLogsTypeTest
1 parent a0b6427 commit 1ffc70d

6 files changed

Lines changed: 565 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
@@ -106,6 +106,9 @@ else if (jsonObject.getString("Type").equals("ADFPipelineRun")) {
106106
else if (jsonObject.getString("Type").equals("AppTraces")) {
107107
eventTypes.add(new AppInsightType(parsedEvent, realHostname));
108108
}
109+
else if (jsonObject.getString("Type").equals("FunctionAppLogs")) {
110+
eventTypes.add(new FunctionAppLogsType(parsedEvent, realHostname));
111+
}
109112
else if (jsonObject.getString("Type").endsWith("_CL")) {
110113
eventTypes.add(new CLType(parsedEvent, realHostname));
111114
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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 FunctionAppLogsType implements EventType {
68+
69+
private final ParsedEvent parsedEvent;
70+
private final String realHostname;
71+
72+
public FunctionAppLogsType(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+
@Override
111+
public String appName() throws PluginException {
112+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
113+
114+
assertKey(record, "AppName", JsonValue.ValueType.STRING);
115+
116+
return new ValidRFC5424AppName(record.getString("AppName")).validAppName();
117+
}
118+
119+
@Override
120+
public long timestamp() throws PluginException {
121+
final JsonObject record = parsedEvent.asJsonStructure().asJsonObject();
122+
123+
assertKey(record, "TimeGenerated", JsonValue.ValueType.STRING);
124+
125+
return new ValidRFC5424Timestamp(record.getString("TimeGenerated")).validTimestamp();
126+
}
127+
128+
@Override
129+
public Set<SDElement> sdElements() {
130+
final Set<SDElement> elems = new HashSet<>();
131+
String time = "";
132+
if (!parsedEvent.enqueuedTimeUtc().isStub()) {
133+
time = parsedEvent.enqueuedTimeUtc().zonedDateTime().toString();
134+
}
135+
136+
String fullyQualifiedNamespace = "";
137+
String eventHubName = "";
138+
String partitionId = "";
139+
String consumerGroup = "";
140+
if (!parsedEvent.partitionCtx().isStub()) {
141+
fullyQualifiedNamespace = String
142+
.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("FullyQualifiedNamespace", ""));
143+
eventHubName = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("EventHubName", ""));
144+
partitionId = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("PartitionId", ""));
145+
consumerGroup = String.valueOf(parsedEvent.partitionCtx().asMap().getOrDefault("ConsumerGroup", ""));
146+
}
147+
148+
elems
149+
.add(new SDElement("aer_02_partition@48577").addSDParam("fully_qualified_namespace", fullyQualifiedNamespace).addSDParam("eventhub_name", eventHubName).addSDParam("partition_id", partitionId).addSDParam("consumer_group", consumerGroup));
150+
151+
elems
152+
.add(new SDElement("event_id@48577").addSDParam("uuid", UUID.randomUUID().toString()).addSDParam("hostname", realHostname).addSDParam("unixtime", Instant.now().toString()).addSDParam("id_source", "aer_02"));
153+
154+
String partitionKey = "";
155+
if (!parsedEvent.systemProperties().isStub()) {
156+
partitionKey = String.valueOf(parsedEvent.systemProperties().asMap().getOrDefault("PartitionKey", ""));
157+
}
158+
159+
String offset = "";
160+
if (!parsedEvent.offset().isStub()) {
161+
offset = parsedEvent.offset().value();
162+
}
163+
164+
elems
165+
.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()));
166+
167+
elems
168+
.add(new SDElement("aer_02@48577").addSDParam("timestamp_source", time.isEmpty() ? "generated" : "timeEnqueued"));
169+
170+
elems.add(new SDElement("nlf_01@48577").addSDParam("eventType", this.getClass().getSimpleName()));
171+
172+
return elems;
173+
}
174+
175+
@Override
176+
public String msgId() {
177+
String sequenceNumber = "";
178+
if (!parsedEvent.systemProperties().isStub()) {
179+
sequenceNumber = String.valueOf(parsedEvent.systemProperties().asMap().getOrDefault("SequenceNumber", ""));
180+
}
181+
return sequenceNumber;
182+
}
183+
184+
@Override
185+
public String msg() {
186+
return parsedEvent.asString();
187+
}
188+
}

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,57 @@ void adfActivityRunType() {
376376
Assertions.assertTrue(sdElementMap.get("aer_02_event@48577").containsKey("properties"));
377377
}
378378

379+
@Test
380+
void functionAppLogsType() {
381+
final String json = Assertions
382+
.assertDoesNotThrow(() -> Files.readString(Paths.get("src/test/resources/function.json")));
383+
final ParsedEvent parsedEvent = new ParsedEventFactory(
384+
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"))
385+
).parsedEvent();
386+
387+
final NLFPlugin plugin = new NLFPlugin(new FakeSourceable());
388+
final List<SyslogMessage> syslogMessages = Assertions
389+
.assertDoesNotThrow(() -> plugin.syslogMessage(parsedEvent));
390+
Assertions.assertEquals(1, syslogMessages.size());
391+
392+
final SyslogMessage syslogMessage = syslogMessages.get(0);
393+
Assertions
394+
.assertEquals(
395+
"{\n" + " \"AppName\": \"app-name\",\n" + " \"Category\": \"function-logs\",\n"
396+
+ " \"EventId\": 123,\n" + " \"EventName\": \"event-name\",\n"
397+
+ " \"ExceptionDetails\": \"123abc\",\n"
398+
+ " \"ExceptionMessage\": \"Found a null value\",\n"
399+
+ " \"ExceptionType\": \"NPE\",\n" + " \"FunctionInvocationId\": \"12345678\",\n"
400+
+ " \"FunctionName\": \"function-1\",\n"
401+
+ " \"HostInstanceId\": \"host-instance-1\",\n" + " \"HostVersion\": \"1.2.3.4.a\",\n"
402+
+ " \"Level\": \"Debug\",\n" + " \"LevelId\": 1,\n"
403+
+ " \"Location\": \"function-1\",\n" + " \"Message\": \"message\",\n"
404+
+ " \"ProcessId\": 123456,\n" + " \"RoleInstance\": \"message\",\n"
405+
+ " \"SourceSystem\": \"Azure\",\n" + " \"TenantId\": \"12\",\n"
406+
+ " \"TimeGenerated\": \"2020-01-01T01:02:34.5678999Z\",\n"
407+
+ " \"Type\": \"FunctionAppLogs\",\n" + " \"_BilledSize\": 1,\n"
408+
+ " \"_ItemId\": \"12-34-56-78\",\n"
409+
+ " \"_Internal_WorkspaceResourceId\": \"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}\",\n"
410+
+ " \"_ResourceId\": \"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}\"\n"
411+
+ "}",
412+
syslogMessage.getMsg()
413+
);
414+
Assertions.assertEquals("md5-0ded52ef915af563e25778bf26b0f129-resourceName", syslogMessage.getHostname());
415+
Assertions.assertEquals("app-name", syslogMessage.getAppName());
416+
Assertions.assertEquals("2020-01-01T01:02:34.567Z", syslogMessage.getTimestamp());
417+
418+
final Map<String, Map<String, String>> sdElementMap = syslogMessage
419+
.getSDElements()
420+
.stream()
421+
.collect(Collectors.toMap((SDElement::getSdID), (sdElem) -> sdElem.getSdParams().stream().collect(Collectors.toMap(SDParam::getParamName, SDParam::getParamValue))));
422+
423+
Assertions.assertEquals(1, sdElementMap.get("nlf_01@48577").size());
424+
Assertions
425+
.assertEquals(FunctionAppLogsType.class.getSimpleName(), sdElementMap.get("nlf_01@48577").get("eventType"));
426+
427+
Assertions.assertTrue(sdElementMap.get("aer_02_event@48577").containsKey("properties"));
428+
}
429+
379430
@Test
380431
void testPostgreSQLType() {
381432
final String json = Assertions

0 commit comments

Comments
 (0)