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
+234Lines changed: 234 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,9 @@ pipelines.
10
10
More information about Diode can be found
11
11
at [https://netboxlabs.com/blog/introducing-diode-streamlining-data-ingestion-in-netbox/](https://netboxlabs.com/blog/introducing-diode-streamlining-data-ingestion-in-netbox/).
12
12
13
+
## Prerequisites
14
+
- Python 3.10 or later installed
15
+
13
16
## Installation
14
17
15
18
```bash
@@ -79,6 +82,123 @@ if __name__ == "__main__":
79
82
80
83
```
81
84
85
+
See all [examples](./examples) for reference.
86
+
87
+
### Using Metadata
88
+
89
+
Entities support attaching custom metadata as key-value pairs. Metadata can be used to store additional context, tracking information, or custom attributes that don't fit into the standard NetBox fields.
90
+
91
+
```python
92
+
from netboxlabs.diode.sdk import DiodeClient, Entity
93
+
from netboxlabs.diode.sdk.ingester import Device, Site, IPAddress
94
+
95
+
with DiodeClient(
96
+
target="grpc://localhost:8080/diode",
97
+
app_name="my-app",
98
+
app_version="1.0.0",
99
+
) as client:
100
+
# Create a device with metadata
101
+
# Note: Both the device and its nested site can have its own metadata
In addition to entity-level metadata, you can attach metadata to the entire ingestion request using the `metadata` keyword argument. This is useful for tracking information about the ingestion batch itself, such as the data source, batch ID, or processing context.
157
+
158
+
```python
159
+
from netboxlabs.diode.sdk import DiodeClient, Entity
160
+
from netboxlabs.diode.sdk.ingester import Device, Site
`DiodeDryRunClient` generates ingestion requests without contacting a Diode server. Requests are printed to stdout by default, or written to JSON files when `output_dir` (or the `DIODE_DRY_RUN_OUTPUT_DIR` environment variable) is specified. The `app_name` parameter serves as the filename prefix; if not provided, `dryrun` is used as the default prefix. The file name is suffixed with a nanosecond-precision timestamp, resulting in the format `<app_name>_<timestamp_ns>.json`.
@@ -149,6 +281,108 @@ diode-replay-dryrun \
149
281
my_app_92722156890707.json
150
282
```
151
283
284
+
#### Adding request-level metadata to dry run output
285
+
286
+
You can include request-level metadata in the dry run output using the `metadata` keyword argument. This metadata will be included in the JSON output file as part of the `IngestRequest`:
287
+
288
+
```python
289
+
from netboxlabs.diode.sdk import DiodeDryRunClient, Entity
290
+
from netboxlabs.diode.sdk.ingester import Device
291
+
292
+
with DiodeDryRunClient(app_name="my_app", output_dir="/tmp") as client:
293
+
# Add request-level metadata
294
+
client.ingest(
295
+
[Entity(device=Device(name="Device A"))],
296
+
metadata={
297
+
"batch_id": "import-2024-01",
298
+
"source": "csv_import",
299
+
"validated": True,
300
+
"record_count": 150,
301
+
}
302
+
)
303
+
```
304
+
305
+
The resulting JSON file will include the metadata in the `IngestRequest`, making it visible when reviewing the dry run output.
306
+
307
+
### CLI to replay dry-run files
308
+
309
+
A small helper command is included to ingest JSON files created by the
310
+
`DiodeDryRunClient` and send them to a running Diode service.
311
+
312
+
Install the helper using `pip`:
313
+
314
+
```bash
315
+
pip install netboxlabs-diode-sdk
316
+
```
317
+
318
+
Run it by providing one or more JSON files and connection details. The command supports replaying multiple dry-run files in a single request:
319
+
320
+
```bash
321
+
diode-replay-dryrun \
322
+
--file /tmp/my_app_92722156890707.json \
323
+
--file /tmp/other.json \
324
+
--target grpc://localhost:8080/diode \
325
+
--app-name my-test-app \
326
+
--app-version 0.0.1 \
327
+
--client-id YOUR_CLIENT_ID \
328
+
--client-secret YOUR_CLIENT_SECRET
329
+
```
330
+
331
+
The `--file`, `--target`, `--app-name`, and `--app-version` arguments are required. You may
332
+
repeat `--file` to specify multiple files. OAuth2
333
+
credentials can be supplied using `--client-id` and `--client-secret` or the
334
+
`DIODE_CLIENT_ID` and `DIODE_CLIENT_SECRET` environment variables.
335
+
336
+
### OTLP client
337
+
338
+
`DiodeOTLPClient` converts ingestion entities into OpenTelemetry log records and exports them to an OTLP endpoint (gRPC). This is useful when a collector ingests log data and forwards it to Diode.
339
+
340
+
```python
341
+
from netboxlabs.diode.sdk import Entity, DiodeOTLPClient
342
+
343
+
with DiodeOTLPClient(
344
+
target="grpc://localhost:4317",
345
+
app_name="my-producer",
346
+
app_version="0.0.1",
347
+
) as client:
348
+
client.ingest([Entity(site="Site1")])
349
+
```
350
+
351
+
Each entity is serialised to JSON and sent as a log record with producer metadata so downstream collectors can enrich and forward the payload. The client raises `OTLPClientError` when the export fails. TLS behaviour honours the existing `DIODE_SKIP_TLS_VERIFY` and `DIODE_CERT_FILE` environment variables.
352
+
353
+
#### Adding request-level metadata as OTLP resource attributes
354
+
355
+
You can add request-level metadata to OTLP exports using the `metadata` keyword argument. This metadata is automatically mapped to OTLP resource attributes with a `diode.metadata.` prefix:
356
+
357
+
```python
358
+
from netboxlabs.diode.sdk import DiodeOTLPClient, Entity
359
+
from netboxlabs.diode.sdk.ingester import Site
360
+
361
+
with DiodeOTLPClient(
362
+
target="grpc://localhost:4317",
363
+
app_name="otlp-producer",
364
+
app_version="1.0.0",
365
+
) as client:
366
+
# Add request-level metadata
367
+
client.ingest(
368
+
[Entity(site=Site(name="Site 1"))],
369
+
metadata={
370
+
"environment": "production",
371
+
"deployment": "us-west-2",
372
+
"version": "1.2.3",
373
+
"priority": 5,
374
+
},
375
+
)
376
+
```
377
+
378
+
The resulting OTLP log records will include resource attributes like:
379
+
-`diode.metadata.environment="production"`
380
+
-`diode.metadata.deployment="us-west-2"`
381
+
-`diode.metadata.version="1.2.3"`
382
+
-`diode.metadata.priority=5` (as integer)
383
+
384
+
These attributes are added alongside standard OTLP resource attributes (`service.name`, `service.version`, `diode.stream`, etc.), allowing downstream collectors and observability platforms to filter, route, and enrich the data based on this metadata.
0 commit comments