Skip to content

Commit 3934f6e

Browse files
committed
Update Dockerfile to use local wheel file and enable observability in myapp_2.py
1 parent 400b8ad commit 3934f6e

File tree

7 files changed

+29
-50
lines changed

7 files changed

+29
-50
lines changed
-390 Bytes
Binary file not shown.

dist/singlestore_pulse-0.3.tar.gz

-567 Bytes
Binary file not shown.

examples/otel-collector/Dockerfile.myapp_2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ WORKDIR /app
55

66
COPY requirements.txt .
77
RUN pip install --no-cache-dir -r requirements.txt
8-
RUN pip3 install git+https://github.com/singlestore-labs/singlestore-pulse.git@master
8+
COPY singlestore_pulse-0.3-py3-none-any.whl .
9+
RUN pip install --no-cache-dir singlestore_pulse-0.3-py3-none-any.whl
10+
# RUN pip3 install git+https://github.com/singlestore-labs/singlestore-pulse.git@master
911

1012
COPY myapp_2.py .
1113
COPY .env .

examples/otel-collector/myapp.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ def cftocf_endpoint(request: Request, body: Item):
326326

327327
def main():
328328
# write to otel collector
329-
_ = Pulse(
330-
otel_collector_endpoint="http://otel-collector:4317",
331-
)
329+
# _ = Pulse(
330+
# otel_collector_endpoint="http://otel-collector:4317",
331+
# )
332332

333333
# Create a FastAPI app
334334
uvicorn.run(app, host="0.0.0.0", port=8000)

examples/otel-collector/myapp_2.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from opentelemetry.sdk._logs import LoggingHandler
1515

16-
from pulse_otel import Pulse, pulse_agent, pulse_tool
16+
from pulse_otel import Pulse, pulse_agent, pulse_tool, observe
1717

1818
import logging
1919
from tenacity import retry, stop_after_attempt, wait_fixed
@@ -179,7 +179,7 @@ def health_check():
179179
def root():
180180
return {"message": "Welcome to the Pulse OTel FastAPI agent!"}
181181

182-
# @pulse_agent("getdata")
182+
@pulse_agent("getdata")
183183
@app.post("/getdata")
184184
def cftocf_endpoint(request: Request, body: Item):
185185
"""
@@ -255,8 +255,9 @@ def http_req(body: Item):
255255
logger.error(f"Request error occurred when calling myapp_2: {e}")
256256
raise HTTPException(status_code=500, detail="Failed to make request to myapp_2 service")
257257

258-
# @pulse_agent("getdata")
258+
259259
@app.post("/go_py_py")
260+
@observe("cftocf_endpoint")
260261
def cftocf_endpoint(request: Request, body: Item):
261262
"""
262263
This is the target endpoint that myapp will call.
@@ -284,9 +285,9 @@ def cftocf_endpoint(request: Request, body: Item):
284285

285286
def main():
286287
# write to otel collector
287-
# _ = Pulse(
288-
# otel_collector_endpoint="http://otel-collector:4317",
289-
# )
288+
_ = Pulse(
289+
otel_collector_endpoint="http://otel-collector:4317",
290+
)
290291

291292
# Create a FastAPI app
292293
uvicorn.run(app, host="0.0.0.0", port=8000)
10.3 KB
Binary file not shown.

pulse_otel/main.py

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -319,58 +319,34 @@ def wrapper(*args, **kwargs):
319319

320320

321321
def observe(name):
322-
"""
323-
A decorator factory that instruments a function for observability using opentelemetry tracing.
324-
Args:
325-
name (str): The name of the span to be created for tracing.
326-
Returns:
327-
Callable: A decorator that wraps the target function, extracting opentelemetry tracing context
328-
from the incoming request (if available), and starts a new tracing span using
329-
the provided name. If no context is found, a new span is started without context.
330-
Behavior:
331-
- Adds session ID to span attributes if available in kwargs.
332-
- Attempts to extract a tracing context from the 'request' argument or from positional arguments.
333-
- Starts a tracing span with the extracted context (if present) or as a new trace.
334-
- Logs debug information about the tracing context and span creation.
335-
- Supports usage both within and outside of HTTP request contexts.
336-
Example:
337-
@observe("my_function_span")
338-
def my_function(request: Request, ...):
339-
...
340-
"""
341322
def decorator(func):
342-
decorated_func = agent(name)(func)
343-
logger.debug("Decorating function with observe:", name)
344-
323+
print("Decorating:", func.__name__)
345324
@functools.wraps(func)
346325
def wrapper(*args, **kwargs):
347-
add_session_id_to_span_attributes(**kwargs)
348-
request: Request = kwargs.get("request")
326+
print(f"[observe] wrapper called for {func.__name__}")
327+
request = kwargs.get("request")
328+
print(f"request: {request}")
329+
print(f"kwargs: {kwargs}")
349330
if request is None:
350331
for arg in args:
351332
if isinstance(arg, Request):
352333
request = arg
353334
break
354335

355-
# Extract context from request if available
356-
ctx = extract(request.headers) if request else None
357-
358-
if ctx:
359-
logger.debug(f"Starting span with context: {ctx}")
360-
# Start span with context
361-
with tracer.start_as_current_span(name, context=ctx, kind=SpanKind.SERVER):
362-
return decorated_func(*args, **kwargs)
336+
if request:
337+
print(f"[observe] Request headers: {request.headers}")
338+
ctx = extract(request.headers) if request else None
339+
if ctx:
340+
print(f"[observe] Extracted context: {ctx}")
341+
with tracer.start_as_current_span(name, context=ctx, kind=SpanKind.SERVER):
342+
return func(*args, **kwargs)
343+
else:
344+
print("[observe] No context found in request headers.")
363345
else:
364-
logger.debug("No context found, starting span without context.")
365-
366-
# Start span without context
367-
# This is useful for cases where we want to start a span without any specific context
368-
# e.g., when the function is called outside of an HTTP request context
369-
# or when we want to create a fresh new trace or context is not properly propagated.
370-
return decorated_func(*args, **kwargs)
346+
print("[observe] No request found.")
371347

348+
return func(*args, **kwargs)
372349
return wrapper
373-
374350
return decorator
375351

376352
class CustomFileSpanExporter(SpanExporter):

0 commit comments

Comments
 (0)