Skip to content

Commit 5288e05

Browse files
committed
Inlining opentelemetry-instrumentation-digma tracer until PR merged
- digma-ai/opentelemetry-instrumentation-digma#41 - Updated tensorflow-federated==0.40.0
1 parent 82056cc commit 5288e05

File tree

6 files changed

+182
-25
lines changed

6 files changed

+182
-25
lines changed

.github/workflows/stack-integration-arm64_tests.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ jobs:
171171
run: |
172172
python -c "import platform; import os; os.system('pip install jaxlib==0.3.14 -f https://whls.blob.core.windows.net/unstable/index.html') if platform.system().lower() == 'windows' else ''"
173173
174-
- name: Install tff python 3.10
175-
if: steps.changes.outputs.stack == 'true'
176-
run: |
177-
pip install ./packages/grid/backend/wheels/tensorflow_federated-0.36.0-py2.py3-none-any.whl
178-
179174
- name: Install Docker Compose
180175
if: steps.changes.outputs.stack == 'true' && runner.os == 'Linux'
181176
shell: bash

packages/grid/backend/backend.dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ RUN --mount=type=cache,target=/root/.cache if [ $(uname -m) != "x86_64" ]; then
3737
# pip install --user /wheels/tensorflow_compression-2.10.0-cp310-cp310-linux_aarch64.whl; \
3838
fi
3939

40-
# install custom built python 3.10 wheel
41-
RUN --mount=type=cache,target=/root/.cache if [ $(uname -m) = "x86_64" ]; then \
42-
pip install --user /wheels/tensorflow_federated-0.36.0-py2.py3-none-any.whl; \
43-
fi
40+
# # install custom built python 3.10 wheel
41+
# RUN --mount=type=cache,target=/root/.cache if [ $(uname -m) = "x86_64" ]; then \
42+
# pip install --user /wheels/tensorflow_federated-0.36.0-py2.py3-none-any.whl; \
43+
# fi
4444

4545
WORKDIR /app
4646
COPY grid/backend/requirements.txt /app
@@ -91,7 +91,7 @@ COPY syft/src/syft/cache /app/syft/src/syft/cache
9191

9292
# install tff
9393
RUN --mount=type=cache,target=/root/.cache if [ $(uname -m) = "x86_64" ]; then \
94-
pip install --user tensorflow-federated==0.38.0; \
94+
pip install --user tensorflow-federated==0.40.0; \
9595
fi
9696

9797
# install syft

packages/syft/setup.cfg

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ syft =
5858
pyzmq==23.2.1 # https://github.com/zeromq/pyzmq/issues/1764
5959

6060
telemetry =
61-
opentelemetry-api==1.12.0
62-
opentelemetry-exporter-jaeger==1.12.0
63-
opentelemetry-instrumentation==0.33b0
64-
opentelemetry-instrumentation-requests==0.33b0
65-
opentelemetry-instrumentation-digma==0.7.0
66-
opentelemetry-sdk==1.12.0
61+
opentelemetry-api==1.14.0
62+
opentelemetry-sdk==1.14.0
63+
opentelemetry-exporter-jaeger==1.14.0
64+
opentelemetry-instrumentation==0.35b0
65+
opentelemetry-instrumentation-requests==0.35b0
66+
; opentelemetry-instrumentation-digma==0.9.0
6767

6868
install_requires =
6969
%(syft)s

packages/syft/src/syft/telemetry.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ def noop(func: Any) -> Any:
5858

5959
opentelemetry.instrumentation.requests.RequestsInstrumentor().instrument()
6060

61-
# third party
62-
from opentelemetry.instrumentation.digma.trace_decorator import (
63-
instrument as _instrument,
64-
)
61+
# relative
62+
# from opentelemetry.instrumentation.digma.trace_decorator import (
63+
# instrument as _instrument,
64+
# )
65+
#
66+
# until this is merged:
67+
# https://github.com/digma-ai/opentelemetry-instrumentation-digma/pull/41
68+
from .trace_decorator import instrument as _instrument
6569

