observability.py¶
OpenTelemetry observability utilities for Splunk add-ons.
This module provides two public components:
-
:class:
LoggerMetricExporter— an OpenTelemetryMetricExporterthat writes every exported data point to a standard Python logger. It is useful for local development, debugging, and as a fallback when the Spotlight collector is not available. -
:class:
ObservabilityService— a high-level wrapper that wires up aMeterProviderand creates the two mandatory event counters required by every Splunk add-on modular input. It automatically tries to connect to the Splunk Spotlight OTLP collector and falls back silently when it is not reachable, so callers never have to handle observability failures themselves.
Typical usage::
import logging
from solnlib.observability import LoggerMetricExporter, ObservabilityService, ATTR_MODINPUT_NAME
logger = logging.getLogger(__name__)
obs = ObservabilityService(
modinput_type="my-input",
logger=logger,
ta_name="my_ta",
ta_version="1.0.0",
extra_exporters=[LoggerMetricExporter(logger)],
)
# In your event collection loop:
if obs.event_count_counter:
obs.event_count_counter.add(
len(events), {ATTR_MODINPUT_NAME: stanza_name}
)
if obs.event_bytes_counter:
obs.event_bytes_counter.add(
total_bytes, {ATTR_MODINPUT_NAME: stanza_name}
)
ATTR_MODINPUT_NAME = 'splunk.modinput.name'
module-attribute
¶
LoggerMetricExporter
¶
Bases: MetricExporter
An OpenTelemetry MetricExporter that logs every data point.
Each exported data point is written to a standard Python logger at INFO
level. Counters are logged as value, histograms as count,
sum, min, max, bucket_counts, and explicit_bounds.
This exporter is always available without any external infrastructure, so it is suitable for local development, CI environments, and as a fallback alongside the OTLP exporter.
Both Counter and Histogram instruments use delta temporality,
meaning each export interval reports only the change since the previous
interval, not a cumulative total.
Example::
import logging
from solnlib.observability import LoggerMetricExporter
logger = logging.getLogger(__name__)
exporter = LoggerMetricExporter(logger)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger |
_Logger
|
The Python logger (or |
required |
Source code in solnlib/observability.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
__init__(logger)
¶
Source code in solnlib/observability.py
115 116 117 118 119 120 121 122 | |
export(metrics_data, timeout_millis=10000, **kwargs)
¶
Export metrics by writing each data point to the logger.
Called automatically by the PeriodicExportingMetricReader on each
export interval. You do not need to call this method directly.
Returns:
| Type | Description |
|---|---|
MetricExportResult
|
|
MetricExportResult
|
|
Source code in solnlib/observability.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
force_flush(timeout_millis=10000)
¶
Flush is a no-op for a synchronous logger; always returns True.
Source code in solnlib/observability.py
188 189 190 | |
shutdown(timeout_millis=30000, **kwargs)
¶
No-op shutdown — the underlying logger needs no teardown.
Source code in solnlib/observability.py
185 186 | |
ObservabilityService
¶
OpenTelemetry observability service for a Splunk modular input.
Sets up a MeterProvider with two built-in event counters and,
when the Spotlight collector is reachable, an OTLP gRPC exporter.
Initialisation failures are caught and logged as warnings so that a
missing or misconfigured observability stack never breaks the add-on.
Resource attributes (fixed for the lifetime of the process):
| Attribute | Value |
|---|---|
splunk.addon.name |
ta_name |
service.namespace |
"splunk.addon" |
splunk.addon.version |
ta_version |
splunk.modinput.type |
modinput_type |
Built-in counters (None if initialisation failed):
| Attribute | Metric name | Unit |
|---|---|---|
event_count_counter |
splunk.addon.events |
1 |
event_bytes_counter |
splunk.addon.events.bytes |
By |
Both counters accept ATTR_MODINPUT_NAME ("splunk.modinput.name")
as the only recommended data-point attribute. Avoid adding other
high-cardinality labels to these metrics.
Additional instruments can be created with :meth:register_instrument.
Example::
import logging
from solnlib.observability import (
LoggerMetricExporter,
ObservabilityService,
ATTR_MODINPUT_NAME,
)
logger = logging.getLogger(__name__)
obs = ObservabilityService(
modinput_type="my-input",
logger=logger,
ta_name="my_ta",
ta_version="1.0.0",
extra_exporters=[LoggerMetricExporter(logger)],
)
# Record ingested events in your collection loop:
attrs = {ATTR_MODINPUT_NAME: stanza_name}
if obs.event_count_counter:
obs.event_count_counter.add(len(events), attrs)
if obs.event_bytes_counter:
obs.event_bytes_counter.add(total_bytes, attrs)
Source code in solnlib/observability.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |
event_bytes_counter: Optional[Counter] = self._meter.create_counter(name='splunk.addon.events.bytes', description='Volume of data ingested by the add-on modular input', unit='By')
instance-attribute
¶
event_count_counter: Optional[Counter] = self._meter.create_counter(name='splunk.addon.events', description='Number of events ingested by the add-on modular input', unit='1')
instance-attribute
¶
__init__(modinput_type, logger, ta_name=None, ta_version=None, extra_exporters=None)
¶
Initialise the observability service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modinput_type |
str
|
Low-cardinality string identifying the modular input
type, e.g. |
required |
logger |
_Logger
|
Python logger (or |
required |
ta_name |
Optional[str]
|
Add-on identifier, e.g. |
None
|
ta_version |
Optional[str]
|
Add-on version string, e.g. |
None
|
extra_exporters |
Optional[List[MetricExporter]]
|
Optional list of additional
|
None
|
Source code in solnlib/observability.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
register_instrument(callback)
¶
Create a custom instrument using the service’s meter.
Passes the internal Meter to callback and returns whatever the
callback creates. If the service failed to initialise (e.g. because
ta_name could not be determined), the meter is None and this
method returns None without invoking the callback.
Always guard the returned value against None before calling it, for
the same reason you guard event_count_counter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback |
Callable[[Meter], Instrument]
|
A callable that receives the |
required |
Returns:
| Type | Description |
|---|---|
Optional[Instrument]
|
The instrument created by callback, or |
Optional[Instrument]
|
not available. |
Example::
latency = obs.register_instrument(
lambda meter: meter.create_histogram(
name="my_ta.request.latency",
description="Latency of outbound API requests",
unit="s",
)
)
if latency:
latency.record(elapsed, {ATTR_MODINPUT_NAME: stanza_name})
Source code in solnlib/observability.py
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | |