|
| 1 | +""" |
| 2 | +Telemetry Demo - Demonstrates OpenTelemetry integration with Dataverse SDK. |
| 3 | +
|
| 4 | +This script shows telemetry flowing through: |
| 5 | +1. Custom hooks (always -- no extra dependencies) |
| 6 | +2. Console span/metric exporters (requires opentelemetry-sdk) |
| 7 | +3. Jaeger UI via OTLP (optional, if running locally via Docker) |
| 8 | +
|
| 9 | +To run with hooks only (no OTel dependency): |
| 10 | + python examples/telemetry_demo.py |
| 11 | +
|
| 12 | +To run with full OTel: |
| 13 | + pip install "PowerPlatform-Dataverse-Client[telemetry]" |
| 14 | + pip install opentelemetry-sdk |
| 15 | + python examples/telemetry_demo.py |
| 16 | +
|
| 17 | +To run Jaeger locally: |
| 18 | + docker run -d --name jaeger -p 16686:16686 -p 4317:4317 -p 4318:4318 jaegertracing/all-in-one:latest |
| 19 | +
|
| 20 | +Then open http://localhost:16686 to see traces. |
| 21 | +
|
| 22 | +Usage: |
| 23 | + python examples/telemetry_demo.py |
| 24 | +""" |
| 25 | + |
| 26 | +import sys |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +# Add src to path for development |
| 30 | +sys.path.insert(0, str(Path(__file__).parent.parent / "src")) |
| 31 | + |
| 32 | +# ============================================================================= |
| 33 | +# OpenTelemetry Setup (optional -- demo works with hooks alone) |
| 34 | +# ============================================================================= |
| 35 | + |
| 36 | +OTEL_CONFIGURED = False |
| 37 | + |
| 38 | +try: |
| 39 | + from opentelemetry import trace, metrics |
| 40 | + from opentelemetry.sdk.trace import TracerProvider |
| 41 | + from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter |
| 42 | + from opentelemetry.sdk.metrics import MeterProvider |
| 43 | + from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader |
| 44 | + from opentelemetry.sdk.resources import Resource |
| 45 | + |
| 46 | + resource = Resource.create({"service.name": "dataverse-telemetry-demo"}) |
| 47 | + |
| 48 | + tracer_provider = TracerProvider(resource=resource) |
| 49 | + tracer_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter())) |
| 50 | + |
| 51 | + # Try OTLP exporter for Jaeger |
| 52 | + try: |
| 53 | + from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter |
| 54 | + |
| 55 | + tracer_provider.add_span_processor( |
| 56 | + BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)) |
| 57 | + ) |
| 58 | + print("OTLP exporter configured - view traces at http://localhost:16686") |
| 59 | + except ImportError: |
| 60 | + pass |
| 61 | + |
| 62 | + trace.set_tracer_provider(tracer_provider) |
| 63 | + |
| 64 | + metric_reader = PeriodicExportingMetricReader(ConsoleMetricExporter(), export_interval_millis=5000) |
| 65 | + metrics.set_meter_provider(MeterProvider(resource=resource, metric_readers=[metric_reader])) |
| 66 | + |
| 67 | + OTEL_CONFIGURED = True |
| 68 | + print("OpenTelemetry configured with Console exporter") |
| 69 | + |
| 70 | +except ImportError: |
| 71 | + print("OpenTelemetry not installed -- running with hooks only") |
| 72 | + print(" Install with: pip install opentelemetry-sdk opentelemetry-api") |
| 73 | + |
| 74 | +print("-" * 60) |
| 75 | + |
| 76 | +# ============================================================================= |
| 77 | +# Dataverse SDK Setup |
| 78 | +# ============================================================================= |
| 79 | + |
| 80 | +from PowerPlatform.Dataverse.client import DataverseClient |
| 81 | +from PowerPlatform.Dataverse.core.config import DataverseConfig |
| 82 | +from PowerPlatform.Dataverse.core.telemetry import TelemetryConfig, TelemetryHook |
| 83 | +from azure.identity import InteractiveBrowserCredential |
| 84 | + |
| 85 | +# Org details |
| 86 | +ORG_URL = "https://aurorabapenvcc726.crmtest.dynamics.com" |
| 87 | +TENANT_ID = "91bee3d9-0c15-4f17-8624-c92bb8b36ead" |
| 88 | + |
| 89 | + |
| 90 | +class DemoTelemetryHook(TelemetryHook): |
| 91 | + """Custom hook that prints request/response info to the console.""" |
| 92 | + |
| 93 | + def on_request_start(self, ctx): |
| 94 | + print(f"\n>>> Starting: {ctx.operation} [{ctx.method}]") |
| 95 | + if ctx.table_name: |
| 96 | + print(f" Table: {ctx.table_name}") |
| 97 | + |
| 98 | + def on_request_end(self, request, response): |
| 99 | + status = "[OK]" if response.status_code < 400 else "[ERR]" |
| 100 | + print(f"<<< {status} {request.operation} - {response.status_code} in {response.duration_ms:.1f}ms") |
| 101 | + if response.service_request_id: |
| 102 | + print(f" Service Request ID: {response.service_request_id}") |
| 103 | + |
| 104 | + def on_request_error(self, request, error): |
| 105 | + print(f"!!! Error in {request.operation}: {error}") |
| 106 | + |
| 107 | + |
| 108 | +def main(): |
| 109 | + print("\n" + "=" * 60) |
| 110 | + print("DATAVERSE TELEMETRY DEMO") |
| 111 | + print("=" * 60) |
| 112 | + |
| 113 | + config = DataverseConfig( |
| 114 | + telemetry=TelemetryConfig( |
| 115 | + enable_tracing=OTEL_CONFIGURED, |
| 116 | + enable_metrics=OTEL_CONFIGURED, |
| 117 | + enable_logging=True, |
| 118 | + log_level="DEBUG", |
| 119 | + hooks=[DemoTelemetryHook()], |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + print(f"\nConnecting to: {ORG_URL}") |
| 124 | + print("(Browser will open for authentication)\n") |
| 125 | + |
| 126 | + credential = InteractiveBrowserCredential(tenant_id=TENANT_ID) |
| 127 | + client = DataverseClient(ORG_URL, credential, config=config) |
| 128 | + |
| 129 | + # ---- Operation 1: Query accounts ---- |
| 130 | + print("\n" + "-" * 60) |
| 131 | + print("OPERATION 1: Query accounts (top 3)") |
| 132 | + print("-" * 60) |
| 133 | + |
| 134 | + for page in client.records.get("account", select=["name", "accountid"], top=3): |
| 135 | + print(f"\nFound {len(page)} accounts:") |
| 136 | + for record in page: |
| 137 | + print(f" - {record.get('name', 'N/A')} ({record.get('accountid', 'N/A')[:8]}...)") |
| 138 | + |
| 139 | + # ---- Operation 2: SQL query ---- |
| 140 | + print("\n" + "-" * 60) |
| 141 | + print("OPERATION 2: SQL query for contacts") |
| 142 | + print("-" * 60) |
| 143 | + |
| 144 | + rows = client.query.sql("SELECT TOP 3 fullname, emailaddress1 FROM contact ORDER BY fullname") |
| 145 | + print(f"\nFound {len(rows)} contacts:") |
| 146 | + for row in rows: |
| 147 | + print(f" - {row.get('fullname', 'N/A')} <{row.get('emailaddress1', 'N/A')}>") |
| 148 | + |
| 149 | + # ---- Operation 3: Table metadata ---- |
| 150 | + print("\n" + "-" * 60) |
| 151 | + print("OPERATION 3: Get table metadata") |
| 152 | + print("-" * 60) |
| 153 | + |
| 154 | + info = client.tables.get("account") |
| 155 | + if info: |
| 156 | + print(f"\nTable: {info.get('table_schema_name')}") |
| 157 | + print(f" Logical: {info.get('table_logical_name')}") |
| 158 | + print(f" Entity Set: {info.get('entity_set_name')}") |
| 159 | + |
| 160 | + print("\n" + "=" * 60) |
| 161 | + print("DEMO COMPLETE") |
| 162 | + print("=" * 60) |
| 163 | + print("\nCheck the console output above for:") |
| 164 | + print(" - Hook output (>>> / <<< lines)") |
| 165 | + if OTEL_CONFIGURED: |
| 166 | + print(" - Span traces (name, attributes, duration)") |
| 167 | + print(" - Metrics (request counts, durations)") |
| 168 | + print("\nIf Jaeger is running, view traces at: http://localhost:16686") |
| 169 | + print() |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main__": |
| 173 | + main() |
0 commit comments