6670
return _instrument
6771

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# until this is merged:
2+
# https://github.com/digma-ai/opentelemetry-instrumentation-digma/pull/41
3+
4+
# stdlib
5+
import asyncio
6+
from functools import wraps
7+
import inspect
8+
from typing import Callable
9+
from typing import Dict
10+
11+
# third party
12+
from opentelemetry import trace
13+
from opentelemetry.semconv.trace import SpanAttributes
14+
from opentelemetry.trace import Tracer
15+
16+
17+
class TracingDecoratorOptions:
18+
class NamingSchemes:
19+
@staticmethod
20+
def function_qualified_name(func: Callable):
21+
return func.__qualname__
22+
23+
default_scheme = function_qualified_name
24+
25+
naming_scheme: Callable[[Callable], str] = NamingSchemes.default_scheme
26+
default_attributes: Dict[str, str] = {}
27+
28+
@staticmethod
29+
def set_naming_scheme(naming_scheme: Callable[[Callable], str]):
30+
TracingDecoratorOptions.naming_scheme = naming_scheme
31+
32+
@staticmethod
33+
def set_default_attributes(attributes: Dict[str, str] = None):
34+
for att in attributes:
35+
TracingDecoratorOptions.default_attributes[att] = attributes[att]
36+
37+
38+
def instrument(
39+
_func_or_class=None,
40+
*,
41+
span_name: str = "",
42+
record_exception: bool = True,
43+
attributes: Dict[str, str] = None,
44+
existing_tracer: Tracer = None,
45+
ignore=False
46+
):
47+
"""
48+
A decorator to instrument a class or function with an OTEL tracing span.
49+
:param cls: internal, used to specify scope of instrumentation
50+
:param _func_or_class: The function or span to instrument, this is automatically assigned
51+
:param span_name: Specify the span name explicitly, rather than use the naming convention.
52+
This parameter has no effect for class decorators: str
53+
:param record_exception: Sets whether any exceptions occurring in the span and the stacktrace are recorded
54+
automatically: bool
55+
:param attributes:A dictionary of span attributes. These will be automatically added to the span. If defined on a
56+
class decorator, they will be added to every function span under the class.: dict
57+
:param existing_tracer: Use a specific tracer instead of creating one :Tracer
58+
:param ignore: Do not instrument this function, has no effect for class decorators:bool
59+
:return:The decorator function
60+
"""
61+
62+
def decorate_class(cls):
63+
for name, method in inspect.getmembers(cls, inspect.isfunction):
64+
# Ignore private functions, TODO: maybe make this a setting?
65+
if not name.startswith("_"):
66+
67+
if isinstance(inspect.getattr_static(cls, name), staticmethod):
68+
setattr(
69+
cls,
70+
name,
71+
staticmethod(
72+
instrument(
73+
record_exception=record_exception,
74+
attributes=attributes,
75+
existing_tracer=existing_tracer,
76+
)(method)
77+
),
78+
)
79+
else:
80+
setattr(
81+
cls,
82+
name,
83+
instrument(
84+
record_exception=record_exception,
85+
attributes=attributes,
86+
existing_tracer=existing_tracer,
87+
)(method),
88+
)
89+
90+
return cls
91+
92+
# Check if this is a span or class decorator
93+
if inspect.isclass(_func_or_class):
94+
return decorate_class(_func_or_class)
95+
96+
def span_decorator(func_or_class):
97+
98+
if inspect.isclass(func_or_class):
99+
return decorate_class(func_or_class)
100+
101+
# sig = inspect.signature(func_or_class)
102+
103+
# Check if already decorated (happens if both class and function
104+
# decorated). If so, we keep the function decorator settings only
105+
undecorated_func = getattr(func_or_class, "__tracing_unwrapped__", None)
106+
if undecorated_func:
107+
# We have already decorated this function, override
108+
return func_or_class
109+
110+
setattr(func_or_class, "__tracing_unwrapped__", func_or_class)
111+
112+
tracer = existing_tracer or trace.get_tracer(func_or_class.__module__)
113+
114+
def _set_semantic_attributes(span, func: Callable):
115+
span.set_attribute(SpanAttributes.CODE_NAMESPACE, func.__module__)
116+
span.set_attribute(SpanAttributes.CODE_FUNCTION, func.__qualname__)
117+
span.set_attribute(SpanAttributes.CODE_FILEPATH, func.__code__.co_filename)
118+
span.set_attribute(SpanAttributes.CODE_LINENO, func.__code__.co_firstlineno)
119+
120+
def _set_attributes(span, attributes_dict):
121+
if attributes_dict:
122+
for att in attributes_dict:
123+
span.set_attribute(att, attributes_dict[att])
124+
125+
@wraps(func_or_class)
126+
def wrap_with_span_sync(*args, **kwargs):
127+
name = span_name or TracingDecoratorOptions.naming_scheme(func_or_class)
128+
with tracer.start_as_current_span(
129+
name, record_exception=record_exception
130+
) as span:
131+
_set_semantic_attributes(span, func_or_class)
132+
_set_attributes(span, TracingDecoratorOptions.default_attributes)
133+
_set_attributes(span, attributes)
134+
return func_or_class(*args, **kwargs)
135+
136+
@wraps(func_or_class)
137+
async def wrap_with_span_async(*args, **kwargs):
138+
name = span_name or TracingDecoratorOptions.naming_scheme(func_or_class)
139+
with tracer.start_as_current_span(
140+
name, record_exception=record_exception
141+
) as span:
142+
_set_semantic_attributes(span, func_or_class)
143+
_set_attributes(span, TracingDecoratorOptions.default_attributes)
144+
_set_attributes(span, attributes)
145+
return await func_or_class(*args, **kwargs)
146+
147+
if ignore:
148+
return func_or_class
149+
150+
wrapper = (
151+
wrap_with_span_async
152+
if asyncio.iscoroutinefunction(func_or_class)
153+
else wrap_with_span_sync
154+
)
155+
wrapper.__signature__ = inspect.signature(func_or_class)
156+
157+
return wrapper
158+
159+
if _func_or_class is None:
160+
return span_decorator
161+
else:
162+
return span_decorator(_func_or_class)

tox.ini

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,15 +547,11 @@ commands =
547547

548548
; install syft and hagrid
549549
bash -c 'if [[ "$HAGRID_FLAGS" == *"latest"* ]]; then \
550-
pip install --pre --force pytest hagrid syft; \
550+
pip install --pre --force pytest hagrid syft[tff]; \
551551
else \
552552
pip install -e packages/hagrid; \
553553
fi'
554554

555-
; install tff
556-
pip install tensorflow_federated==0.40.0
557-
558-
559555
; fix windows encoding
560556
- chcp 65001
561557

0 commit comments

Comments
 (0)