SlideShare a Scribd company logo
From logs to metrics.
@leodido
Who I am.
Leonardo Di Donato
Senior Software Engineer at
Creator of go-syslog
www.linkedin.com/in/leodidonato
twitter.com/leodido
github.com/leodido
@leodido
How many logs
do we generate
every day?
@leodido
The quantity is not the only factor ...
How many standards - if any - we use to log?
How strictly we follow those standards formats?
@leodido
How to transform
kubernetes logs into
metrics with the TICK stack.
Almost everyone needs to govern their logs.
Deriving metrics, synthesizing and visualizing them helps
in decision making.
git.io/k8s-logs-to-metrics-tick (PoC)
@leodido
First of all we needed a log parser.
But to parse which format … ?
BSD-syslog - RFC 3164 - resembled a de-facto standard.
Wide usage, lot of tools, long lifetime span (2001).
But …
messy/informal RFC ...
no strict well-defined grammar
no single stable framing technique
too many customisations around.
Nope!
@leodido
Thus we chose real syslog.
RFC 5424 deprecates RFC 3164
● Well-defined grammar
● Octet counting framing
○ finally the stack trace for a
panic in a single syslog …
● TLS transport mapping
○ secure logs
● Only 9 years old - ie., 2009
@leodido
We chose Ragel to create the (Go) syslog parser
github.com/influxdata/go-syslog
A state machine compiler
● regular languages -> FSM
● can execute code (actions) at arbitrary points
● non-determinism operators
● table or control flow driven state machines
● various host languages - c, c++, obj-c, asm, d, go, java, ruby, c#, ocaml
@leodido
action dgt { printf("DGT: %cn", fc); }
action dec { printf("DEC: .n"); }
action exp { printf("EXP: %cn", fc); }
action exp_sign { printf("SGN: %cn", fc); }
action number { /*NUMBER*/ }
number = (
[0-9]+ $dgt ( '.' @dec [0-9]+ $dgt )?
( [eE] ( [+-] $exp_sign )? [0-9]+ $exp )?
) %number;
main := ( number 'n' )*;
st0:
if ( ++p == pe )
goto out0;
if ( 48 <= (*p) && (*p) <= 57 )
goto tr0;
goto st_err;
tr0:
{ printf("DGT: %cn", (*p)); }
st1:
if ( ++p == pe )
goto out1;
switch ( (*p) ) {
case 10: goto tr5;
case 46: goto tr7;
case 69: goto st4;
case 101: goto st4;
}
if ( 48 <= (*p) && (*p) <= 57 )
goto tr0;
goto st_err;
// ...
The gotos are your best friends. Only when you do not write them!
@leodido
go-syslog provides parsers for RFC 5424 and RFC 5425.
<85>1 2018-10-11T22:14:15.003Z leodido - 31932 - [ex@31932 iut="3"] An auth token...
prival = facility * 8 + severity
● tools.ietf.org/html/rfc5424.html (Syslog grammar)
● tools.ietf.org/html/rfc5425.html (TLS + octet counting)
● tools.ietf.org/html/rfc5426.html (UDP)
● tools.ietf.org/html/rfc6587.html (TCP + octet counting)
● man7.org/linux/man-pages/man3/syslog.3.html
● man7.org/linux/man-pages/man0/syslog.h.0p.html
● man7.org/linux/man-pages/man1/logger.1.html
@leodido
bestEffortOn := true
i := []byte(`<165>1 2018-10-11T22:14:15.003Z mymach.it e - 1 [ex@32473 iut="3"] An app event...`)
p := rfc5424.NewParser()
m, e := p.Parse(i, &bestEffortOn) // best effort mode on means both m and e can have value ...
This results in m being equal to the following SyslogMessage instance. While error e is nil in this case.
// (rfc5424.SyslogMessage)({
// priority: (*uint8)(165),
// facility: (*uint8)(20),
// severity: (*uint8)(5),
// version: (uint16) 1,
// timestamp: (*time.Time)(2018-10-11 22:14:15.003 +0000 UTC),
// hostname: (*string)((len=9) "mymach.it"),
// appname: (*string)((len=1) "e"),
// procID: (*string)(<nil>),
// msgID: (*string)((len=1) "1"),
// structuredData: (*map[string]map[string]string)((len=1) {
// (string) (len=8) "ex@32473": (map[string]string) (len=1) {
// (string) (len=3) "iut": (string) (len=1) "3"
// }
// }),
// message: (*string)((len=33) "An app event...")
// })
@leodido
It provides also a builder.
Incrementally build valid syslog messages
msg := &SyslogMessage{}
msg.SetTimestamp("not a RFC3339MICRO timestamp")
// Not yet a valid message (try msg.Valid())
msg.SetPriority(191)
msg.SetVersion(1)
msg.Valid() // Now it is minimally valid
str, _ := msg.String()
// str is “<191>1 - - - - - -”
Notice that its API ignores input values that does not follow the grammar.
@leodido
Performances.
● ~250ns to parse the smallest legal message
● ~2µs to parse an average legal message
● ~4µs to parse a very long legal message
[no]_empty_input__________________________________-4 30000000 253 ns/op 224 B/op 3 allocs/op
[no]_multiple_syslog_messages_on_multiple_lines___-4 20000000 433 ns/op 304 B/op 12 allocs/op
[no]_impossible_timestamp_________________________-4 10000000 1080 ns/op 528 B/op 11 allocs/op
[no]_malformed_structured_data____________________-4 20000000 552 ns/op 400 B/op 12 allocs/op
[no]_with_duplicated_structured_data_id___________-4 5000000 1246 ns/op 688 B/op 17 allocs/op
[ok]_minimal______________________________________-4 30000000 264 ns/op 247 B/op 9 allocs/op
[ok]_average_message______________________________-4 5000000 1984 ns/op 1536 B/op 26 allocs/op
[ok]_complicated_message__________________________-4 5000000 1644 ns/op 1280 B/op 25 allocs/op
[ok]_very_long_message____________________________-4 2000000 3826 ns/op 2464 B/op 28 allocs/op
[ok]_all_max_length_and_complete__________________-4 3000000 2792 ns/op 1888 B/op 28 allocs/op
[ok]_all_max_length_except_structured_data_and_mes-4 5000000 1830 ns/op 883 B/op 13 allocs/op
[ok]_minimal_with_message_containing_newline______-4 20000000 294 ns/op 250 B/op 10 allocs/op
[ok]_w/o_procid,_w/o_structured_data,_with_message-4 10000000 956 ns/op 364 B/op 11 allocs/op
[ok]_minimal_with_UTF-8_message___________________-4 20000000 586 ns/op 359 B/op 10 allocs/op
[ok]_with_structured_data_id,_w/o_structured_data_-4 10000000 998 ns/op 592 B/op 14 allocs/op
[ok]_with_multiple_structured_data________________-4 5000000 1538 ns/op 1232 B/op 22 allocs/op
[ok]_with_escaped_backslash_within_structured_data-4 5000000 1316 ns/op 920 B/op 20 allocs/op
[ok]_with_UTF-8_structured_data_param_value,_with_-4 5000000 1580 ns/op 1050 B/op 21 allocs/op
@leodido
Telegraf is the plugin-driven server agent for collecting &
reporting metrics.
github.com/influxdata/telegraf
Thus we created the syslog input plugin for it, using go-syslog
● Listens for syslog messages transmitted over UDP - RFC 5426 - or TCP.
● Supports (atm) only messages formatted according to RFC 5424.
● Supports TLS, octet framing (both over TCP - RFC 6587 - and TLS - RFC 5425).
● BSD format - RFC 3164 - in progress.
@leodido
Metrics
Measurement: syslog
● tags
○ severity (string)
○ facility (string)
○ hostname (string)
○ appname (string)
● fields
○ version (integer)
○ severity_code (integer)
○ facility_code (integer)
○ timestamp (integer) - the time recorded in the
syslog message
○ procid (string)
○ msgid (string)
○ sdid (bool)
○ structured data elements (string)
● timestamp - the time the messages was received
@leodido
Input (with octet counting)
169 <165>1 2018-10-01:14:15.000Z mymachine.example.com evntslog - ID47 [exampleSDID@32473
iut="3" eventSource="Application" eventID="1011"] An application event log entry...
Output
syslog,appname=evntslog,facility=local4,hostname=mymachine.example.com,severity=notice
exampleSDID@32473_eventID="1011",exampleSDID@32473_eventSource="Application",exampleSDID@32
473_iut="3",facility_code=20i,message="An application event log
entry...",msgid="ID47",severity_code=5i,timestamp=1065910455003000000i,version=1i
1538421339749472344
@leodido
Our solution
Grab k8s and kernel
logs from journald.
Parse them via telegraf
syslog input plugin.
Visualize logs with
chronograf log viewer.
Elicit new metrics to plot applying a
kapacitor UDF.
Source code at git.io/k8s-logs-to-metrics-tick
@leodido
YAML TIME!
Using rsyslog to grab RFC 5424 syslog messages from journald.
apiVersion: v1
kind: ConfigMap
metadata:
name: rsyslog
namespace: logging
labels:
component: rsyslog
app: rsyslog
data:
rsyslog.conf: |+
# …
module(load="imjournal" ...)
# This module only works with the journald and json-file docker log drivers
module(load="mmkubernetes" tls.cacert="..." tokenfile="..." annotation_match=["."])
# Extracts k8s metadata
action(type="mmkubernetes")
# …
# Compose RFC5424 message
template(name="rfc5424" type="list") { … }
action(type="omfwd" target="127.0.0.1" port="6514" protocol="tcp" tcp_framing="octet-counted"
template="rfc5424" ...)
@leodido
Setup telegraf syslog plugin to receive log messages over TCP.
apiVersion: v1
kind: ConfigMap
metadata:
name: telegraf
namespace: logging
labels:
component: telegraf
app: telegraf
data:
telegraf.conf: |+
# ...
[agent]
interval = "10s"
round_interval = true
metric_batch_size = 1000
# ...
[[outputs.influxdb]]
urls = ["http://influxdb:8086"] # required
database = "telegraf" # required
retention_policy = "autogen"
write_consistency = "any"
timeout = "1m"
[[inputs.syslog]]
server = "tcp://:6514"
best_effort = true
@leodido
Let’s deploy chronograf and influxDB
apiVersion: v1
kind: Service
metadata:
name: chronograf
namespace: logging
labels:
component: chronograf
app: chronograf
spec:
ports:
- port: 80
targetPort: 8888
name: server
selector:
component: chronograf
---
apiVersion: apps/v1
kind: Deployment
# ...
apiVersion: v1
kind: Service
metadata:
name: influxdb
namespace: logging
labels:
component: influxdb
app: influxdb
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: “true”
spec:
clusterIP: None
ports:
- port: 8086
name: server
selector:
component: influxdb
---
apiVersion: apps/v1
kind: StatefulSet
# ...
@leodido
@leodido
@leodido
Logs are just a mess!
Inspecting logs coming from a single server is easy.
Inspecting logs coming from a distributed system is hard.
We need metrics!
@leodido
Now we want to detect and count the OOMs.
Logs are streams.
We need a streaming processor!
github.com/influxdata/kapacitor
A streaming processor can be programmed to identify
the patterns we want and act on them, e.g: OOM Kills.
Memory cgroup out of memory: Kill process 13012 (stress) score 1958 or sacrifice child
@leodido
Let’s write a tick script to grab log points
dbrp "telegraf"."autogen"
stream
|from()
.measurement('syslog')
.truncate(1ms)
.where(lambda: "appname" == 'kernel') # filter by points tag
.where(lambda: "message" =~ /sacrifice/) # filtering on messages with regex
@example() # passing points to our user defined function (UDF)
|influxDBOut()
.database('telegraf')
.measurement('k8s')
Collection Ingestion Storage
Stream
reprocessing
Dashboards
@leodido
Let’s configure kapacitor
# …
[udf]
[udf.functions]
[udf.functions.example]
socket = "/tmp/example.sock"
timeout = "10s"
[[influxdb]]
enabled = true
default = true
name = "logging"
urls = ["http://localhost:8086"]
timeout = 0
startup-timeout = "5m"
[influxdb.subscriptions]
telegraf = ["autogen"]
@leodido
Let’s write the UDF
func (h *handler) Point(p *agent.Point) error {
var r = regexp.MustCompile(`(?m).*Kill process (?P<pid>d+) (?P<proc>(.*)) score (?P<score>d+)`)
message, ok := p.FieldsString[“message”]
if ok {
m := r.FindStringSubmatch(message)
data := mapSubexpNames(m, r.SubexpNames())
proc := strings.Trim(data[“proc”], “()”)
state := h.state[proc]
if state == nil {
state := &myState{Counter: 0}
h.state[proc] = state
}
h.state[proc].update()
newpoint := &agent.Point{
Time: time.Now().UnixNano(),
Tags: map[string]string{
“proc”: proc,
“pid”: string(data[“pid”]),
},
FieldsInt: map[string]int64{
“count”: h.state[proc].Counter,
},
}
// Send point
h.agent.Responses <- &agent.Response{
Message: &agent.Response_Point{
Point: newpoint,
},
}
}
return nil
}
@leodido
@leodido
From logs to metrics
Thanks.
@leodido
git.io/k8s-logs-to-metrics-tick
git.io/go-syslog
github.com/influxdata

