Skip to content

Instantly share code, notes, and snippets.

@ziozzang
Last active February 21, 2019 00:30
Show Gist options
  • Select an option

  • Save ziozzang/859d25acbf2aaab5e5756f89b050c2c6 to your computer and use it in GitHub Desktop.

Select an option

Save ziozzang/859d25acbf2aaab5e5756f89b050c2c6 to your computer and use it in GitHub Desktop.
Elastic APM
  • Python3 에서 동작 가능
pip install elastic-apm

  • 환경 변수 설정
export ELASTIC_APM_APP_NAME="example"
export ELASTIC_APM_SERVER_URL="http://apm-server.kube-system:8200"
ELASTIC_APM_SECRET_TOKEN=

  • 파이썬 코드
import os
os.environ

import logging
from elasticapm.conf.constants import ERROR, KEYWORD_MAX_LENGTH, SPAN, TRANSACTION
import elasticapm
from elasticapm import Client

#####
client = Client()

handler = client.get_handler()
handler.setLevel(logging.DEBUG)


# https://www.elastic.co/guide/en/apm/agent/python/current/configuration.html
client = Client({'SERVICE_NAME': 'example', 'ELASTIC_APM_SERVER_URL':'http://apm-server.kube-system:8200'})#, **defaults)

client = Client(service_name="example", environment="production", server_url='http://apm-server.kube-system:8200', hostname = "host-10-1-2-70", version = "1.2.3", transport_class="elasticapm.transport.http.Transport", debug=True)

#, transport_class": "elasticapm.transport.http.AsyncTransport", "async_mode": True)


try:
    x = int("five")
except ValueError:
    client.capture_exception()
    
try:
    1 / 0
except Exception:
    client.capture_exception()
    
client.capture_message('Billing process succeeded.')
client.capture_message("foo", handled=False)

client.begin_transaction('myapp.zxcv')
client.begin_transaction('myapp.asdf')

client.begin_transaction('myapp.qwer')

client.end_transaction('myapp.qwer',"failed") # /*processor.status*/ 

client.end_transaction('myapp.asdf',"ok") # /*processor.status*/ 

client.end_transaction('myapp.zxcv',404) # /*processor.status*/ 

elasticapm.set_transaction_name('myapp.billing_process')

client.set_transaction_result('SUCCESS')
end_transaction('asdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment