-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
959 lines (932 loc) · 58.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
<!DOCTYPE html>
<html lang="en">
<head>
<title>Grafana Loki Unleashed</title>
<meta charset="utf-8" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="node_modules/reveal.js/dist/reset.css" />
<link rel="stylesheet" href="node_modules/reveal.js/dist/reveal.css" />
<link rel="stylesheet" href="node_modules/reveal.js/dist/theme/black.css" id="theme" />
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="font-redcap">Grafana Loki</h1>
<h2 class="font-redcap" style="background: none; -webkit-text-fill-color: #8b0303; font-size: 1.5em; color: #8b0303; text-shadow: 1px 3px 3px #f15b2bff !important;"
>unleashed</h2>
</section>
<section>
<section>
<h2 class="style-grafana">About Me</h2>
</section>
<section>
<h3 class="style-grafana">Andreas Gebhardt</h3>
<ul>
<li>Software Engineer</li>
<li>Open Source Enthusiast</li>
<li>
<div style="display: flex; align-items: center;">
<span>agebhar1@</span> {
<a target="_blank" href="https://www.github.com/agebhar1" style="top: 4px"><img class="github-mark"/></a>,
<a target="_blank" href="https://bsky.app/profile/agebhar1.bsky.social" style="top: 4px"><svg style="width: 0.95em; color: var(--r-main-color); fill: currentcolor;" viewBox="0 0 580 512" aria-hidden="true"><g><path fill="currentColor" d="M123.121 33.664C188.241 82.553 258.281 181.68 284 234.873c25.719-53.192 95.759-152.32 160.879-201.21C491.866-1.611 568-28.906 568 57.947c0 17.346-9.945 145.713-15.778 166.555-20.275 72.453-94.155 90.933-159.875 79.748C507.222 323.8 536.444 388.56 473.333 453.32c-119.86 122.992-172.272-30.859-185.702-70.281-2.462-7.227-3.614-10.608-3.631-7.733-.017-2.875-1.169.506-3.631 7.733-13.43 39.422-65.842 193.273-185.702 70.281-63.111-64.76-33.89-129.52 80.986-149.071-65.72 11.185-139.6-7.295-159.875-79.748C9.945 203.659 0 75.291 0 57.946 0-28.906 76.135-1.612 123.121 33.664Z"></path></g></svg></a>
}
</div>
</li>
</ul>
</section>
</section>
<section>
<!-- https://grafana.com/docs/loki/latest/ -->
<h1>Grafana Loki</h1>
<q>Like Prometheus, but for logs!</q>
<p>
<span class="fragment" style="font-size: 0.75em;">core of a full-featured logging stack</span>
</p>
<aside class="notes">
<ul>
<li>only indexing metadata (labels)</li>
<li>log data compressed and stored in chunks in object stores (e.g S3)</li>
<li>simplifies operation and significantly lowers cost</li>
</ul>
</aside>
</section>
<section>
<!-- https://grafana.com/docs/loki/latest/fundamentals/ -->
<section>
<h1>Origin</h1>
<span style="font-size: 0.75em;">Design Document (2018)</span>
</section>
<section>
<h2>Existing Solutions</h2>
<ul>
<li class="fragment">full-text search based (e.g. Elasticsearch <a target="_blank" href="https://www.elastic.co/guide/en/elasticsearch/reference/8.8/documents-indices.html">Data in: documents and indices</a>)</li>
<li class="fragment">based on <a target="_blank" href="https://en.wikipedia.org/wiki/Inverted_index">inverted index</a></li>
<li class="fragment">complicated to scale</li>
<li class="fragment">resource intensive</li>
</ul>
</section>
<section>
<h2>Daily Business</h2>
<figure class="fragment">
<img style="width: 50%;" src="images/alicegoldfuss-tweet.png" />
<figcaption>
<a target="_blank" href="https://twitter.com/alicegoldfuss/status/981947777256079360">https://twitter.com/alicegoldfuss/status/981947777256079360</a>
</figcaption>
</figure>
<br/>
<ul>
<li class="fragment">queries focusing on a time range (+ simple parameters)</li>
</ul>
<aside class="notes">
<ul>
<li>Kubernetes: logging has changed in modern cloud native (...) workloads, STDOUT/STDERR</li>
</ul>
</aside>
</section>
<section>
<h2>Cost Efficiency</h2>
<ul>
<li class="fragment">excessive costs in SaaS log aggregation</li>
<li class="fragment">scaling and sharding an inverted index is hard</li>
<li class="fragment">
<a target="_blank" href="https://github.com/oklog/oklog">OK Log</a> (archived) by Peter Bourgon
<ul>
<li>eventually consistent, mesh-based distribution strategy</li>
<li>massive cost reduction</li>
<li>simpler operations</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Prometheus and Cortex</h2>
<ul>
<li class="fragment">monitoring system around time series database (TSDB)</li>
<li class="fragment">indexes collection of samples (a time series) and Key/Values (labels)</li>
<li class="fragment">queries by specifying subset of labels (matchers)</li>
<li class="fragment">
labels stored in an inverted index
<ul class="fragment">
<li>(Cortex) in memory for recent (data), distributed K/V store for historic data</li>
<li>scales linearly by retention and throughput</li>
<li>limited in cardinality of given labels (by design)</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Cortex Architecture</h2>
<figure>
<img src="images/cortex-architecture.excalidraw.png">
<figcaption>
<a target="_blank" href="https://cortexmetrics.io/docs/architecture/">https://cortexmetrics.io/docs/architecture/</a>
</figcaption>
</figure>
</section>
<section>
<h2>Stories <span class="note-inline">1/4</span></h2>
<q>After receiving an alert on my service and drilling into the query associated with said alert, I want to quickly see the logs associated with the jobs which produced those timeseries at the time of the alert.</q>
</section>
<section>
<h2>Stories <span class="note-inline">2/4</span></h2>
<q>After a pod or node disappears, I want to be able to retrieve logs from just before it died, so I can diagnose why it died.</q>
</section>
<section>
<h2>Stories <span class="note-inline">3/4</span></h2>
<q>After discovering an ongoing issue with my service, I want to extract a metric from some logs and combine it with my existing time series data.</q>
</section>
<section>
<h2>Stories <span class="note-inline">4/4</span></h2>
<q>I have a legacy job which does not expose metrics about errors - it only logs them. I want to build an alert based on the rate of occurrences of errors in the log.</q>
</section>
<section>
<h2>Proposed Solution</h2>
<ul>
<li class="fragment">index metadata (form of Prometheus-style multidimensional labels)</li>
<li class="fragment">use existing distributed databases and object store systems</li>
</ul>
</section>
<section>
<h2>Architecture</h2>
<ul>
<li class="fragment">agent is Promtail <span class="note-inline">reuse Prometheus code for service discovery, labeling and relabeling</span></li>
<li class="fragment">mirror Cortex architecture <span class="note-inline">on write request</span>
<ul>
<li>distributor/ingester</li>
<li>reuse consistent hash ring</li>
</ul>
</li>
<li class="fragment">
log chunks <span class="note-inline">format important for cost/performance</span>
<ul>
<li>to large to entirely decompress</li>
<li>support streaming <span class="note-inline">read request</span></li>
</ul>
</li>
<li class="fragment"><a target="_blank" href="https://docs.google.com/spreadsheets/d/1zTtln0kKspSWsHtwmamZzSnsHspHQigd-1jotZ8l9gc/edit#gid=0">Compression Stats</a></li>
</ul>
<aside class="notes">
<ul>
<li>'Chunk': logs for a given label set over a certain period</li>
<li>format: important cost and performance</li>
<li>(must) support: appends, seeks and streaming reads</li>
<li>for optimal chunk size consider:
<ul>
<li>per operation cost vs. storage costs</li>
<li>per chunk index load</li>
<li>cost of memory</li>
<li>compression effectiveness => need to be batched</li>
<li>=> approx. 1 MB (compressed)</li>
</ul></li>
<li>consists of a series of blocks
<ul>
<li>first block, gorilla-style encoded time index (<a target="_blank" href="https://www.vldb.org/pvldb/vol8/p1816-teller.pdf">Gorilla: A Fast, Scalable, In-Memory Time Series Database</a> by Facebook)</li>
</ul>
</li>
</ul>
</aside>
</section>
</section>
<section>
<!-- https://grafana.com/docs/loki/latest/fundamentals/architecture/ -->
<section>
<h1>Architecture</h1>
</section>
<section>
<img src="images/loki-architecture.excalidraw.png">
</section>
<section data-visibility="hidden">
<h2>Components</h2>
</section>
<section data-visibility="hidden">
<h3>Distributor</h3>
<ul>
<li>stateless</li>
<li>handling incoming streams<span class="note-inline">hash(unique tenant + label set) / <a target="_blank" href="https://en.wikipedia.org/wiki/Consistent_hashing">Consistent Hashing</a></span></li>
<li>validated for correctness (e.g. labels)</li>
<li>ensure (rate) limit and timestamps</li>
<li>(valid) chunks split into batches, sent to multiple ingesters in parallel</li>
<li><a target="_blank" href="https://www.amazon.science/publications/dynamo-amazons-highly-available-key-value-store">Dynamo</a> style quorum consistency</li>
</ul>
</section>
<section data-visibility="hidden">
<h3>Ingester</h3>
<ul>
<li>writing log data to long-term storage<span class="note-inline">write path</span></li>
<li>returning log data for in-memory queries<span class="note-inline">read path</span></li>
<li>using write ahead log (WAL)</li>
<li>chunks compressed and marked read-only when
<ul>
<li>reached capacity</li>
<li>idle time exceeded (no update occurred)</li>
<li>flush occurs</li>
</ul>
</li>
<li>timestamp ordering
<ul>
<li>accepts out-of-order writes, if enabled</li>
<li>rejected, otherwise</li>
</ul>
</li>
</ul>
</section>
<section data-visibility="hidden">
<h3>Query Frontend</h3>
<ul>
<li>optional/stateless service</li>
<li>adjust query, e.g. splits larger into multiple smaller</li>
<li>holds queries in a (FIFO) queue
<ul>
<li>ensure retry of large queries in case of OOM error</li>
<li>distribute across all queriers</li>
<li>prevent single tenant DoS-ing</li>
</ul>
</li>
<li>caching query result
<ul>
<li>if incomplete, sub-queries calculated and executed</li>
<li>optionally align queries to improve cacheability</li>
</ul>
</li>
</ul>
</section>
<section data-visibility="hidden">
<h3>Querier</h3>
<ul>
<li>handles (execute) queries</li>
<li>fetching from ingesters and long-term storage</li>
<li>applies deduplication <code><pre class="inline">because of replication</pre></code></li>
</ul>
</section>
<section>
<h2>Multi-tenancy</h2>
<ul class="fragment">
<li>HTTP Header <code><pre class="inline">X-Scope-OrgId</pre></code> (multi-tenant mode)</li>
<li><code><pre class="inline">fake</pre></code> otherwise</li>
</ul>
</section>
<section>
<h2 style="margin-top: -1.5em;">Chunk Format</h2>
<code>
<pre class="js:trim-whitespace" style="width: 53em">
┌─────────────────────────────────────────────────────────────────────────────┐
│ Header (Tenant, Labels, ...) │
├─────────────────────────────────────────────────────────────────────────────┤
| | | |
│ MagicNumber (4b) | version (1b) │ encoding (1b) │
│ [0x012EE56A] | [4] | │
├─────────────────────────────────────────────────────────────────────────────┤<───────┐
│ #structuredMetadata (uvarint) │ │
├─────────────────────────────────────────────────────────────────────────────┤ │
│ len (label-1) (uvarint) | label-1 (bytes) │ † │
├─────────────────────────────────────────────────────────────────────────────┤ │
│ ... │ │
├─────────────────────────────────────────────────────────────────────────────┤ │
│ len (label-n) (uvarint) | label-n (bytes) │ † │
├─────────────────────────────────────────────────────────────────────────────┤ │
│ checksum(from structuredMetadata) │ │
├─────────────────────────────────────────────────────────────────────────────┤<─────┐ │
│ block-1 bytes (†) | checksum (4b) │ │ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │
│ ... │ │ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │
│ block-n bytes (†) | checksum (4b) │ │ │
├─────────────────────────────────────────────────────────────────────────────┤<──┐ │ │
│ #blocks (uvarint) │ │ │ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │ │
│ #entries(uvarint) |mint, maxt (varint64)| offset, uncompSize, len (uvarint) │──────┘ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │
│ ... │ │ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │
│ #entries(uvarint) |mint, maxt (varint64)| offset, uncompSize, len (uvarint) │ │ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │
│ checksum(from blocks) │ │ │
├─────────────────────────────────────────────────────────────────────────────┤ │ │
│ structuredMetadata len (8b) | structuredMetadata offset (8b) │────────┘
├─────────────────────────────────────────────────────────────────────────────┤ │
│ blocks len (8b) | blocks offset (8b) │───┘
└─────────────────────────────────────────────────────────────────────────────┘
</pre>
</code>
<ul>
<li>v4 used by schema v13 (structured metadata)</li>
<li><code><pre class="inline">uncompSize</pre></code> since v3</li>
<li><small>†</small> compressed w/ encoding</li>
<li><code><pre class="inline">mint</pre></code>, <code><pre class="inline">maxt</pre></code> minimum/maximum Unix nanosecond timestamp</li>
<li><code><pre class="inline">(u)varint</pre></code> Go package <code><pre class="inline">encoding/binary</pre></code></li>
</ul>
</section>
<section>
<h2>Block Format</h2>
<code>
<pre class="js:trim-whitespace" style="width: 68em">
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ts (varint) | len (*) | log-1 bytes | len(from #symbols) | #symbols (*) | symbol-1 (*) | ... | symbol-2*n (*) │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ts (varint) | len (*) | log-2 bytes | len(from #symbols) | #symbols (*) | symbol-1 (*) | ... | symbol-2*n (*) │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ... │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ ts (varint) | len (*) | log-n bytes | len(from #symbols) | #symbols (*) | symbol-1 (*) | ... | symbol-2*n (*) │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
* = uvarint
uncompressed
</pre>
</code>
<ul>
<li>compressed with gzip, snappy or disabled</li>
<li>symbol → <small><code>key=2*i</code>,<br><code>value=2*i+1</code></small></li>
<li><code><pre class="inline">ts</pre></code> Unix nanosecond timestamp</li>
<li><code><pre class="inline">len</pre></code> bytes of log entry</li>
<li class="fragment">Loki Dev Tool: <a target="_blank" href="https://github.com/grafana/loki/tree/main/cmd/chunks-inspect">chunks-inspect</a></li>
</ul>
</section>
<section>
<h2>Storage</h2>
<ul>
<li class="fragment">Single Storage <span class="note-inline">since Loki 2.0</span>
<ul>
<li>Amazon S3 <span class="note-inline">or API compliant</span></li>
<li>Azure Blob Storage</li>
<li>Baidu Object Storage</li>
<li>Google Cloudstorage</li>
<li>Filesystem <span class="note-inline">only for Monolithic Mode</span></li>
</ul>
</li>
<li class="fragment">Multi-store (deprecated)</li>
</ul>
<!-- https://grafana.com/docs/loki/latest/operations/storage/wal/ -->
</section>
<section>
<h2>Deployment Modes</h2>
<ul>
<li class="fragment">built out of 8 component microservices</li>
<li class="fragment">designed to run as a horizontally-scalable distributed system</li>
<li class="fragment">controlled by <code><pre class="inline">-target</pre></code></li>
</ul>
</section>
<section>
<h3>Monolithic Mode</h3>
<ul>
<li class="fragment"><code><pre class="inline">-target=all</pre></code><span class="note-inline">default</span></li>
<li class="fragment">getting started quickly, small r/w volumes up tp approx. 100GB/day</li>
<li class="fragment">HA by two instances using <code><pre class="inline">memberlist_config</pre></code> and shared object store</li>
<li class="fragment">route via round robin</li>
<li class="fragment">query parallelization is limited</li>
</ul>
</section>
<section>
<h3>Simple Scalable Deployment Mode</h3>
<ul>
<li class="fragment"><code><pre class="inline">-target=read|write|backend</pre></code></li>
<li class="fragment">higher volume, or separate r/w concerns</li>
<li class="fragment">can scale to several TB/day</li>
<li class="fragment">
advantages:
<ul>
<li>higher availability of write path</li>
<li>separately scalable read path</li>
</ul>
</li>
<li class="fragment">requires load balancer in front of Loki</li>
<li class="fragment"><a target="_blank" href="https://github.com/grafana/loki/blob/main/production/docker/README.md">Docker Compose</a> example</li>
</ul>
</section>
<section>
<h3>Microservice Mode</h3>
<ul>
<li class="fragment">each component by <code><pre class="inline">-target=ingester|distributor|…</pre></code><span class="note-inline">#8 microservices</span></li>
<li class="fragment">most efficient but also most complex to set up and maintain</li>
<li class="fragment">works best with Kubernetes deployments</li>
<li class="fragment"><a target="_blank" href="https://github.com/grafana/loki/blob/main/production/nomad/README.md">Nomad</a> example</li>
</ul>
<aside class="notes">
<ul>
<ul>
<li><code>ingester</code></li>
<li><code>distributor</code></li>
<li><code>query-frontend</code></li>
<li><code>query-scheduler</code></li>
<li><code>querier</code></li>
<li><code>index-gateway</code></li>
<li><code>ruler</code></li>
<li><code>compactor</code></li>
</ul>
</ul>
</aside>
</section>
<section>
<h2>Labels</h2>
</section>
<section>
<h3>Best Practices</h3>
<ul>
<li class="fragment">static labels (like regions, clusters, servers)
<ul>
<li>to make it easier to query logs</li>
</ul>
</li>
<li class="fragment">use dynamic labels sparingly
<ul>
<li>low label cardinality, high → many streams (must be loaded!)</li>
<li>long-lived values (e.g. initial path segment on HTTP)</li>
<li>add label for something until you know you need it</li>
<li>will be used frequently in queries</li>
<li>use structured metadata if possible <br>(often parsed, high cardinality, expensive extraction)</li>
</ul>
</li>
<li class="fragment">must always be bounded (⤳ unbounded number of streams)</li>
<li class="fragment">be aware of dynamic labels applied by clients</li>
</ul>
</section>
<section>
<h3>Best Practices <small>(cont'd)</small></h3>
<h4>debugging high cardinality</h4>
use Series API or <code><pre class="inline">logcli series --analyze-labels</pre></code>
<code>
<pre class="js:trim-whitespace" style="width: 80em">
$ logcli series --since=3h --analyze-labels '{}'
2024/12/15 15:44:33 http://localhost:3100/loki/api/v1/series?end=1734273873655967479&match=%7B%7D&start=1734263073655967479
Total Streams: 4
Unique Labels: 5
Label Name Unique Values Found In Streams
service 3 3
container 3 3
job 2 4
hostname 1 3
syslog_identifier 1 1
</pre>
</code>
<code>
<pre class="js:trim-whitespace" style="width: 80em">
$ logcli series --since=3h '{}'
2024/12/15 15:44:11 http://localhost:3100/loki/api/v1/series?end=1734273851263938421&match=%7B%7D&start=1734263051263938421
{hostname="loki", job="journal", syslog_identifier="kernel"}
{container="vagrant-postgres-1", job="containers", service="postgres"}
{container="vagrant-loki-1", hostname="loki", job="containers", service="loki"}
{container="vagrant-grafana-1", hostname="loki", job="containers", service="grafana"}
</pre>
</code>
</section>
<section>
<!-- https://grafana.com/docs/loki/latest/get-started/labels/structured-metadata/ -->
<h3>Structured Metadata</h3>
<ul>
<li>GA v2.9.4 (experimental since v2.9.0), requires chunk format v4</li>
<li class="fragment">attach metadata w/o indexing / including in log line content
<ul>
<li>often required in queries but high cardinality and expensive to extract</li>
</ul>
</li>
<li class="fragment">use(d) if ingesting in OpenTelemetry format</li>
<!-- https://grafana.com/docs/loki/v2.9.x/get-started/labels/structured-metadata/
It is an antipattern to extract information that already exists in your log lines and put it into structured metadata.
-->
</ul>
<!--
$ cat << EOF > structured-metadata.yml
---
# https://grafana.com/docs/loki/latest/clients/promtail/configuration/
server:
http_listen_port: 9081
grpc_listen_port: 0
positions:
filename: /tmp/positions.yml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: structured-metadata
# https://grafana.com/docs/loki/latest/clients/promtail/configuration/#static_configs
static_configs:
- labels:
job: structured-metadata
__path__: /mnt/log/structured-metadata-example.log
pipeline_stages:
- logfmt:
mapping:
stream:
traceID:
- replace:
expression: "(traceID=[0-9a-c]+\\s?)"
- labels:
stream:
- structured_metadata:
traceID:
EOF
--> <!--
$ echo "time=2019-04-30T02:12:41.8443515Z log=\"log message\" traceID=0242ac120002 stream=stderr" | promtail -config.file=structured-metadata.yml --stdin --dry-run --inspect
Clients configured:
----------------------
url: http://loki:3100/loki/api/v1/push
batchwait: 1s
batchsize: 1048576
follow_redirects: true
enable_http2: true
backoff_config:
min_period: 500ms
max_period: 5m0s
max_retries: 10
timeout: 10s
tenant_id: ""
drop_rate_limited_batches: false
stream_lag_labels: ""
[inspect: logfmt stage]:
{stages.Entry}.Extracted["stream"]:
+: stderr
{stages.Entry}.Extracted["traceID"]:
+: 0242ac120002
[inspect: replace stage]:
{stages.Entry}.Entry.Entry.Line:
-: time=2019-04-30T02:12:41.8443515Z log="log message" traceID=0242ac120002 stream=stderr
+: time=2019-04-30T02:12:41.8443515Z log="log message" stream=stderr
[inspect: labels stage]:
{stages.Entry}.Entry.Labels:
-: {__path__="/mnt/log/structured-metadata-example.log", job="structured-metadata"}
+: {__path__="/mnt/log/structured-metadata-example.log", job="structured-metadata", stream="stderr"}
2024-12-16T15:25:32.56309801+0100 {__path__="/mnt/log/structured-metadata-example.log", job="structured-metadata", stream="stderr"}
$ echo "time=2019-04-30T02:12:41.8443515Z log=\"log message\" traceID=0242ac120002 stream=stderr" >> promtail/log/structured-metadata-example.log
$ ~/src/github.com/agebhar1/loki/cmd/chunks-inspect/chunks-inspect -b -l ~/downloads/193cfe4973b_193cfe4973c_27424b05
Chunks file: /home/agebhar1/downloads/193cfe4973b_193cfe4973c_27424b05
Metadata length: 266
Data length: 210
UserID: fake
From: 2024-12-16 14:34:44.923000 UTC
Through: 2024-12-16 14:34:44.924000 UTC (1ms)
Labels:
__name__ = logs
filename = /mnt/log/structured-metadata-example.log
job = structured-metadata
stream = stderr
Format (Version): 4
Encoding: snappy
Found 1 block(s)
Minimum time (from first block): 2024-12-16 14:34:44.923089 UTC
Maximum time (from last block): 2024-12-16 14:34:44.923089 UTC
Block 0: position: 50, original length: 97, minT: 2024-12-16 14:34:44.923089 UTC maxT: 2024-12-16 14:34:44.923089 UTC
2024-12-16 14:34:44.923089 UTC time=2019-04-30T02:12:41.8443515Z log="log message" stream=stderr
Structured Metadata:
traceID = 0242ac120002
Total chunk size of uncompressed data: 92 compressed data: 210 ratio: 0.438
-->
</section>
</section>
<section>
<section>
<h1>Clients</h1>
</section>
<!-- https://grafana.com/docs/loki/latest/clients/ -->
<section>
<ul>
<li><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/">Promtail</a></li>
<li class="fragment"><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/docker-driver/">Docker Driver</a><span class="note-inline">via plugin</span></li>
<li class="fragment"><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/fluentd/">Fluentd</a><span class="note-inline">via plugin</span></li>
<li class="fragment"><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/fluentbit/">Fluent Bit</a><span class="note-inline">via plugin</span></li>
<li class="fragment"><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/logstash/">Logstash</a><span class="note-inline">via plugin</span></li>
<li class="fragment"><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/lambda-promtail/">Lambda Promtail</a></li>
<li class="fragment">…</li>
</ul>
</section>
<section>
<h2>Promtail</h2>
<ul>
<li class="fragment">borrows Prometheus <a target="_blank" href="https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config">service discovery mechanism</a></li>
<li class="fragment">tail logs from
<ul>
<li>files on the same host (traditional, K8S pods, …)</li>
<li>Systemd Journal (Linux)</li>
<li>Windows Event Log</li>
<li>Syslog (Receiver)</li>
<li>Kafka</li>
<li>…</li>
</ul>
</li>
<li class="fragment"><a target="_blank" href="https://grafana.com/docs/loki/latest/send-data/promtail/logrotation/#rename-and-create-recommend">recommends rename and create</a> for log rotation</li>
<li class="fragment">native support for compressed files <code><pre class="inline">*.gz|*.z|*.bz2|*.tar.gz</pre></code></li>
<li class="fragment">uses file to save current positions</li>
<li class="fragment">exposes Prometheus metrics via <code><pre class="inline">/metrics</pre></code></li>
</section>
<section>
<h2>Promtail <small>(cont'd)</small></h2>
<ul>
<!-- https://github.com/grafana/loki/tree/main/clients/pkg/logentry/stages -->
<li>
each job can run multiple pipelines w/ different <a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/">stages</a>:
<ul>
<li class="fragment">filtering:
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/match/">match</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/drop/">drop</a>
</li>
<li class="fragment">parsing:
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/regex/">regex</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/json/">json</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/logfmt/">logfmt</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/multiline/">multiline</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/geoip/">geoip</a>,
…
</li>
<li class="fragment">transform:
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/template/">template</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/pack/">pack</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/decolorize/">decolorize</a>
</li>
<li class="fragment">actions:
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/timestamp/">timestamp</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/output/">output</a>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/send-data/promtail/stages/sampling/">sampling</a><small>†</small>,
<a target="_blank" href="https://grafana.com/docs/loki/latest/send-data/promtail/stages/structured_metadata/">structured_metadata</a><small>† ‡</small>,
<span style="border-bottom: 1px solid white;"><a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/stages/metrics/">metrics</a></span>,
…
<br>
<span class="note-inline">† since v2.9, ‡ experimental</span>
</li>
</ul>
</li>
<li class="fragment">
<a target="_blank" href="https://grafana.com/docs/loki/latest/clients/promtail/troubleshooting/">troubleshooting</a>, e.g. inspecting pipeline stages:<br>
<code>
<pre class="js:trim-whitespace" style="width: 70em">
$ cat my.log | \
promtail -config.file=config.yml --stdin --dry-run --inspect --client.url http://127.0.0.1:3100/loki/api/v1/push
</pre>
</code>
</li>
</ul>
</section>
<section>
<h3>Metrics Action Stage</h3>
<ul>
<li class="fragment">exposed via <code><pre class="inline">/metrics</pre></code></li>
<li class="fragment">based on existence / exact match of extracted data map value</li>
<li class="fragment">types:
<ul>
<li class="fragment">Counter:
<code><pre class="inline">inc</pre></code> or
<code><pre class="inline">add</pre></code>
</li>
<li class="fragment">Gauge:
<code><pre class="inline">set</pre></code>,
<code><pre class="inline">inc</pre></code>,
<code><pre class="inline">dec</pre></code>,
<code><pre class="inline">add</pre></code> or
<code><pre class="inline">sub</pre></code>
</li>
<li class="fragment">Histogram</li>
</ul>
</li>
</ul>
</section>
<section>
<h3>Structured Metadata Stage</h3>
<ul>
<li class="fragment">takes data from extracted map (json, logfmt, …)</li>
<li class="fragment">updates structured metadata (k/v map)</li>
<li class="fragment">use replace stage to remove from log line</li>
</ul>
</section>
<section>
<h2>mtail</h2>
<q><a target="_blank" href="https://github.com/google/mtail">mtail</a> is a tool for extracting metrics from application logs to be exported into a timeseries database or timeseries calculator for alerting and dashboarding.</q>
<code>
<pre class="js:trim-whitespace" style="width: 60em">
# simple line counter
counter lines_total
/$/ {
lines_total++
}
</pre>
</code>
</section>
</section>
<section>
<section>
<h1 style="text-transform: none;">LogQL</h1>
</section>
<section>
<h2>Log queries</h2>
<code>
<pre class="js:trim-whitespace" style="width: 60%;">
|= -- line filter
!=
|~
!~
{ stream selector } | -- log pipeline
= -- label filter
!=
=~
!~
> >=
< <=
~ RegEx (Go RE2)
</pre>
</code>
<ul>
<li class="fragment">Label filter expression
<ul>
<li>value types: String, Duration, Number, Bytes</li>
<li>chain predicates: <code><pre class="inline">and</pre></code> (or <code><pre class="inline">,</pre></code>), <code><pre class="inline">or</pre></code></li>
</ul>
</li>
<li class="fragment">Parser expression:
<code><pre class="inline">json</pre></code>,
<code><pre class="inline">logfmt</pre></code>,
<code><pre class="inline">pattern …</pre></code>,
<code><pre class="inline">regex …</pre></code>
</li>
<li class="fragment">others: <code><pre class="inline">line_format …</pre></code>, <code><pre class="inline">label_format …</pre>, <code><pre class="inline">drop …</pre></code></code></li>
</ul>
</section>
<section>
<h2>Metric queries</h2>
<ul>
<li class="fragment">Log (metadata) range aggregation (over duration):<br>
<code><pre class="inline">rate(…)</pre></code>,
<code><pre class="inline">count_over_time(…)</pre></code>,
<code><pre class="inline">bytes_rate(…)</pre></code>,<br>
<code><pre class="inline">bytes_over_time(…)</pre></code>,
<code><pre class="inline">absent_over_time(…)</pre></code>
</li>
<li class="fragment">Unwrapped range aggregation (by label)
<ul>
<li>requires <code><pre class="inline">unwrap <function> …</pre></code></li>
<li>functions: <code><pre class="inline">duration_seconds(…)</pre></code>, <code><pre class="inline">bytes(…)</pre></code></li>
<li>aggregations:
<code><pre class="inline">rate(…)</pre></code>,
<code><pre class="inline">rate_counter(…)</pre></code>,
<code><pre class="inline">sum_over_time(…)</pre></code>,
<code><pre class="inline">avg_over_time(…)</pre></code>,<br>
<code><pre class="inline">max_over_time(…)</pre></code>,
<code><pre class="inline">min_over_time(…)</pre></code>, …
</li>
</ul>
</li>
<li class="fragment">Aggregate operators
<ul>
<li>like PromQL, aggregate the elements of a single vector</li>
<li>over labels:
<code><pre class="inline">sum(…)</pre></code>,
<code><pre class="inline">avg(…)</pre></code>,
<code><pre class="inline">min(…)</pre></code>, …
</li>
<li>of elements
<code><pre class="inline">count(…)</pre></code>,
<code><pre class="inline">topk(…)</pre></code>,
<code><pre class="inline">bottom(…)</pre></code>
</li>
<li><code><pre class="inline">sort(…)</pre></code>, <code><pre class="inline">sort_desc(…)</pre></code> by sample value</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Template functions</h2>
<ul>
<li class="fragment">Go text template <code><pre class="inline">{{ … }}</pre></code></li>
<li class="fragment">used in <code><pre class="inline">| line_format</pre></code> and <code><pre class="inline">| label_format</pre></code></li>
<!-- https://github.com/grafana/loki/blob/main/pkg/logql/log/fmt.go -->
<li class="fragment">several functions:
<ul>
<li>text: <code><pre class="inline">lower</pre></code>, <code><pre class="inline">upper</pre></code>, <code><pre class="inline">replace</pre></code>…</li>
<li>numbers: <code><pre class="inline">add</pre></code>, <code><pre class="inline">sub</pre></code>, <code><pre class="inline">mul</pre></code>…</li>
<li>date: <code><pre class="inline">now</pre></code>, <code><pre class="inline">toDate</pre></code>, <code><pre class="inline">toDateInZone</pre></code>…</li>
<li><code><pre class="inline">b64enc</pre></code>, <code><pre class="inline">b64dec</pre></code>…</li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Matching IP addresses</h2>
<ul>
<li class="fragment"><code><pre class="inline">ip("<pattern>")</pre></code>
<li class="fragment">line (<code><pre class="inline">|= !=</pre></code>)/label (<code><pre class="inline">=, !=</pre></code>) filter</li>
<li class="fragment">single IPv4, IPv6 addresses</li>
<li class="fragment">CIDR patterns</li>
<li class="fragment">chainable</li>
</ul>
</section>
</section>
<section>
<section>
<h1>Demo</h1>
<code>
<pre class="js:trim-whitespace">
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'opensuse/Tumbleweed.x86_64'...
==> default: Generating MAC address for NAT networking...
…
==> default: Running provisioner: shell...
default: Running: inline script
default: Retrieving repository 'openSUSE-Tumbleweed-Non-Oss' metadata [.........done]
default: Building repository 'openSUSE-Tumbleweed-Non-Oss' cache [...done]
…
==> default: Running provisioner: shell...
default: Running: inline script
default: Archive: /tmp/logcli-linux-amd64.zip
default: inflating: /home/vagrant/.local/bin/logcli-linux-amd64
$ vagrant ssh
Have a lot of fun...
vagrant@loki:~> cd /vagrant/
vagrant@loki:/vagrant> docker-compose up -d
…
</pre>
</code>
<ul>
<li>open <a target="_blank" href="http://192.168.56.10:3000">http://192.168.56.10:3000</a><span class="note-inline">admin/s3cr3t</span></li>
<li>if run w/o Vagrant <code><pre class="inline">$ cp dotenv .env</pre></code> and adjust</li>
</ul>
</section>
<!--
promtail_custom_log_bytes_total{filename="/mnt/log/a.log"}
promtail_custom_log_lines_total{exported_job="various"}
-->
<section data-visibility="hidden">
<code>
<pre class="js:trim-whitespace">
$ # format => apache_common, apache_combined, apache_error, rfc3164, rfc5424, json, logfmt
$ k6 run -e FORMAT=logfmt --iterations 100 k6/script.js
$ watch 'date >> /home/vagrant/promtail/log/date.log'
</pre>
</code>
<div style="font-size: small; text-align: right !important;padding-right: 2.75em;">generate log test data</div>
</section>
<section data-visibility="hidden">
<h3>Log Queries</h3>
<code>
<pre class="js:trim-whitespace">
{app=~"k6"} |= "status=200" # all requests w/ HTTP Status 200
{app=~"k6"} |~ "status=404" # all requests w/ HTTP Status 404
{app=~"k6"} |~ "status=5" # all requests w/ HTTP Status 5xx
{app=~"k6"} |= "method=POST" | logfmt | __error__ = "" | bytes > 20KB # all POST requests w/ >20KB
{job="journal", syslog_identifier="sshd-session"} # users invoked `sudo`
|~ `pam_unix\([a-z]+:session\)`
|= "opened"
| pattern "pam_unix(<type>:session): session opened for user <_> by <user>(uid=<_>)"
| __error__ = ""
| label_format type="{{.type}}"
| line_format "{{.user}}"
</pre>
</code>
</section>
<section data-visibility="hidden">
<h3>Metric Queries</h3>
<code>
<pre class="js:trim-whitespace">
sum(count_over_time(
{job="journal", syslog_identifier="sshd-session"}[$__interval]
|~ `pam_unix\(sudo:session\)`
|= "opened"
| pattern "pam_unix(sudo:session): session opened for user <_> by <user>(uid=<_>)"
| __error__ = ""
| line_format "{{.user}}")) by (user)
sum(count_over_time({app=~"k6", format="logfmt"}[$__interval] | logfmt)) by (method)
sum(rate({app=~"k6", format="logfmt"}[$__interval] | logfmt | unwrap bytes)) by (method)
</pre>
</code>
</section>
<section data-visibility="hidden">
<h3>Structured Metadata</h3>
<ul>
<li>labels: <code><pre class="inline">job</pre></code>, <code><pre class="inline">syslog_identifier</pre></code></li>
<li>structured metadata: <code><pre class="inline">level</pre></code></li>
</ul>
<code>
<pre class="js:trim-whitespace">
{job="journal", syslog_identifier="kernel", level="error"} <span style="color: #8b0303;"># ✘</span> <span style="color: darkgrey;">can not be used in stream selector</span>
{job="journal", syslog_identifier="kernel"} | level="error" <span style="color: #0e8b03;"># ✔</span> <span style="color: darkgrey;">no parsing expression required</span>
</pre>
</code>
</section>
</section>
<section>
<h2 class="font-bloody">Resources</h2>
<ul style="font-size: 0.6em">
<li><a target="_blank" href="https://grafana.com/docs/loki/latest/">Grafana Loki</a><span class="note-inline">Documentation</span></li>
<li><a target="_blank" href="https://docs.google.com/document/d/11tjK_lvp1-SVsFZjgOTr1vV3-q6vBAsZYIQ5ZeYBkyM/view">Loki Design Document</a><span class="note-inline">2018 (Google Docs)</span></li>
<li><a target="_blank" href="https://grafana.com/blog/2019/04/15/how-we-designed-loki-to-work-easily-both-as-microservices-and-as-monoliths/">How we designed Loki to work easily both as Microservices and as Monoliths</a><span class="note-inline">04/2019 (Blog Post)</span></li>
<li><a target="_blank" href="https://grafana.com/blog/2020/02/19/how-loki-reduces-log-storage/">How Loki Reduces Log Storage</a><span class="note-inline">02/2020 (Blog Post)</span></li>
<li><a target="_blank" href="https://medium.com/lonto-digital-services-integrator/grafana-loki-configuration-nuances-2e9b94da4ac1">Grafana Loki Configuration Nuances</a><span class="note-inline">05/2023 (Blog Post)</span></li>
<li><a target="_blank" href="https://www.elastic.co/guide/en/elasticsearch/reference/8.8/documents-indices.html">Elasticsearch</a><span class="note-inline">Data in: documents and indices</span></li>
<li><a target="_blank" href="https://cortexmetrics.io/docs/architecture/">Cortex Architecture</a><span class="note-inline">Documentation</span></li>
<li><a target="_blank" href="https://dev.to/arslan_ah/how-to-use-consistent-hashing-in-a-system-design-interview-33ge">How to Use Consistent Hashing in a System Design Interview?</a><span class="note-inline">Blogpost</span></li>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/Inverted_index">Inverted Index</a><span class="note-inline">Wikipedia</span></li>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/Consistent_hashing">Consistent Hashing</a><span class="note-inline">Wikipedia</span></li>
<li><a target="_blank" href="https://www.amazon.science/publications/dynamo-amazons-highly-available-key-value-store">Dynamo: Amazon’s highly available key-value store</a><span class="note-inline">by Amazon</span></li>
<li><a target="_blank" href="https://www.vldb.org/pvldb/vol8/p1816-teller.pdf">Gorilla: A Fast, Scalable, In-Memory Time Series Database</a><span class="note-inline">by Facebook</span></li>
<li><a target="_blank" href="https://github.com/grafana/loki/blob/main/production/docker/README.md">Simple Scalable Deployment Mode example</a><span class="note-inline">by Grafana</span></li>
<li><a target="_blank" href="https://github.com/grafana/loki/blob/main/production/nomad/README.md">Microservice Mode example</a><span class="note-inline">by Grafana</span></li>
<li><a target="_blank" href="https://github.com/grafana/loki/tree/main/cmd/chunks-inspect">chunks-inspect</a><span class="note-inline">Loki Dev Tool</span></li>
<li><a target="_blank" href="https://github.com/oklog/oklog">OK Log</a><span class="note-inline">by Peter Bourgon</span></li>
<li><a target="_blank" href="https://github.com/google/mtail">mtail</a><span class="note-inline">extract internal monitoring data from application logs for collection into a timeseries database (by Google)</span></li>
<li>Font <a target="_blank" href="https://www.fontspace.com/redcap-font-f3647"><span class="font-redcap">Redcap</span></a></li>
</ul>
</section>
</div>
</div>
<!-- https://github.com/astefanutti/decktape -->
<script type="module" src="/src/main.ts"></script>
</body>
</html>