7,540
edits
Changes
→Metrikák gyűjtése logokból
<br>
==HAproxy log struktúrastructure==
https://www.haproxy.com/blog/introduction-to-haproxy-logging/
<br>
<pre>
Aug 6 20:53:30 192.168.122.223 haproxy[39]: 192.168.42.1:50708 [06/Aug/2019:20:53:30.267] public be_edge_http:mynamespace:test-app-service/pod:test-app-57574c8466-qbtg8:test-app-service:172.17.0.12:8080 1/0/0/321/321 200 135 - - --NI 2/2/0/1/0 0/0 "GET /test/slowresponse/1 HTTP/1.1"
<br>
<br>
<br>
==grok-exporter bemutatásaintroduction==A grokGrok-exporter egy olyan eszköz, ami logokat képes reguláris kifejezések alapján feldolgozni, amiből elő tudja állítani is a tool that can process logs based on regular expressions to produce 4 alapvető basic types of prometheus metrika típustmetrics:
* gauge
* counter
* kvantilis
https://github.com/fstab/grok_exporter/blob/master/CONFIG.md<br>
<br>
<br>
===Alternatív megoldásokAlternative Solutions ==='''Fluentd''':<br>A '''fluentdFluentd'''-vel is megoldható a feladatalso solves the problem. Ehhez három To do this, you need to use three fluentd plugin-plugins (I haven't kell használni (ezt nem probáltam kitried this):
* fluent-plugin-rewrite-tag-filter
* fluent-plugin-prometheus
'''mtail''':<br>
https://github.com/google/mtail
<br>
===Config fájlConfiguration file===A The configuration of grok-exporter konfigurációja a can be found in '''/etc/grok_exporter/config.yml''' fájlban van. There are 5 részre osztható sections.
* global:
* input: Megmondja, hogy honnan és hogyan olvassa Tells you where and how to retrieve logs. Can be a logokat. Lehet stdin, file és and webhook. Mi a We will use the file inputot fogjuk használniinput. * grok: A Location of the grok patternek helyepatterns. A The Docker image-ben ez a will have this /grok/patterns mappa leszfolder. * metrics: Ez a legfontosabb részThis is the most important part. Itt kell egyenként definiálni a metrikákat és a hozzá tartozó reguláris kifejezést Here you need to define the metrics and the associated regular expression (in the form of grok patternek formájábanpatterns)* server: Milyen porton hallgatózzon a szerverWhat port the server should listen to.
<br>
====Metrics====
* label: A találati csoportoknak lehet nevet adniYou can name the result groups. A névre lehet hivatkozni a The name can be referenced in the label szekcióbansection, amiből létre fog hozni egy olyan címkét, aminek az értéke which will create a parsolt adat leszlabel whose value will be the parsed data.
<br>
====match====A In match-ben fel kell írni egy reguláris kifejezést , you have to write a regular expression from grok építő kockákbólbuilding cubes. Azt feltételezzük, hogy az egyes elemeket It is assumed that each element is separated by a pause in the log-ban egy szünet választja el. Minden egyes építő kocka Each build cube has the shape '''%{PATTERN-NÉVNAME}''' alakú, ahol where PATTERN NAME must exist in a PATTERN-NÉV-nek léteznie kell valamelyik pattern gyűjteménybencollection. A legáltalánosabb típus a The most common type is '''%{DATA}''', ami egy tetszőleges adatstruktúrára vonatkozik, ami nem tartalmaz szünetetwhich refers to an arbitrary data structure that does not contain a break. There are several patterns that are combined from multiple elementary patterns. Több olyan If you want the regular expression described by the pattern is van, ami több elemi pattern-ből van kombinálva. Ha azt akarjuk hogy to be a pattern-el leírt reguláris kifejezésből találati csoport is képződjönresult group, nevet kell adni a patternekyou must name the patterns, plfor example: <prePre>%{DATA:} this_is_the_name}</prePre>Ekkor The value of the field found by the pattern-el megtalált mező értéke bele fog kerülni a will then be included in the variable '''this_is_the_name''' változóba, amire lehet hivatkozni a metrika értékének a meghatározásánál illetve a címke legyártásnálwhich can be referenced when defining the value of the metric or when producing the label.
<br>
====labels====A You can refer to patterns named in the labels szekcióban nevesített pattern-ekre lehet hivatkozni. Ekkor az adott logsorból parszolt mező értékét fogja adni a definiált címkéneksection. PlThis will give the value of the field parsed from the given log string to the defined label. a For example, using '''%{DATA:this_is_the_name}''' pattern használata esetén felírhatjuk a következő címkét, you could write the following tag: <br><prePre>
mylabel: '{{.this_is_the_name}}'
</prePre>EkkorThen, ha a if the field described by the%{DATA} pattern által leírt mező értéke was 'myvalue' volt, akkor a metrikára rá fog kerülni egy ilyen címkethen the metric would be labeled with the following: '''{mylabel="myvalue"}'''<br>Nézzünk egy példátLet's look at an example: <br>Adott a következő The following log sorline is given: <prePre>7/30.07./2016 142:37:03 PM adam 1.5</prePre>És a következő metrika szabály a And the following metric rule in grok config-ban:
<source lang="C++">
metrics:
user: '{{.user}}'
</source>
<pre>
# HELP Example counter metric with labels.
<br>
====Metrika értékének meghatározásaDetermine the value of a metric ====A For a counter típusú metrikánál nincs szükség a metrika értékét meghatározni-type metric, mert ott azt fogja számolniyou do not need to determine the value of the metric, hogy hány illeszkedő logsort találtbecause it will count the number of matching logs found. Ezzel ellentétben az összes többi típusnál meg kell adniIn contrast, for all other types, hogy mit tekintünk az értéknekyou have to specify what is considered a value. Ezt a This should be specified in the '''value''' szekcióban kell megadnisection, ahol be kell hivatkozni egy nevesített where a named grok pattern-t a from the match szekcióból ugyan úgy section must be referenced in the same way as Go templét formában, ahogy a címkéket is definiáltuktemplates as defined in the tags. Pl adott a következő két logsorEg the following two log lines are given: <prePre>7/30.07./2016 142:37:03 PM adam 17/30.07./2016 142:37:03 adam PM Adam 5</prePre>És erre az alábbi And for this we define the following histogram-ot definiáljuk, ami két vödörből állwhich consists of two buckets, az buckets 1-es és and 2-es vödörből:
<source lang="C++">
metrics:
user: '{{.user}}'
</source>
<pre>
# HELP Example counter metric with labels.
<br>
====FüggvényekFunctions ====A metrika értékekre You can apply functions to the values of the metric (valuevalues) és a címkékre is lehet függvényeket alkalmazniand to the tags. A függvények a Functions must be grok-exporter version '''0.2.7'''-es grok-exporter verzió kell vagy újabbor later. Lehet használni string manipulációs függvényeket és aritmetikai függvényeket isString manipulation functions and arithmetic functions can also be used. A következő két argumentumú aritmetikai függvények támogatottakThe following two arguments arithmetic functions are supported:
* add
* subtract
* multiply
* divide
</source>
# HELP Example counter metric with labels.
# TYPE grok_example_lines histogram
grok_example_lines_bucket{user="adam", le="1"} 0grok_example_lines_bucket{user="adam", le="2"} 0grok_example_lines_bucket{user="adam", le="+Inf"} 2
...
</prePre> Mivel a két érték Since the two values will change to 1000 ill and 5000-re fog módosulnirespectively, ezért mindkettő az both will fall into the infinite kategóriába fog esnicategory.
<br>
<br>
==Creating a grok config fájl elkészítésefile ==Össze kell állítani egy olyan You need to compile a grok pattern-t ami illeszkedik a that fits in the HAproxy access-log sorokra, és képes kigyűjteni az összes számunkra fontos attribútumotlines and can extract all the attributes that are important to us:
* válasz kiszolgálásának szumma idejetotal time to respond
* haproxy instance id
* openshfit service névtérnamespace* pod névname
<br>
[0.1, 0.2, 0.4, 1, 3, 8, 20, 60, 120]
</prePre>A válaszidőket tartalmazó metrikáknak konvenció szerint a következő a neve: Response time metrics by convention are called '''<prefix>_http_request_duration_seconds'''
'''config.yml'''
* '''type: file''' -> fájlból olvassuk a logokatread logs from file* '''path: /var/log/messages''' -> Az The rsyslog szerver a server writes logs to /var/log/messages mappába írja a logokat alapértelmezetten by default* '''readall: true''' -> mindig az egész always reads the entire log fájlt beolvassafile. Ezt csak tesztelésre szabad így használniThis should only be used for testing, éles környezetbenin a live environment, ezt mindig and should always be set to false-ra kell állítani. * '''patterns_dir: ./patterns''' -> A Pattern definitions can be found in the docker image-ben itt találhatók a pattern definíciók* <pre>value: "{{divide .Tt 1000}}"</pre> A kiszolgálási idő a The serving time in the HAproxy log-ban miliszekundumban van, ezt konvertálni kell szekundumrais in milliseconds and must be converted to seconds. * '''port: 9144''' -> Ezen a porton lesz elérhető a This port will provide the /metrics végpontendpoint.
<br>
{{warning|nem szabad éles környezetben elfelejteni a do not forget to set the value of '''readall''' értékét to'''false'''-ra állítani, mert nagyon lerontja in a hatásfokotlive environment as this will greatly reduce efficiency}}
<br>
<br>
===Online grok tesztertester ===Több There are several online grok tesztelő eszköz is léteziktesting tools. Ezekkel nagyon hatékonyan össze lehet állatni a szükséges These can be used to compile the required grok pattern-tvery effectively: https://grokdebug.herokuapp.com/
:[[File:ClipCapIt-190808-170333.PNG]]
<br>
==making docker image elkészítése==A The grok-exporter docker image elérhető a is available on the docker hub-on több változatban in several versions. The only problem with them is. A gond velük csak az, hogy nem tartalmazzák az that they do not include the rsyslog szervertserver, amire szükségünk van, hogy a what we need is for HAproxy közvetlen el tudja küldeni a logokat a to send logs directly to the grok-exporter podokank. <br>
docker-hub link: https://hub.docker.com/r/palobo/grok_exporter <br>
<br>
<br>
<br>
<br>
===Dockerfile===A We will start with '''palobo/grok_exporter''' Dockerfile-ból fogunk kiindulni, de ki fogjuk azt egészíteni az but will complement it with the rsyslog installációval és portoljuk installation and port it to centos-re: https://github.com/berkiadam/haproxy-metrics/tree/master/grok- CentOS-exporter-centos
<br>
➲[[File:Grok-exporter-docker-build.zip|Dokcer Download all files required for Docker image buld-hez szükséges összes fájl letöltésebuild]]
<br>
CMD sh -c "nohup /usr/sbin/rsyslogd -i ${PID_DIR}/pid -n &" && ./grok_exporter -config /grok/config.yml
</source>
{{note|Fontos, hogy a grok-exporter-ből legalább a It is important that we use at least version 0.2.7of grok-es verziót használjukexporter, abban jelent meg először a függvények kezelésethe function handling first appeared}}
<br>
<br>
<pre>
$ModLoad omstdout.so
<br>
===Lokális Local build és lokális tesztand local test ===Első körben a lokális docker démonnal fogjuk First, we will build-elni a the docker image-t, hogy tudjuk lokálisan futtatni tesztelés céljábólwith the local docker daemon so that we can run it locally for testing. Később majd ezt a Later we will build this on the minishfit VM-en fogjuk build-elni, mert csak onnan fogjuk tudni feltölteni a since we will only be able to upload it to the minishfit docker registry-from there. Since we will be. Mivel majd egy távoli uploading the image to a remote (nem a lokálisnot local) docker repository-ba akarjuk majd az image-t felölteni, fontos, hogy betartsuk az elnevezési konvenciókatit is important to follow the naming conventions: <prePre><repo URL>:<repo port>/<névtérnamespace>/<image-névname>:<tag></prePre>
<pre>
Aug 6 20:53:30 192.168.122.223 haproxy[39]: 192.168.42.1:50708 [06/Aug/2019:20:53:30.267] public be_edge_http:mynamespace:test-app-service/pod:test-app-57574c8466-qbtg8:test-app-service:172.17.0.12:8080 1/0/0/321/321 200 135 - - --NI 2/2/0/1/0 0/0 "GET /test/slowresponse/1 HTTP/1.1"
<br>
<br>
# docker logs grok
<br>
<pre>
...
<br>
<br>
<pre>
# docker exec -it grok /bin/bash