Hi,
I’ve been trying for two days to get an Alloy config working to drop log lines that do not contain a specific field (duration
in my case), as I don’t want to process them.
I’ve tried multiple approaches using json
, drop
, and match
stages, but I haven’t been able to get it working as expected.
I’ve looked at multiple forum posts (can only put 2 links so no more…):
- How to keep only log lines that match a given regexp in alloy and send them to Loki
- Drop log lines not matching regex
But I still don’t get how to make it work.
Example Log
{"level":"info","ts":"2025-03-20T16:19:07Z","msg":"Calculating next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"12345678-aaaa-bbbb-cccc-9876543210ef","actual":{"state":"starting","date":"2025-03-20T16:19:02Z"}}
{"level":"info","ts":"2025-03-20T16:19:07Z","msg":"Next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"12345678-aaaa-bbbb-cccc-9876543210ef","next":"starting"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Calculating next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab","actual":{"state":"starting","date":"2025-03-20T16:19:02Z"}}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab","next":"initializing"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Updating process state \"initializing\"","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Previous state was \"starting\"","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab","process_id":"node-x1a2b3c4","service":"node","service_version":"1.2.3-456-untested","graphical":"false","cloud_provider":"aws","region":"us-east-1","resource_type":"m5.large","state":"starting","duration":"6"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Updated Process.Status.LastState to \"&{initializing 2025-03-20 16:19:08 +0000 UTC}\"","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Calculating next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"55555555-cccc-bbbb-aaaa-999999999999","actual":{"state":"initializing","date":"2025-03-20T16:19:08Z"}}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"55555555-cccc-bbbb-aaaa-999999999999","next":"initializing"}
{"level":"info","ts":"2025-03-20T16:19:07Z","msg":"Calculating next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"12345678-aaaa-bbbb-cccc-9876543210ef","actual":{"state":"starting","date":"2025-03-20T16:19:02Z"}}
{"level":"info","ts":"2025-03-20T16:19:07Z","msg":"Next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"12345678-aaaa-bbbb-cccc-9876543210ef","next":"starting"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Calculating next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab","actual":{"state":"starting","date":"2025-03-20T16:19:02Z"}}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab","next":"initializing"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Updating process state \"initializing\"","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Previous state was \"starting\"","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab","process_id":"node-x1a2b3c4","service":"node","service_version":"1.2.3-456-untested","graphical":"false","cloud_provider":"aws","region":"us-east-1","resource_type":"m5.large","state":"starting","duration":"6"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Updated Process.Status.LastState to \"&{initializing 2025-03-20 16:19:08 +0000 UTC}\"","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"87654321-ffff-eeee-dddd-0123456789ab"}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Calculating next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"55555555-cccc-bbbb-aaaa-999999999999","actual":{"state":"initializing","date":"2025-03-20T16:19:08Z"}}
{"level":"info","ts":"2025-03-20T16:19:08Z","msg":"Next process state","controller":"process","controllerGroup":"system.pipeline","controllerKind":"Process","Process":{"name":"node-x1a2b3c4","namespace":"test-env"},"namespace":"test-env","name":"node-x1a2b3c4","reconcileID":"55555555-cccc-bbbb-aaaa-999999999999","next":"initializing"}
I don’t care about lines without the duration
field and don’t even want to store them, as they are handled by another loki.process
pipeline.
My Current Process
After two days of troubleshooting, here’s the setup I have:
loki.process "task_analysis_json_extraction" {
stage.json {
expressions = {
line = "",
}
}
stage.match {
selector = "{line!~\".*duration.*\"}"
action = "drop"
}
forward_to = [loki.process.task_analysis_field_extraction.receiver]
}
loki.process "task_analysis_field_extraction" {
stage.json {
expressions = {
level = "level",
ts = "ts",
msg = "msg",
controller = "controller",
controllerGroup = "controllerGroup",
controllerKind = "controllerKind",
Task = "Task",
namespace = "namespace",
name = "name",
reconcileID = "reconcileID",
task_id = "task_id",
app = "app",
app_version = "app_version",
graphical = "graphical",
state = "state",
duration = "duration",
}
}
stage.timestamp {
source = "ts"
format = "RFC3339"
}
stage.labels {
values = {
app_version = "",
namespace = "",
app = "",
graphical = "",
state = "",
}
}
stage.structured_metadata {
values = {
name = "",
reconcileID = "",
task_id = "",
duration = "",
}
}
// Forward to logs service
forward_to = [loki.write.logs_service.receiver]
}
I split it into two process
blocks for debugging purposes.
As line filtering is not available in the match stage, and the drop stage requires an RE2 expression, I can’t use something like this ^(?!.*\bduration\b).*\n?
.
Any help would be greatly appreciated!