More Related Content

PDF
Formbook - In-depth malware analysis (Botconf 2018)
Rémi Jullian
 
PDF
Fuzzing - Part 1
UTD Computer Security Group
 
PDF
Analysis of a Modified RC4
Tharindu Weerasinghe
 
PPTX
Secure coding for developers
sluge
 
PDF
Sourcefire Vulnerability Research Team Labs
losalamos
 
PPTX
C Programming Training in Ambala ! Batra Computer Centre
jatin batra
 
PDF
Jurczyk windows kernel reference count vulnerabilities. case study
DefconRussia
 
PDF
Cryptographic algorithms diversity: Russian (GOST) crypto algorithms
Dmitry Baryshkov
 
Formbook - In-depth malware analysis (Botconf 2018)
Rémi Jullian
 
Fuzzing - Part 1
UTD Computer Security Group
 
Analysis of a Modified RC4
Tharindu Weerasinghe
 
Secure coding for developers
sluge
 
Sourcefire Vulnerability Research Team Labs
losalamos
 
C Programming Training in Ambala ! Batra Computer Centre
jatin batra
 
Jurczyk windows kernel reference count vulnerabilities. case study
DefconRussia
 
Cryptographic algorithms diversity: Russian (GOST) crypto algorithms
Dmitry Baryshkov
 

