Skip to content

Instantly share code, notes, and snippets.

@lamoboos223
Created March 3, 2024 05:05
Show Gist options
  • Save lamoboos223/a04d81cd3f54831aadd10934cf017885 to your computer and use it in GitHub Desktop.
Save lamoboos223/a04d81cd3f54831aadd10934cf017885 to your computer and use it in GitHub Desktop.
pip install jaeger-client
from jaeger_client import Tracer, SpanContext, Config

# Replace these with your actual configuration
SERVICE_NAME = "your-application-name"
COLLECTOR_HOST = "localhost"
COLLECTOR_PORT = 5778

def configure_tracer():
    config = Config(
        config={
            "sampler": {
                "type": "const",
                "param": 1,  # Sample all requests (optional, adjust as needed)
            },
            "local_agent": {
                "reporting_host": COLLECTOR_HOST,
                "reporting_port": COLLECTOR_PORT,
            },
            "service_name": SERVICE_NAME,
        }
    )
    return Tracer(config=config, name=SERVICE_NAME)

tracer = configure_tracer()  # Initialize tracer globally

def traced_login(username, password):
    with tracer.start_span("login", tags={"username": username}) as span:
        # Simulate login logic (replace with your actual implementation)
        login_successful = username == "user1" and password == "correct_password"  # Don't use real passwords

        # Don't log the password! (Security concern)
        span.set_tag("login_successful", str(login_successful))

        return login_successful  # Return the login result

# Example usage (replace with your actual logic)
def handle_login_request(username, password):
    login_result = traced_login(username, password)
    if login_result:
        print(f"Login successful for user: {username}")
    else:
        print("Login failed!")

if __name__ == "__main__":
    handle_login_request("user1", "correct_password")  # Replace with actual values
@lamoboos223
Copy link
Author

logs payload

from flask import Flask, request
from flask_opentracing import FlaskTracer
from jaeger_client import Config
import requests


app = Flask(__name__)


def initialize_tracer():
    config = Config(
        config={
            'sampler': {'type': 'const', 'param': 1},
            'logging': True,
        },
        service_name='hello-world',
    )
    return config.initialize_tracer()

flask_tracer = FlaskTracer(initialize_tracer, True, app)


@app.route('/hi', methods=["POST"])
def hi():
    """
        curl --location 'http://127.0.0.1:5000/hi' \
        --header 'Content-Type: application/json' \
        --data '{
            "school": "KSU",
            "age": 27
        }'
    """
    body = request.data.decode("utf-8")
    max = 100
    logged_body = body[:max] if body else "no body supplied"
    span = flask_tracer.get_span()
    span.set_tag("payload_lama", logged_body)
    return "hi"

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

doesn't log payload

from flask import Flask, request
from flask_opentracing import FlaskTracer
from jaeger_client import Config
import requests

app = Flask(__name__)

@app.route('/hi')
def hi():
    return "hi"

def initialize_tracer():
    config = Config(
        config={
            'sampler': {'type': 'const', 'param': 1},
            'logging': True,
        },
        service_name='hello-world',
    )
    return config.initialize_tracer()

flask_tracer = FlaskTracer(initialize_tracer, True, app)

if __name__ == '__main__':
    app.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment