Skip to main content

OpenTelemetry hook sampler

Project description

opentelemetry-hook-sampler

Custom function based sampler for opentelemetry-python.

How to install

$ pip install opentelemetry-hook-sampler

How to use

import opentelemetry.trace
from opentelemetry.sdk.resources import SERVICE_NAME
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry_hook_sampler import HookSampler


def sampler_hook():
    """
    Sample 1 in 10 traces.
    
    The example is static, but you can get info from any context available
    in your application. E.g. http request, celery task, thread locals, etc.
    """
    return 10


resource = Resource(attributes={SERVICE_NAME: "foo"})
sampler = HookSampler(sampler_hook)
provider = TracerProvider(resource=resource, sampler=sampler)
opentelemetry.trace.set_tracer_provider(provider)

Honeycomb specific

Honeycomb expects a SampleRate attribute to normalize data. It is not OpenTelemetry spec, but it is supported through opentelemetry_hook_sampler.HoneycombHookSampler.

Sampler examples

django-threadlocals

Be careful, threadlocals module don't work with async Django.

from threadlocals.threadlocals import get_current_request


def sampler_hook():
    request = get_current_request()
    # 10% /foo requests
    if request.path == '/foo':
        return 10
    return 1

django-g

from django_g import get_current_request


def sampler_hook():
    request = get_current_request()
    # 10% /foo requests
    if request.path == '/foo':
        return 10
    return 1

celery

import celery


def sampler_hook():
    task_name = celery.current_app.current_worker_task.request.task
    # 10% foo.bar.baz tasks
    if task_name == 'foo.bar.baz':
        return 10
    return 1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

opentelemetry-hook-sampler-0.0.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file opentelemetry-hook-sampler-0.0.3.tar.gz.

File metadata

File hashes

Hashes for opentelemetry-hook-sampler-0.0.3.tar.gz
Algorithm Hash digest
SHA256 09838a55fc8fa881007a69c48331ffd011cf96707878fb2c39af5bac1c7f5ab9
MD5 25d1f621cbe18bcbe62de38909153c85
BLAKE2b-256 bcba7f35b4e625b5ba9835cdadb2b952dd08243fc55391f49cc1c23166752487

See more details on using hashes here.

File details

Details for the file opentelemetry_hook_sampler-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_hook_sampler-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 eaa7e0d9fa1df3b0be231afea974e5b94f8bbb7c44913a2ab83aaa295c520940
MD5 533084e6a2fae1a8d1c0f1e9045b5ac3
BLAKE2b-256 4a4b12da2372be7c6ab775a7f5e1ac50ecc6e53af7c729a1c3e35dc6a98070b1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page