What's hot (9)

PDF
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
44CON
 
PDF
john-devkit: 100 типов хешей спустя / john-devkit: 100 Hash Types Later
Positive Hack Days
 
PPT
Cs423 raw sockets_bw
jktjpc
 
PPTX
Synchronization
David Evans
 
PDF
Martin Novotny and Timo Kasper
Information Security Awareness Group
 
PDF
Advanced cfg bypass on adobe flash player 18 defcon russia 23
DefconRussia
 
PPTX
Java Bytecode Fundamentals - JUG.lv
Anton Arhipov
 
PDF
Salsa20
Amit Ghosh
 
PPTX
Eloi Sanfelix - Hardware security: Side Channel Attacks [RootedCON 2011]
RootedCON
 
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
44CON
 
john-devkit: 100 типов хешей спустя / john-devkit: 100 Hash Types Later
Positive Hack Days
 
Cs423 raw sockets_bw
jktjpc
 
Synchronization
David Evans
 
Martin Novotny and Timo Kasper
Information Security Awareness Group
 
Advanced cfg bypass on adobe flash player 18 defcon russia 23
DefconRussia
 
Java Bytecode Fundamentals - JUG.lv
Anton Arhipov
 
Salsa20
Amit Ghosh
 
Eloi Sanfelix - Hardware security: Side Channel Attacks [RootedCON 2011]
RootedCON
 
