Skip to main content

OpenTelemetry instrumentation for Python's re (regex) module

Project description

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

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_instrumentation_re-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file opentelemetry_instrumentation_re-0.1.0.tar.gz.

File metadata

  • Download URL: opentelemetry_instrumentation_re-0.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for opentelemetry_instrumentation_re-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5d33cec0c0b799f8ec7666627639ac8b342592e25b8e32da0b3f52da4c67b692
MD5 07cbbee4656d084c48e2870566051937
BLAKE2b-256 2b9490c384cbd0540ddf77f178e72dfa71e988d9071dfdea5fe7319542f5423c

See more details on using hashes here.

File details

Details for the file opentelemetry_instrumentation_re-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: opentelemetry_instrumentation_re-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for opentelemetry_instrumentation_re-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3360c97bdba07ff239a017f8ad536e25c22437247b165c16cc8dac057272c0c3
MD5 e7943cadae173cf554a9e7ffeab64cf0
BLAKE2b-256 ed9895227cbcdd563426f477c7b27f8fad03a8316e23ff1e957f867238bca067

See more details on using hashes here.

Supported by

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