instana / python-sensor

:snake: Python Distributed Tracing & Metrics Sensor for Instana
https://www.instana.com/
MIT License
69 stars 31 forks source link

Automatic memcached instrumentation #200

Closed mmanciop closed 2 years ago

mmanciop commented 4 years ago
import opentracing
import os
import pylibmc
import schedule
import sys
import time
import wrapt

memcached_hostname = os.environ['MEMCACHED_HOSTNAME']
memcached_port = os.environ['MEMCACHED_PORT']

mc = pylibmc.Client([memcached_hostname])

@wrapt.decorator
def instana_pylibmc(wrapped, instance, args, kwargs):
    with opentracing.tracer.start_active_span('memcache') as batch_scope:
        batch_scope.span.set_tag('span.kind', 'exit')
        batch_scope.span.set_tag('memcached.connection', '%s:%s' % (memcached_hostname, memcached_port))
        batch_scope.span.set_tag('memcached.operation', 'get')
        batch_scope.span.set_tag('memcached.key', args[0])
        try:
            rv = wrapped(*args, **kwargs)
            is_cache_hit = "false" if rv is None else "false"
            batch_scope.span.set_tag('memcached.cache_hit', is_cache_hit)
            return rv
        except Exception as e:
            batch_scope.span.log_exception(e)

@instana_pylibmc
def get_key(my_key):
    value = mc[my_key]

def job():
    with opentracing.tracer.start_active_span('Scheduled Job') as batch_scope:
        batch_scope.span.set_tag('span.kind', 'entry')
        get_key('my_key')

def main():
    schedule.every(1).seconds.do(job)

    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == "__main__":
    main()
Ferenc- commented 2 years ago

Moved to Kanbanize, closing for now.