Ad

Similar to From logs to metrics (20)

PPTX
A New Framework for Detection
Sourcefire VRT
 
PDF
1032 cs208 g operation system ip camera case share.v0.2
Stanley Ho
 
PPTX
eProsima RPC over DDS - Connext Conf London October 2015
Jaime Martin Losa
 
PDF
LSFMM 2019 BPF Observability
Brendan Gregg
 
PDF
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Marina Kolpakova
 
PDF
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Semihalf
 
ODP
Beyond php - it's not (just) about the code
Wim Godden
 
PDF
Understanding binder in android
Haifeng Li
 
ODP
Linux kernel tracing superpowers in the cloud
Andrea Righi
 
PDF
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
Code Engn
 
PDF
Unifying Events and Logs into the Cloud
Eduardo Silva Pereira
 
PDF
Unifying Events and Logs into the Cloud
Treasure Data, Inc.
 
PPTX
SEMLA_logging_infra
swy351
 
PDF
Вениамин Гвоздиков: Особенности использования DTrace
Yandex
 
PDF
Data Structures for High Resolution, Real-time Telemetry at Scale
ScyllaDB
 
PDF
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Igalia
 
PDF
Icpc11b.ppt
Yann-Gaël Guéhéneuc
 
PDF
Android binder introduction
Derek Fang
 
