Skip to content

vuonglv1612/apm-agent-utils

Repository files navigation

Elastic APM Agent Utils

This package can help you create Elastic APM instrumentations using regex.

Dependencies

  1. python2.7+ || python3.6+
  2. elastic-apm 5.4.0+

Installation

pip install apm-agent-utils

Usage

Create a file named instrumentations.py

# instrumentations.py
from apm_agent_utils.instrumentation import InstrumentationBuilder

builder = InstrumentationBuilder("Test")
builder.add_instrument("logic.logic1", r"^hello.*")  # finding and wrapping your funtions by regex
builder.add_instrument("logic.logic2", r".*")

Test = builder.create_instrument()

In your app.py, adding the following lines at top of the file.

import elasticapm
from apm_agent_utils.utils import add_instrumentation

add_instrumentation("instrumentations.Test")
elasticapm.instrument()

==> app.py

# app.py
import elasticapm
from apm_agent_utils.utils import add_instrumentation
from elasticapm.contrib.flask import ElasticAPM
from flask import Flask

add_instrumentation("instrumentations.Instrument")
elasticapm.instrument()

app = Flask(__name__)
app.config['ELASTIC_APM'] = {
    'SERVICE_NAME': 'vuonglv_test',
    'SECRET_TOKEN': '#####',
    'SERVER_URL': 'http://##.##.##.##:8200/',
    'DEBUG': True
}

apm = ElasticAPM(app)

main.py - your controllers

# main.py
from app import app

from logic.logic1 import hello
from logic.logic2 import foo, bar


@app.route("/")
def run():
    print(hello())
    print(foo())
    print(bar())
    return {"ok": True}


if __name__ == "__main__":
    app.run(debug=True)

Project structure look like this:
structure
Run app

python3 main.py

Get your endpoint

curl --location --request GET 'http://localhost:5000'

Goto your APM dashboard, result look like this: successful dashboard

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages