You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67-27Lines changed: 67 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -273,7 +273,7 @@ The purpose of synapseutils is to create a space filled with convenience functio
273
273
274
274
OpenTelemetry (OTEL)
275
275
--------------------------------
276
-
[OpenTelemetry](https://opentelemetry.io/) helps support the analysis of traces and spans which can provide insights into latency, errors, and other performance metrics. The synapseclient is ready to provide traces should you want them. The Synapse Python client supports OTLP Exports and can be configured via environment variables as defined [here](https://opentelemetry-python.readthedocs.io/en/stable/exporter/otlp/otlp.html).
276
+
[OpenTelemetry](https://opentelemetry.io/) helps support the analysis of traces and spans which can provide insights into latency, errors, and other performance metrics. The synapseclient is ready to provide traces should you want them. The Synapse Python client supports OTLP Exports and can be configured via environment variables as defined [here](https://opentelemetry.io/docs/specs/otel/protocol/exporter/).
277
277
278
278
Read more about OpenTelemetry in Python [here](https://opentelemetry.io/docs/instrumentation/python/)
279
279
@@ -290,47 +290,87 @@ docker run --name jaeger \
290
290
jaegertracing/all-in-one:latest
291
291
```
292
292
Explanation of ports:
293
-
*`4318` HTTP
294
-
*`16686` Jaeger UI
293
+
*`4318` HTTP port for OTLP data collection
294
+
*`16686` Jaeger UI for visualizing traces
295
295
296
296
Once the docker container is running you can access the Jaeger UI via: `http://localhost:16686`
297
297
298
-
#### Example
299
-
By default the OTEL exporter sends trace data to `http://localhost:4318/v1/traces`, however you may override this by setting the `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` environment variable.
298
+
#### Environment Variable Configuration
300
299
301
-
```
300
+
By default, the OTEL exporter sends trace data to `http://localhost:4318/v1/traces`. You can customize the behavior through environment variables:
301
+
302
+
*`OTEL_SERVICE_NAME`: Defines a unique identifier for your application or service in telemetry data (defaults to 'synapseclient'). Set this to a descriptive name that represents your specific implementation, making it easier to filter and analyze traces in your monitoring system.
303
+
*`OTEL_EXPORTER_OTLP_ENDPOINT`: Specifies the destination URL for sending telemetry data (defaults to 'http://localhost:4318'). Configure this to direct data to your preferred OpenTelemetry collector or monitoring service.
304
+
*`OTEL_DEBUG_CONSOLE`: Controls local visibility of telemetry data. Set to 'true' to output trace information to the console, which is useful for development and troubleshooting without an external collector.
305
+
*`OTEL_SERVICE_INSTANCE_ID`: Distinguishes between multiple instances of the same service (e.g., 'prod', 'development', 'local'). This helps identify which specific deployment or environment generated particular traces.
306
+
*`OTEL_EXPORTER_OTLP_HEADERS`: Configures authentication and metadata for telemetry exports. Use this to add API keys, authentication tokens, or custom metadata when sending traces to secured collectors or third-party monitoring services.
307
+
308
+
309
+
#### Enabling OpenTelemetry in your code
310
+
To enable OpenTelemetry with the Synapse Python client, simply call the
311
+
`enable_open_telemetry()` method on the Synapse class. Additionally you can access an
312
+
instance of the OpenTelemetry tracer via the `get_tracer()` call. This will allow you
313
+
to create new spans for your code.
314
+
315
+
```python
302
316
import synapseclient
303
-
from opentelemetry import trace
304
-
from opentelemetry.sdk.trace import TracerProvider
305
-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
306
-
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
307
-
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
with tracer.start_as_current_span("my_function_span"):
320
324
syn = synapseclient.Synapse()
321
-
syn.login()
322
-
my_entity = syn.get("syn52569429")
323
-
print(my_entity)
325
+
syn.login(authToken='auth_token')
326
+
```
324
327
325
-
main()
328
+
#### Advanced Configuration
329
+
330
+
You can pass additional resource attributes to `enable_open_telemetry()`:
331
+
332
+
```python
333
+
import synapseclient
334
+
335
+
# Enable with custom resource attributes
336
+
synapseclient.Synapse.enable_open_telemetry(
337
+
resource_attributes={
338
+
"deployment.environment": "development",
339
+
"service.version": "1.2.3", # Overrides the `OTEL_SERVICE_NAME` environment variable
340
+
"service.instance.id": "4.5.6", # Overrides the `OTEL_SERVICE_INSTANCE_ID` environment variable
341
+
"custom.attribute": "value"
342
+
}
343
+
)
326
344
```
327
345
346
+
When OpenTelemetry is enabled in the Synapse client, the following happens automatically:
347
+
348
+
1. Instrumentation is set up for:
349
+
-**Threading** (via `ThreadingInstrumentor`): Ensures proper context propagation across threads, which is essential for maintaining trace continuity in multi-threaded applications
350
+
-**HTTP libraries**:
351
+
-`requests` (via `RequestsInstrumentor`): Captures all HTTP requests made using the requests library, including methods, URLs, status codes, and timing information
352
+
-`httpx` (via `HTTPXClientInstrumentor`): Tracks both synchronous and asynchronous HTTP requests made with the httpx library
353
+
-`urllib` (via `URLLibInstrumentor`): Monitors lower-level HTTP operations made directly with Python's standard library
354
+
- Each instrumented HTTP library includes custom hooks that extract Synapse entity IDs from URLs when possible and add them as span attributes
355
+
356
+
2. Traces are configured to collect spans across your application:
357
+
- Spans automatically capture operation duration, status, and errors.
358
+
- An attribute propagation mechanism ensures that certain attributes (like `synapse.transfer.direction` and `synapse.operation.category`) are properly passed to child spans for uploads/downloads.
359
+
- Trace data is exported via OTLP (OpenTelemetry Protocol).
360
+
361
+
3. Resource information is automatically added to your traces, including:
362
+
- Python version
363
+
- OS type
364
+
- Synapse client version
365
+
- Service name (defaults to "synapseclient" but can be customized via environment variables)
366
+
- Service instance ID
328
367
368
+
Note that once enabled, OpenTelemetry cannot be disabled in the same process - you would need to restart your Python interpreter to disable it.
0 commit comments