Skip to content

Commit 75445ed

Browse files
author
Noah Hummel
authored
feat: distributed tracing (#39)
* WIP * feat: introduce distributed tracing extension for cloud events * chore: downgrade to python 3.10 * chore: don't test on windows * chore: don't test on macos because docker won't work
1 parent 51cc7ae commit 75445ed

File tree

15 files changed

+302
-455
lines changed

15 files changed

+302
-455
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ analyze:
44
@poetry run pylint eventsourcingdb_client_python tests
55

66
test:
7-
@poetry run pytest
7+
@poetry run pytest --maxfail=1
88

99
clean:
1010

eventsourcingdb_client_python/event/event.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from datetime import datetime
22
from typing import TypeVar
33

4-
from .tracing import TracingContext
54
from ..errors.validation_error import ValidationError
65
from .event_context import EventContext
76

@@ -20,7 +19,8 @@ def __init__(
2019
time: datetime,
2120
data_content_type: str,
2221
predecessor_hash: str,
23-
tracing_context: TracingContext = None
22+
trace_parent: str = None,
23+
trace_state: str = None
2424
):
2525
super().__init__(
2626
source,
@@ -31,13 +31,15 @@ def __init__(
3131
time,
3232
data_content_type,
3333
predecessor_hash,
34-
tracing_context
34+
trace_parent,
35+
trace_state
3536
)
3637
self.data = data
3738

3839
@staticmethod
3940
def parse(unknown_object: dict) -> Self:
4041
event_context = super(Event, Event).parse(unknown_object)
42+
4143
data = unknown_object.get('data')
4244
if not isinstance(data, dict):
4345
raise ValidationError(
@@ -54,7 +56,8 @@ def parse(unknown_object: dict) -> Self:
5456
event_context.time,
5557
event_context.data_content_type,
5658
event_context.predecessor_hash,
57-
event_context.tracing_context
59+
event_context.trace_parent,
60+
event_context.trace_state
5861
)
5962

6063
def to_json(self):

eventsourcingdb_client_python/event/event_candidate.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from dataclasses import dataclass
22

3-
from .tracing import TracingContext
43
from .validate_subject import validate_subject
54
from .validate_type import validate_type
65

@@ -11,7 +10,8 @@ class EventCandidate:
1110
subject: str
1211
type: str
1312
data: dict
14-
tracing_context: TracingContext | None = None
13+
trace_parent: str = None
14+
trace_state: str = None
1515

1616
def validate(self) -> None:
1717
validate_subject(self.subject)
@@ -25,7 +25,9 @@ def to_json(self):
2525
'type': self.type
2626
}
2727

28-
if self.tracing_context is not None:
29-
json['tracingContext'] = self.tracing_context.to_json()
28+
if self.trace_parent is not None:
29+
json['traceparent'] = self.trace_parent
30+
if self.trace_state is not None:
31+
json['tracestate'] = self.trace_state
3032

3133
return json

eventsourcingdb_client_python/event/event_context.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from datetime import datetime
33
from typing import TypeVar
44

5-
from .tracing import TracingContext
65
from ..errors.internal_error import InternalError
76
from ..errors.validation_error import ValidationError
87
from .validate_subject import validate_subject
@@ -21,7 +20,8 @@ class EventContext:
2120
time: datetime
2221
data_content_type: str
2322
predecessor_hash: str
24-
tracing_context: TracingContext | None = None
23+
trace_parent: str = None
24+
trace_state: str = None
2525

2626
@staticmethod
2727
def parse(unknown_object: dict) -> Self:
@@ -77,10 +77,15 @@ def parse(unknown_object: dict) -> Self:
7777
raise ValidationError(
7878
f'Failed to parse predecessor_hash \'{predecessor_hash}\' to string.')
7979

80-
raw_tracing_context = unknown_object.get("tracingContext")
81-
tracing_context = TracingContext.parse(raw_tracing_context) \
82-
if raw_tracing_context is not None \
83-
else None
80+
trace_parent = unknown_object.get('traceparent')
81+
if trace_parent is not None and not isinstance(trace_parent, str):
82+
raise ValidationError(
83+
f'Failed to parse trace_parent \'{trace_parent}\' to string.')
84+
85+
trace_state = unknown_object.get('tracestate')
86+
if trace_state is not None and not isinstance(trace_state, str):
87+
raise ValidationError(
88+
f'Failed to parse trace_state \'{trace_state}\' to string.')
8489

8590
return EventContext(
8691
source=source,
@@ -91,11 +96,12 @@ def parse(unknown_object: dict) -> Self:
9196
time=time,
9297
data_content_type=data_content_type,
9398
predecessor_hash=predecessor_hash,
94-
tracing_context=tracing_context
99+
trace_parent=trace_parent,
100+
trace_state=trace_state,
95101
)
96102

97103
def to_json(self):
98-
return {
104+
json = {
99105
'specversion': self.spec_version,
100106
'id': self.event_id,
101107
'time': self.time.isoformat(sep='T'),
@@ -105,3 +111,10 @@ def to_json(self):
105111
'datacontenttype': self.data_content_type,
106112
'predecessorhash': self.predecessor_hash
107113
}
114+
115+
if self.trace_parent is not None:
116+
json['traceparent'] = self.trace_parent
117+
if self.trace_state is not None:
118+
json['tracestate'] = self.trace_state
119+
120+
return json

eventsourcingdb_client_python/event/source.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from dataclasses import dataclass
22

3-
from ..event.tracing import TracingContext
43
from ..event.event_candidate import EventCandidate
54

65

@@ -13,6 +12,14 @@ def new_event(
1312
subject: str,
1413
event_type: str,
1514
data: dict,
16-
tracing_context: TracingContext = None
15+
trace_parent: str = None,
16+
trace_state: str = None
1717
) -> EventCandidate:
18-
return EventCandidate(self.source, subject, event_type, data, tracing_context)
18+
return EventCandidate(
19+
self.source,
20+
subject,
21+
event_type,
22+
data,
23+
trace_parent,
24+
trace_state
25+
)

eventsourcingdb_client_python/event/tracing.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)