Skip to main content

Observability

ib-interface uses OpenTelemetry to export structured log data to SigNoz or any OTLP-compatible backend.

Architecture

Python's standard logging module is bridged to OpenTelemetry via LoggingHandler. All log records emitted under the ib_insync logger are captured, enriched with OTel resource attributes, and exported.

Setup

1. Start SigNoz

Run SigNoz locally with Docker Compose:

git clone -b main https://github.com/SigNoz/signoz.git && cd signoz/deploy
docker compose -f docker/clickhouse-setup/docker-compose.yaml up -d

SigNoz UI will be available at http://localhost:3301.

2. Enable telemetry in ib-interface

from ib_interface.telemetry import setup_telemetry, shutdown_telemetry

setup_telemetry(
service_name="ib-interface",
otlp_endpoint="localhost:4317",
)

Call shutdown_telemetry() on exit to flush pending log records.

If otlp_endpoint is omitted, logs are written to the console in OTel format (useful for local development).

3. Verify

After connecting to TWS/Gateway, check the SigNoz Logs tab. Filter by service.name = ib-interface to see log entries.

Instrumented Events

EventLoggerLevelKey Attributes
Connection establishedib_insync.ibINFOhost, port, clientId
Connection lostib_insync.ibWARNINGreason
Error callbackib_insync.wrapperERROR/WARNINGreqId, errorCode, errorString, errorTime
Order placedib_insync.ibINFOorderId, action, orderType, totalQuantity
Order status changeib_insync.wrapperINFOorderId, status, filled, remaining
Order fillib_insync.wrapperINFOorderId, avgFillPrice
Protocol type detectedib_insync.decoderDEBUGprotocol (text/protobuf), msgId

Dashboard Configuration

Create a SigNoz dashboard with the following panels:

Connection Health

  • Query: Filter logs where body contains "connect" or "disconnect"
  • Visualization: Timeline showing connection/disconnection events
  • Purpose: Monitor connection stability over time

Error Rate

  • Query: Filter logs with severity_number >= 13 (WARNING and above)
  • Visualization: Bar chart grouped by 5-minute intervals
  • Aggregation: Count of error/warning log records
  • Purpose: Detect error spikes that may indicate TWS/Gateway issues

Order Latency

  • Query: Filter logs where body contains "order" or "fill"
  • Visualization: Timeline of order placement to fill events
  • Group by: orderId attribute
  • Purpose: Track order execution performance

Protocol Distribution

  • Query: Filter logs from ib_insync.decoder where body contains "protocol"
  • Visualization: Pie chart of text vs protobuf message counts
  • Purpose: Verify protobuf adoption and monitor protocol mix

Alerting Rules

Configure alerts in SigNoz under Settings > Alert Rules.

Connection Drop Alert

FieldValue
NameIB Connection Lost
Queryservice.name = ib-interface AND severity_text = WARNING AND body CONTAINS "disconnect"
ConditionCount > 0 in 1-minute window
SeverityCritical
ChannelSlack / PagerDuty / Email
DescriptionTWS/Gateway connection was lost. Check network and TWS status.

Error Spike Alert

FieldValue
NameIB Error Spike
Queryservice.name = ib-interface AND severity_number >= 17 (ERROR+)
ConditionCount > 10 in 5-minute window
SeverityWarning
ChannelSlack / Email
DescriptionElevated error rate detected. Review error codes in SigNoz logs.

No Activity Alert

FieldValue
NameIB No Log Activity
Queryservice.name = ib-interface
ConditionCount = 0 in 15-minute window
SeverityWarning
ChannelSlack / Email
DescriptionNo log records received. Service may be down or telemetry misconfigured.

Environment Variables

SigNoz and OTel can also be configured via environment variables:

VariableDefaultDescription
OTEL_SERVICE_NAMEib-interfaceService name in SigNoz
OTEL_EXPORTER_OTLP_ENDPOINT(none)OTLP collector endpoint
OTEL_EXPORTER_OTLP_INSECUREtrueUse insecure gRPC for local dev

These are standard OTel environment variables and are recognized by the OpenTelemetry SDK. The setup_telemetry() function parameters take precedence when provided.