Skip to content

Commit 776ab9e

Browse files
committed
feat: Be explicit about span vs resource attributes
1 parent 1b8d688 commit 776ab9e

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ Troncos is designed to take advantage of `ddtrace` made by DataDog.
4545

4646
The ddtrace docs can be found [here](https://ddtrace.readthedocs.io/en/stable/).
4747

48+
[Best practices for traces](https://grafana.com/docs/tempo/latest/operations/best-practices/#naming-conventions-for-span-and-resource-attributes) is a good guide to get started.
49+
50+
#### Span vs resource attributes
51+
52+
- A `span attribute` is a key/value pair that provides context for its span.
53+
- A `resource attribute` is a key/value pair that describes the context of how the span was collected.
54+
55+
For more information, read the [Attribute and Resource](https://opentelemetry.io/docs/specs/otel/overview/) sections in the OpenTelemetry specification.
56+
4857
### Enabling the tracer
4958

5059
Configure ddtrace as usual and run `configure_tracer` to send spans to Tempo.
@@ -62,9 +71,10 @@ from troncos.tracing import configure_tracer
6271

6372
# Configure tracer as described in the ddtrace docs.
6473
ddtrace.config.django["service_name"] = 'SERVICE_NAME'
74+
# These are added as span attributes
6575
ddtrace.tracer.set_tags(
6676
tags={
67-
"fulfillment_center": 'osl2',
77+
"key": "value",
6878
}
6979
)
7080

@@ -78,6 +88,14 @@ configure_tracer(
7888
enabled=False, # Set to True when TRACE_HOST is configured.
7989
service_name='SERVICE_NAME',
8090
endpoint=f"http://{TRACE_HOST}:{TRACE_PORT}/v1/traces",
91+
resource_attributes={
92+
"app": "app",
93+
"component": "component",
94+
"role": "role",
95+
"tenant": "tenant",
96+
"owner": "owner",
97+
"version": "version",
98+
}
8199
)
82100
```
83101

tests/tracing/test_writer.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
def tracer_test(
1313
httpserver: HTTPServer,
1414
service_name: str,
15-
service_attributes: dict[str, Any] | None = None,
15+
resource_attributes: dict[str, Any] | None = None,
1616
) -> Generator[Tracer, Any, Any]:
1717
httpserver.expect_oneshot_request("/v1/trace").respond_with_data("OK")
1818

1919
tracer = Tracer()
2020
tracer.configure(
2121
writer=OTELWriter(
2222
service_name=service_name,
23-
service_attributes=service_attributes,
23+
resource_attributes=resource_attributes,
2424
endpoint=httpserver.url_for("/v1/trace"),
2525
exporter=Exporter.HTTP,
2626
)
@@ -51,7 +51,7 @@ def test_attributes(httpserver: HTTPServer) -> None:
5151
with tracer_test(
5252
httpserver,
5353
"test_attributes",
54-
service_attributes={"service_attribute": "working"},
54+
resource_attributes={"resource_attribute": "working"},
5555
) as tracer:
5656
with tracer.trace("test") as span:
5757
span.set_tag("span_attribute", "also_working")
@@ -82,7 +82,7 @@ def test_headers(httpserver: HTTPServer) -> None:
8282
tracer.configure(
8383
writer=OTELWriter(
8484
service_name="test_headers",
85-
service_attributes={},
85+
resource_attributes={},
8686
endpoint=httpserver.url_for("/v1/trace"),
8787
exporter=Exporter(
8888
exporter_type=ExporterType.HTTP, headers={"test-header": "works"}

troncos/tracing/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ def configure_tracer(
1313
enabled: bool,
1414
endpoint: str,
1515
service_name: str,
16-
service_attributes: dict[str, Any] | None = None,
16+
resource_attributes: dict[str, Any] | None = None,
1717
exporter: Exporter = Exporter.HTTP,
1818
) -> None:
1919
"""Configure ddtrace to write traces to the otel tracing backend."""
2020

2121
# Initialize our custom writer used to handle ddtrace spans.
2222
writer = OTELWriter(
2323
service_name=service_name,
24-
service_attributes=service_attributes,
24+
resource_attributes=resource_attributes,
2525
endpoint=endpoint,
2626
exporter=exporter,
2727
)

troncos/tracing/_writer.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ class OTELWriter(TraceWriter):
1313
def __init__(
1414
self,
1515
service_name: str,
16-
service_attributes: dict[str, Any] | None,
16+
resource_attributes: dict[str, Any] | None,
1717
endpoint: str,
1818
exporter: Exporter,
1919
) -> None:
2020
self.service_name = service_name
21-
self.service_attributes = service_attributes
21+
self.resource_attributes = resource_attributes
2222
self.endpoint = endpoint
2323
self.exporter = exporter
2424

2525
self.otel_span_processors = get_otel_span_processors(
2626
endpoint=endpoint, exporter=exporter
2727
)
2828
self.otel_default_resource = Resource.create(
29-
{"service.name": service_name, **(service_attributes or {})}
29+
{"service.name": service_name, **(resource_attributes or {})}
3030
)
3131
self.otel_ignore_attrs = (
3232
set(self.otel_default_resource.attributes.keys()) | default_ignore_attrs()
@@ -35,7 +35,7 @@ def __init__(
3535
def recreate(self) -> "OTELWriter":
3636
return self.__class__(
3737
self.service_name,
38-
self.service_attributes,
38+
self.resource_attributes,
3939
self.endpoint,
4040
exporter=self.exporter,
4141
)

0 commit comments

Comments
 (0)