Skip to content

Commit

Permalink
Merge pull request #27 from xhdnoah/master
Browse files Browse the repository at this point in the history
feat: add trace_provider arg
  • Loading branch information
nicholasxuu committed May 10, 2024
2 parents f6c22ae + 329d1f4 commit 8128cc0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

---

## [1.2.0] - 2024-04-29
## [1.2.1] - 2024-04-29

### Added:

Expand Down
4 changes: 2 additions & 2 deletions cachext/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pickle
import time
from threading import RLock, Lock
from .otel import otel_instrument


class BaseBackend(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -68,8 +69,7 @@ class Redis(BaseBackend):

def __init__(self, prefix=None, default_ttl=600, **kwargs):
import redis
from opentelemetry.instrumentation.redis import RedisInstrumentor
RedisInstrumentor().instrument()
otel_instrument()
self._client = redis.StrictRedis(**kwargs)
self.prefix = prefix
self.default_ttl = default_ttl
Expand Down
5 changes: 3 additions & 2 deletions cachext/exts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import backends
from .cache import cached
from .otel import otel_instrument


CACHE_LABELS = ("prefix_name", "func_name")
Expand Down Expand Up @@ -47,8 +48,8 @@ def __init__(self, ns='REDIS_'):

def init_app(self, app):
import redis
from opentelemetry.instrumentation.redis import RedisInstrumentor
RedisInstrumentor().instrument()

otel_instrument(app)
opts = app.config.get_namespace(self.ns)
self._pool = redis.ConnectionPool(**opts)
self._client = redis.StrictRedis(connection_pool=self._pool)
Expand Down
19 changes: 19 additions & 0 deletions cachext/otel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import opentelemetry.trace as trace
from opentelemetry.instrumentation.redis import RedisInstrumentor


def sync_once(func):
def wrapper(*args, **kwargs):
if not wrapper._done:
wrapper._done = True
return func(*args, **kwargs)

wrapper._done = False
return wrapper


@sync_once
def otel_instrument(app=None):
if app is None or app.config.get_namespace("OTEL_").get("enable", False):
RedisInstrumentor().instrument(
tracer_provider=trace.get_tracer_provider())

0 comments on commit 8128cc0

Please sign in to comment.