Skip to main content

OpenTelemetry instrumentation for Python regex libraries

Test

This library provides OpenTelemetry instrumentation for three Python regex libraries: the standard library re, and the regex and google-re2 packages. It emits spans for all regex operations (see Supported Operations) and for compiled pattern methods.

Installation

Install the package; add the extras for the libraries you want to instrument:

pip install opentelemetry-instrumentation-re                    # stdlib re (always available)
pip install opentelemetry-instrumentation-re[regex]             # regex package
pip install opentelemetry-instrumentation-re[google-re2]        # google-re2 package

Compatibility: stdlib re works out of the box. To instrument regex, install version >= 2021.0; for google-re2, install version >= 1.0. All recent versions are supported.

Usage

Manual instrumentation

Instrument the library or libraries you use. Each exposes the same operations; use the instrumentor that matches your imports.

stdlib re:

import re
from opentelemetry_instrumentation_re import ReInstrumentor

ReInstrumentor().instrument()
re.search(r"\d+", "hello 42 world")
pattern = re.compile(r"\d+")
pattern.findall("a1 b2 c3")

regex package:

import regex
from opentelemetry_instrumentation_re import RegexInstrumentor

RegexInstrumentor().instrument()
regex.search(r"\d+", "hello 42 world")

google-re2 package:

import re2
from opentelemetry_instrumentation_re import GoogleRe2Instrumentor

GoogleRe2Instrumentor().instrument()
re2.search(r"\d+", "hello 42 world")

You can instrument more than one of these in the same process if your application uses multiple regex libraries.

Auto-instrumentation

If you already run your app with OpenTelemetry Python auto-instrumentation, you don't need to change any code: install this package (and the [regex] and/or [google-re2] extras for the libraries you use). The agent discovers and enables the instrumentors automatically. To disable them, use OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=re,regex,google_re2 as needed. For setup of the agent itself, see the linked doc.

Uninstrumenting

Call uninstrument() on the same instrumentor you used to stop tracing for that library:

from opentelemetry_instrumentation_re import ReInstrumentor, RegexInstrumentor, GoogleRe2Instrumentor

ReInstrumentor().uninstrument()         # re
RegexInstrumentor().uninstrument()      # regex
GoogleRe2Instrumentor().uninstrument()  # google-re2

Span Attributes

Spans are named {library}.{operation} (e.g. re.search, regex.findall, google_re2.sub). The following attributes are added to spans:

  • re.operation - operation name (e.g. search, match, sub, findall).
  • re.pattern - full pattern string (may contain sensitive data; consider your export/backend policy).
  • re.string_length - length of the input string.
  • re.match_count - number of matches found (only for findall and subn operations).
  • re.library.name - instrumented library: re, regex, or google_re2.

Example Span

After calling re.search(r"\d+", "hello 42 world") with the stdlib re instrumentor, the following span is created:

{
  "name": "re.search",
  "kind": "INTERNAL",
  "attributes": {
    "re.operation": "search",
    "re.pattern": "\\d+",
    "re.string_length": 14,
    "re.library.name": "re"
  }
}

Example with re.findall(r"\d", "a1 b2 c3"), which includes the re.match_count attribute:

{
  "name": "re.findall",
  "kind": "INTERNAL",
  "attributes": {
    "re.operation": "findall",
    "re.pattern": "\\d",
    "re.string_length": 8,
    "re.match_count": 3,
    "re.library.name": "re"
  }
}

Supported Operations

Operation Module-level Function Compiled Pattern Method Instrumented Libraries Notes
search re.search() pattern.search() re, regex, google-re2
match re.match() pattern.match() re, regex, google-re2
fullmatch re.fullmatch() pattern.fullmatch() re, regex, google-re2
split re.split() pattern.split() re, regex, google-re2
findall re.findall() pattern.findall() re, regex, google-re2 Includes re.match_count
finditer re.finditer() pattern.finditer() re, regex, google-re2
sub re.sub() pattern.sub() re, regex, google-re2
subn re.subn() pattern.subn() re, regex, google-re2 Includes re.match_count

Learn more

Development & Contributing

Contributions are welcome. See CONTRIBUTING.md for setup, running tests, linting, Git hooks (prek), and development workflow. Maintainers: see RELEASING.md for how to publish releases.

License

Apache-2.0

Metadata

Release files for opentelemetry-instrumentation-re 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opentelemetry-instrumentation-re 0.1.0
File Size Uploaded
opentelemetry_instrumentation_re-0.1.0.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opentelemetry-instrumentation-re 0.1.0
File Interpreter ABI Platform
opentelemetry_instrumentation_re-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / opentelemetry_instrumentation_re-0.1.0.tar.gz

Download URL opentelemetry_instrumentation_re-0.1.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5d33cec0c0b799f8ec7666627639ac8b342592e25b8e32da0b3f52da4c67b692
BLAKE2b-256 checksum
How to use checksums
2b9490c384cbd0540ddf77f178e72dfa71e988d9071dfdea5fe7319542f5423c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / opentelemetry_instrumentation_re-0.1.0-py3-none-any.whl

Download URL opentelemetry_instrumentation_re-0.1.0-py3-none-any.whl
Size 13.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3360c97bdba07ff239a017f8ad536e25c22437247b165c16cc8dac057272c0c3
BLAKE2b-256 checksum
How to use checksums
ed9895227cbcdd563426f477c7b27f8fad03a8316e23ff1e957f867238bca067
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page