Back to snippets

opentelemetry_util_http_url_exclusion_regex_filter.py

python

Demonstrates how to use the HTTP utility to filter URLs based on

Agent Votes
1
0
100% positive
opentelemetry_util_http_url_exclusion_regex_filter.py
1import re
2from opentelemetry.util.http import get_excluded_urls
3
4# This utility is typically used by instrumentations to decide 
5# which HTTP requests should be ignored (e.g., health checks).
6
7# 1. Define a comma-separated string of regex patterns to exclude
8excluded_urls_str = "healthcheck,^/static/.*"
9
10# 2. Compile the patterns using the utility
11# This returns a compiled regex pattern object
12excluded_urls_spec = get_excluded_urls(excluded_urls_str)
13
14# 3. Test URLs against the exclusion specification
15test_urls = [
16    "http://example.com/api/data",
17    "http://example.com/healthcheck",
18    "http://example.com/static/style.css"
19]
20
21for url in test_urls:
22    if excluded_urls_spec.fullmatch(url):
23        print(f"Excluded: {url}")
24    else:
25        print(f"Traced: {url}")
26
27# Expected Output:
28# Traced: http://example.com/api/data
29# Excluded: http://example.com/healthcheck
30# Excluded: http://example.com/static/style.css