PDF
bcc/BPF tools - Strategy, current tools, future challenges
IO Visor Project
 
PDF
BPF Tools 2017
Brendan Gregg
 
A New Framework for Detection
Sourcefire VRT
 
1032 cs208 g operation system ip camera case share.v0.2
Stanley Ho
 
eProsima RPC over DDS - Connext Conf London October 2015
Jaime Martin Losa
 
LSFMM 2019 BPF Observability
Brendan Gregg
 
Pragmatic Optimization in Modern Programming - Demystifying the Compiler
Marina Kolpakova
 
Oczyszczacz powietrza i stos sieciowy? Czas na test! Semihalf Barcamp 13/06/2018
Semihalf
 
Beyond php - it's not (just) about the code
Wim Godden
 
Understanding binder in android
Haifeng Li
 
Linux kernel tracing superpowers in the cloud
Andrea Righi
 
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
Code Engn
 
Unifying Events and Logs into the Cloud
Eduardo Silva Pereira
 
Unifying Events and Logs into the Cloud
Treasure Data, Inc.
 
SEMLA_logging_infra
swy351
 
Вениамин Гвоздиков: Особенности использования DTrace
Yandex
 
Data Structures for High Resolution, Real-time Telemetry at Scale
ScyllaDB
 
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Igalia
 
Android binder introduction
Derek Fang
 
bcc/BPF tools - Strategy, current tools, future challenges
IO Visor Project
 
BPF Tools 2017
Brendan Gregg
 
Ad

More from Leonardo Di Donato (9)

PDF
Prometheus as exposition format for eBPF programs running on Kubernetes
Leonardo Di Donato
 
PDF
Open metrics: Prometheus Unbound?
Leonardo Di Donato
 
PDF
Continuous Time Bayesian Network Classifiers, M.Sc Thesis
Leonardo Di Donato
 
PDF
Topic Modeling for Information Retrieval and Word Sense Disambiguation tasks
Leonardo Di Donato
 
PDF
Guida all'estrazione di dati dai Social Network
Leonardo Di Donato
 
PPTX
Virtual Worlds
Leonardo Di Donato
 
PDF
A Location Based Mobile Social Network
Leonardo Di Donato
 
PDF
Sistema Rilevamento Transiti (SRT) - Software Analysis and Design
Leonardo Di Donato
 
PDF
CRADLE: Clustering by RAndom minimization Dispersion based LEarning - Un algo...
Leonardo Di Donato
 
Prometheus as exposition format for eBPF programs running on Kubernetes
Leonardo Di Donato
 
Open metrics: Prometheus Unbound?
Leonardo Di Donato
 
Continuous Time Bayesian Network Classifiers, M.Sc Thesis
Leonardo Di Donato
 
Topic Modeling for Information Retrieval and Word Sense Disambiguation tasks
Leonardo Di Donato
 
Guida all'estrazione di dati dai Social Network
Leonardo Di Donato
 
Virtual Worlds
Leonardo Di Donato
 
A Location Based Mobile Social Network
Leonardo Di Donato
 
Sistema Rilevamento Transiti (SRT) - Software Analysis and Design
Leonardo Di Donato
 
CRADLE: Clustering by RAndom minimization Dispersion based LEarning - Un algo...
Leonardo Di Donato
 

Recently uploaded (20)

PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PDF
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Presentation about variables and constant.pptx
kr2589474
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
vAdobe Premiere Pro 2025 (v25.2.3.004) Crack Pre-Activated Latest
imang66g
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Activate_Methodology_Summary presentatio
annapureddyn
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 

From logs to metrics

  • 1. From logs to metrics. @leodido
  • 2. Who I am. Leonardo Di Donato Senior Software Engineer at Creator of go-syslog www.linkedin.com/in/leodidonato twitter.com/leodido github.com/leodido @leodido
  • 3. How many logs do we generate every day? @leodido
  • 4. The quantity is not the only factor ... How many standards - if any - we use to log? How strictly we follow those standards formats? @leodido
  • 5. How to transform kubernetes logs into metrics with the TICK stack. Almost everyone needs to govern their logs. Deriving metrics, synthesizing and visualizing them helps in decision making. git.io/k8s-logs-to-metrics-tick (PoC) @leodido
  • 6. First of all we needed a log parser. But to parse which format … ? BSD-syslog - RFC 3164 - resembled a de-facto standard. Wide usage, lot of tools, long lifetime span (2001). But … messy/informal RFC ... no strict well-defined grammar no single stable framing technique too many customisations around. Nope! @leodido
  • 7. Thus we chose real syslog. RFC 5424 deprecates RFC 3164 ● Well-defined grammar ● Octet counting framing ○ finally the stack trace for a panic in a single syslog … ● TLS transport mapping ○ secure logs ● Only 9 years old - ie., 2009 @leodido
  • 8. We chose Ragel to create the (Go) syslog parser github.com/influxdata/go-syslog A state machine compiler ● regular languages -> FSM ● can execute code (actions) at arbitrary points ● non-determinism operators ● table or control flow driven state machines ● various host languages - c, c++, obj-c, asm, d, go, java, ruby, c#, ocaml @leodido
  • 9. action dgt { printf("DGT: %cn", fc); } action dec { printf("DEC: .n"); } action exp { printf("EXP: %cn", fc); } action exp_sign { printf("SGN: %cn", fc); } action number { /*NUMBER*/ } number = ( [0-9]+ $dgt ( '.' @dec [0-9]+ $dgt )? ( [eE] ( [+-] $exp_sign )? [0-9]+ $exp )? ) %number; main := ( number 'n' )*; st0: if ( ++p == pe ) goto out0; if ( 48 <= (*p) && (*p) <= 57 ) goto tr0; goto st_err; tr0: { printf("DGT: %cn", (*p)); } st1: if ( ++p == pe ) goto out1; switch ( (*p) ) { case 10: goto tr5; case 46: goto tr7; case 69: goto st4; case 101: goto st4; } if ( 48 <= (*p) && (*p) <= 57 ) goto tr0; goto st_err; // ... The gotos are your best friends. Only when you do not write them! @leodido
  • 10. go-syslog provides parsers for RFC 5424 and RFC 5425. <85>1 2018-10-11T22:14:15.003Z leodido - 31932 - [ex@31932 iut="3"] An auth token... prival = facility * 8 + severity ● tools.ietf.org/html/rfc5424.html (Syslog grammar) ● tools.ietf.org/html/rfc5425.html (TLS + octet counting) ● tools.ietf.org/html/rfc5426.html (UDP) ● tools.ietf.org/html/rfc6587.html (TCP + octet counting) ● man7.org/linux/man-pages/man3/syslog.3.html ● man7.org/linux/man-pages/man0/syslog.h.0p.html ● man7.org/linux/man-pages/man1/logger.1.html @leodido
  • 11. bestEffortOn := true i := []byte(`<165>1 2018-10-11T22:14:15.003Z mymach.it e - 1 [ex@32473 iut="3"] An app event...`) p := rfc5424.NewParser() m, e := p.Parse(i, &bestEffortOn) // best effort mode on means both m and e can have value ... This results in m being equal to the following SyslogMessage instance. While error e is nil in this case. // (rfc5424.SyslogMessage)({ // priority: (*uint8)(165), // facility: (*uint8)(20), // severity: (*uint8)(5), // version: (uint16) 1, // timestamp: (*time.Time)(2018-10-11 22:14:15.003 +0000 UTC), // hostname: (*string)((len=9) "mymach.it"), // appname: (*string)((len=1) "e"), // procID: (*string)(<nil>), // msgID: (*string)((len=1) "1"), // structuredData: (*map[string]map[string]string)((len=1) { // (string) (len=8) "ex@32473": (map[string]string) (len=1) { // (string) (len=3) "iut": (string) (len=1) "3" // } // }), // message: (*string)((len=33) "An app event...") // }) @leodido
  • 12. It provides also a builder. Incrementally build valid syslog messages msg := &SyslogMessage{} msg.SetTimestamp("not a RFC3339MICRO timestamp") // Not yet a valid message (try msg.Valid()) msg.SetPriority(191) msg.SetVersion(1) msg.Valid() // Now it is minimally valid str, _ := msg.String() // str is “<191>1 - - - - - -” Notice that its API ignores input values that does not follow the grammar. @leodido
  • 13. Performances. ● ~250ns to parse the smallest legal message ● ~2µs to parse an average legal message ● ~4µs to parse a very long legal message [no]_empty_input__________________________________-4 30000000 253 ns/op 224 B/op 3 allocs/op [no]_multiple_syslog_messages_on_multiple_lines___-4 20000000 433 ns/op 304 B/op 12 allocs/op [no]_impossible_timestamp_________________________-4 10000000 1080 ns/op 528 B/op 11 allocs/op [no]_malformed_structured_data____________________-4 20000000 552 ns/op 400 B/op 12 allocs/op [no]_with_duplicated_structured_data_id___________-4 5000000 1246 ns/op 688 B/op 17 allocs/op [ok]_minimal______________________________________-4 30000000 264 ns/op 247 B/op 9 allocs/op [ok]_average_message______________________________-4 5000000 1984 ns/op 1536 B/op 26 allocs/op [ok]_complicated_message__________________________-4 5000000 1644 ns/op 1280 B/op 25 allocs/op [ok]_very_long_message____________________________-4 2000000 3826 ns/op 2464 B/op 28 allocs/op [ok]_all_max_length_and_complete__________________-4 3000000 2792 ns/op 1888 B/op 28 allocs/op [ok]_all_max_length_except_structured_data_and_mes-4 5000000 1830 ns/op 883 B/op 13 allocs/op [ok]_minimal_with_message_containing_newline______-4 20000000 294 ns/op 250 B/op 10 allocs/op [ok]_w/o_procid,_w/o_structured_data,_with_message-4 10000000 956 ns/op 364 B/op 11 allocs/op [ok]_minimal_with_UTF-8_message___________________-4 20000000 586 ns/op 359 B/op 10 allocs/op [ok]_with_structured_data_id,_w/o_structured_data_-4 10000000 998 ns/op 592 B/op 14 allocs/op [ok]_with_multiple_structured_data________________-4 5000000 1538 ns/op 1232 B/op 22 allocs/op [ok]_with_escaped_backslash_within_structured_data-4 5000000 1316 ns/op 920 B/op 20 allocs/op [ok]_with_UTF-8_structured_data_param_value,_with_-4 5000000 1580 ns/op 1050 B/op 21 allocs/op @leodido
  • 14. Telegraf is the plugin-driven server agent for collecting & reporting metrics. github.com/influxdata/telegraf Thus we created the syslog input plugin for it, using go-syslog ● Listens for syslog messages transmitted over UDP - RFC 5426 - or TCP. ● Supports (atm) only messages formatted according to RFC 5424. ● Supports TLS, octet framing (both over TCP - RFC 6587 - and TLS - RFC 5425). ● BSD format - RFC 3164 - in progress. @leodido
  • 15. Metrics Measurement: syslog ● tags ○ severity (string) ○ facility (string) ○ hostname (string) ○ appname (string) ● fields ○ version (integer) ○ severity_code (integer) ○ facility_code (integer) ○ timestamp (integer) - the time recorded in the syslog message ○ procid (string) ○ msgid (string) ○ sdid (bool) ○ structured data elements (string) ● timestamp - the time the messages was received @leodido
  • 16. Input (with octet counting) 169 <165>1 2018-10-01:14:15.000Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"] An application event log entry... Output syslog,appname=evntslog,facility=local4,hostname=mymachine.example.com,severity=notice exampleSDID@32473_eventID="1011",exampleSDID@32473_eventSource="Application",exampleSDID@32 473_iut="3",facility_code=20i,message="An application event log entry...",msgid="ID47",severity_code=5i,timestamp=1065910455003000000i,version=1i 1538421339749472344 @leodido
  • 17. Our solution Grab k8s and kernel logs from journald. Parse them via telegraf syslog input plugin. Visualize logs with chronograf log viewer. Elicit new metrics to plot applying a kapacitor UDF. Source code at git.io/k8s-logs-to-metrics-tick @leodido
  • 19. Using rsyslog to grab RFC 5424 syslog messages from journald. apiVersion: v1 kind: ConfigMap metadata: name: rsyslog namespace: logging labels: component: rsyslog app: rsyslog data: rsyslog.conf: |+ # … module(load="imjournal" ...) # This module only works with the journald and json-file docker log drivers module(load="mmkubernetes" tls.cacert="..." tokenfile="..." annotation_match=["."]) # Extracts k8s metadata action(type="mmkubernetes") # … # Compose RFC5424 message template(name="rfc5424" type="list") { … } action(type="omfwd" target="127.0.0.1" port="6514" protocol="tcp" tcp_framing="octet-counted" template="rfc5424" ...) @leodido
  • 20. Setup telegraf syslog plugin to receive log messages over TCP. apiVersion: v1 kind: ConfigMap metadata: name: telegraf namespace: logging labels: component: telegraf app: telegraf data: telegraf.conf: |+ # ... [agent] interval = "10s" round_interval = true metric_batch_size = 1000 # ... [[outputs.influxdb]] urls = ["http://influxdb:8086"] # required database = "telegraf" # required retention_policy = "autogen" write_consistency = "any" timeout = "1m" [[inputs.syslog]] server = "tcp://:6514" best_effort = true @leodido
  • 21. Let’s deploy chronograf and influxDB apiVersion: v1 kind: Service metadata: name: chronograf namespace: logging labels: component: chronograf app: chronograf spec: ports: - port: 80 targetPort: 8888 name: server selector: component: chronograf --- apiVersion: apps/v1 kind: Deployment # ... apiVersion: v1 kind: Service metadata: name: influxdb namespace: logging labels: component: influxdb app: influxdb annotations: service.alpha.kubernetes.io/tolerate-unready-endpoints: “true” spec: clusterIP: None ports: - port: 8086 name: server selector: component: influxdb --- apiVersion: apps/v1 kind: StatefulSet # ... @leodido
  • 24. Logs are just a mess! Inspecting logs coming from a single server is easy. Inspecting logs coming from a distributed system is hard. We need metrics! @leodido
  • 25. Now we want to detect and count the OOMs. Logs are streams. We need a streaming processor! github.com/influxdata/kapacitor A streaming processor can be programmed to identify the patterns we want and act on them, e.g: OOM Kills. Memory cgroup out of memory: Kill process 13012 (stress) score 1958 or sacrifice child @leodido
  • 26. Let’s write a tick script to grab log points dbrp "telegraf"."autogen" stream |from() .measurement('syslog') .truncate(1ms) .where(lambda: "appname" == 'kernel') # filter by points tag .where(lambda: "message" =~ /sacrifice/) # filtering on messages with regex @example() # passing points to our user defined function (UDF) |influxDBOut() .database('telegraf') .measurement('k8s') Collection Ingestion Storage Stream reprocessing Dashboards @leodido
  • 27. Let’s configure kapacitor # … [udf] [udf.functions] [udf.functions.example] socket = "/tmp/example.sock" timeout = "10s" [[influxdb]] enabled = true default = true name = "logging" urls = ["http://localhost:8086"] timeout = 0 startup-timeout = "5m" [influxdb.subscriptions] telegraf = ["autogen"] @leodido
  • 28. Let’s write the UDF func (h *handler) Point(p *agent.Point) error { var r = regexp.MustCompile(`(?m).*Kill process (?P<pid>d+) (?P<proc>(.*)) score (?P<score>d+)`) message, ok := p.FieldsString[“message”] if ok { m := r.FindStringSubmatch(message) data := mapSubexpNames(m, r.SubexpNames()) proc := strings.Trim(data[“proc”], “()”) state := h.state[proc] if state == nil { state := &myState{Counter: 0} h.state[proc] = state } h.state[proc].update() newpoint := &agent.Point{ Time: time.Now().UnixNano(), Tags: map[string]string{ “proc”: proc, “pid”: string(data[“pid”]), }, FieldsInt: map[string]int64{ “count”: h.state[proc].Counter, }, } // Send point h.agent.Responses <- &agent.Response{ Message: &agent.Response_Point{ Point: newpoint, }, } } return nil } @leodido