Ho hlokomela Flask Microservices ka Prometheus

Melao e 'maloa ea khoutu mme sesebelisoa sa hau se hlahisa metrics, wow!

E le hore u utloisise hore na prometheus_ e sebetsa joangflask_export mohlala o fokolang o lekane:

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app)

@app.route('/')
def main():
    return 'OK'

Ke phetho seo u se hlokang hore u qale! Ka ho kenya thepa le mola bakeng sa ho qala PrometheusMetrics, o tla fumana metrics nako ea kopo и kopa li-counters, e bontšitsoeng qetellong /metrics Sesebelisoa sa Flask seo e ngolisitsoeng ho sona, hammoho le metrics eohle ea kamehla eo u e fumanang ho tloha setsing Laeborari ea bareki ba Prometheus.

u ka fumana ho bonolo ho sebelisa mohlala sebakeng sa polokelo ea GitHub e tsamaisang mohlala Prometheus и grafana mmoho le sesebelisoa sa demo ho hlahisa metrics e tla shebahala tjena:

Ho hlokomela Flask Microservices ka Prometheus

U tla boela u fumane lethathamo la litsupa ho README mehlala e hlahang ho dashboard, mmoho le lipotso tsa Prometheus tse hlahisang li-dashboard.

phetoho

Ho na le likhetho tse ngata tsa tlhophiso laebraring, sheba README mehlala ea morero oa bona ka tlhaloso e khutšoanyane.

Tlhophiso ea mantlha e bontšitsoe ka holimo. Etsa feela mohlala PrometheusMetrics, a re e bitseng metrics, ebe u e sebelisa ho hlalosa metrics e meng eo u batlang ho e bokella ka ho khabisa mesebetsi:

  • @metrics.counter(..)

  • @metrics.gauge(..)

  • @metrics.summary(..)

  • @metrics.histogram(..)

Li-counter li bala mehala, 'me tse ling li bokella metrics ho latela nako ea mehala eo. O ka hlalosa lileibole bakeng sa e 'ngoe le e 'ngoe ea tsena, mohlomong u sebelisa mekhoa ea kopo kapa karabo. Ka mohlala:

from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)

# group by endpoint rather than path
metrics = PrometheusMetrics(app, group_by='endpoint')

@app.route('/collection/:collection_id/item/:item_id')
@metrics.counter(
    'cnt_collection', 'Number of invocations per collection', labels={
        'collection': lambda: request.view_args['collection_id'],
        'status': lambda resp: resp.status_code
    })
def get_item_from_collection(collection_id, item_id):
    pass

Mohlala o ka holimo, ho tobetsa ntlha ea ho qetela /collection/10002/item/76 e tla etsa hore counter e eketsehe, mohlala cnt_collection{pokello = "10002", boemo = "200"}, 'me u tla fumana metrics ea kamehla (bakeng sa ntlha ka 'ngoe mohlaleng ona) ho tsoa laebraring ea kamehla:

  • flask_http_request_duration_seconds - Nako ea kopo ea HTTP ka metsotsoana bakeng sa likopo tsohle tsa Flask ka mokhoa, tsela le boemo

  • flask_http_request_total - Palo eohle ea likopo tsa HTTP ka mekhoa le maemo

Ho na le likhetho tsa ho tlola ho lateloa ha lintlha tse ikhethileng, ho ngola lintlha tse ling tsa kamehla kapa ho tlola tse thathamisitsoeng ka holimo, kapa ho sebelisa metric e ts'oanang ho liphetho tse ngata. Sheba README projeke ho bona se fumanehang.

app = Flask(__name__)
metrics = PrometheusMetrics(app)

@app.route('/')
def main():
    pass  # requests tracked by default

@app.route('/skip')
@metrics.do_not_track()
def skip():
    pass  # default metrics are not collected

# custom metric to be applied to multiple endpoints
common_counter = metrics.counter(
    'by_endpoint_counter', 'Request count by endpoints',
    labels={'endpoint': lambda: request.endpoint}
)

@app.route('/common/one')
@common_counter
def endpoint_one():
    pass  # tracked by the custom and the default metrics

@app.route('/common/two')
@common_counter
def endpoint_two():
    pass  # also tracked by the custom and the default metrics

# register additional default metrics
metrics.register_default(
    metrics.counter(
        'by_path_counter', 'Request count by request paths',
        labels={'path': lambda: request.path}
    )
)

Laeborari e na le likeketso tse bonolo bakeng sa lilaebrari tse tsebahalang tsa ho sebetsa ka bongata joalo ka uWSGI le Gunicorn. U ka boela ua fumana mehlala e menyenyane ea linyeoe tsa ts'ebeliso e lebisitsoeng, ho kenyelletsa le multiprocessing.

Pokello ea metrics

Joalokaha ho boletsoe ka holimo, laebrari e fana ka ntlha ea ho qetela ka ho sa feleng /metrics ka kopo ea Flask, e ka sebetsang e le sepheo sa Sehahi sa Prometheus.

Mohlala oa dashboard o ka holimo, o ka lebisa Prometheus ho sesebelisoa sa Flask se nang le litlhophiso tsa kamehla ka tlhophiso ena:

scrape_configs:
  - job_name: 'example'

    dns_sd_configs:
      - names: ['app']
        port: 5000
        type: A
        refresh_interval: 5s

Sheba mohlala o feletseng ho GitHub polokelo. Sena se nka hore Prometheus a ka fumana maemo a kopo ea Flask ea hau ho http://app:5000/metrics, moo lebitso la sebaka sa ts'ebeliso le ka rarollang liaterese tse ngata tsa IP, mohlala, ha li kena Kubernetes kapa Sesepa sa Docker.

Haeba ho hlahisa metrics endpoint ka tsela ena ho sa u tšoanele, mohlomong hobane u sa batle ho lumella phihlello ea kantle ho eona, u ka e thibela habonolo ka ho feta. tsela=Ha ho letho ha ho etsoa mohlala PrometheusMetrics.

from flask import Flask, request
from prometheus_flask_exporter import PrometheusMetrics

app = Flask(__name__)
metrics = PrometheusMetrics(app, path=None)

...

metrics.start_http_server(5099)

Joale u ka sebelisa qala_http_server(port)ho bula ntlha ena boema-kepeng bo fapaneng ba HTTP, 5099 mohlaleng o ka holimo. Ntle le moo, haeba u thabetse hore pheletso e be ts'ebelisong e ts'oanang ea Flask, empa u hloka ho fetola tsela ea eona ho tloha /metrics, o ka fetisa URI e 'ngoe joalo ka parameter ea tsela, kapa oa e sebelisa register_endpoint(..)ho e kenya hamorao.

litšupiso

Haeba u etsa qeto ea ho leka, ikutloe u lokolohile ho bula taba ho GitHub kapa u siee maikutlo, maikutlo le litlhahiso!

Спасибо!

Source: www.habr.com

Eketsa ka tlhaloso