Home › Studies › AI-200 › Secure & Monitor › OpenTelemetry & KQL

OpenTelemetry & KQL

Microsoft OpenTelemetry Distro for Python · connection string · cloud role name · sampling · custom spans, metrics and exceptions · KQL operators

By the tools4free.dev team · Last reviewed November 13, 2026

Practice questions for this topic →

Enable OpenTelemetry in Python

OpenTelemetry (OTel) is a vendor-neutral observability framework. You instrument the application, collect telemetry and analyze it in Application Insights. The Microsoft OpenTelemetry Distro automatically collects traces, metrics and logs with built-in instrumentations and exports them to Azure Monitor, any OTLP endpoint, or Microsoft Agent 365.

LanguagePackageInstall
Pythonmicrosoft-opentelemetrypip install microsoft-opentelemetry
Node.js@microsoft/opentelemetrynpm install @microsoft/opentelemetry
.NETMicrosoft.OpenTelemetrydotnet add package Microsoft.OpenTelemetry
JavaAgent or native-image integrationSee the Java tabs

Connection string, cloud role and sampling

Way to set the connection stringLanguagesUse for
Environment variable APPLICATIONINSIGHTS_CONNECTION_STRINGAllProduction
Configuration file applicationinsights.jsonJava onlyProduction (Java)
In code.NET, Node.js and PythonLocal development and test only
Sampling setting (Python)Value
DefaultRate-limited sampling at five traces per second
OTEL_TRACES_SAMPLER=microsoft.fixed_percentageOTEL_TRACES_SAMPLER_ARG is a value in 0.0 to 1.0 (0.1 = about 10%)
OTEL_TRACES_SAMPLER=microsoft.rate_limitedOTEL_TRACES_SAMPLER_ARG is the maximum traces per second (for example 1.5)

Custom spans, metrics and exceptions

KQL basics for logs and metrics

GoalKQL
Glance at a table (arbitrary rows)SecurityEvent | take 10
Latest 10 rows by timeSecurityEvent | top 10 by TimeGenerated
Filter rowsSecurityEvent | where Level == 8
Recent time rangewhere TimeGenerated > ago(30m)
Pick columns| project TimeGenerated, Computer, Activity
Add a column, keep the rest| extend EventCode=substring(Activity, 0, 4)
Count per groupPerf | where TimeGenerated > ago(1h) | summarize count() by ObjectName
Average per time bucket| summarize avg(CounterValue) by bin(TimeGenerated, 1h)

KQL aggregation, join and charts

Join kindRows returned
innerunique (default)Deduplicated left rows that match the right table
innerOnly matching rows from both tables
leftouterAll left rows and only matching right rows
leftantiLeft rows that don't match any right row

For best performance, if one table is always smaller than the other, use it as the left side of the join.

Application Insights requests are in the AppRequests table. Columns include AppRoleName, DurationMs, Success and ResultCode.

CLI and PowerShell

TaskAzure CLIPowerShell
Install the Python distropip install microsoft-opentelemetry-
Set the connection string for productionexport APPLICATIONINSIGHTS_CONNECTION_STRING="<ConnectionString>"-
Keep about 10% of tracesexport OTEL_TRACES_SAMPLER="microsoft.fixed_percentage" export OTEL_TRACES_SAMPLER_ARG=0.1-
Limit to about 1.5 traces per secondexport OTEL_TRACES_SAMPLER="microsoft.rate_limited" export OTEL_TRACES_SAMPLER_ARG=1.5-
Custom span (Python)-with tracer.start_as_current_span("hello") as span:
Latest 10 rows (KQL)-SecurityEvent | top 10 by TimeGenerated
Count per group (KQL)-Perf | where TimeGenerated > ago(1h) | summarize count() by ObjectName
Hourly average (KQL)-Perf | where TimeGenerated > ago(7d) | where Computer == "DC01.na.contosohotels.com" | where CounterName == "Available MBytes" | summarize avg(CounterValue) by bin(TimeGenerated, 1h)

Exam Tips

Tick each tip once you can answer it without looking.

“Install OpenTelemetry for a Python app that reports to Application Insights” → pip install microsoft-opentelemetry. Trap: Microsoft.OpenTelemetry is .NET; @microsoft/opentelemetry is Node.js.

“Production connection string” → Environment variable APPLICATIONINSIGHTS_CONNECTION_STRING. Trap: In code is for local dev/test; applicationinsights.json is Java only.

“Two services in one Application Insights resource look like one node” → Set Cloud Role Names (OTEL_SERVICE_NAME, service.namespace). Trap: The connection string names the resource, not the role.

“Cloud role name when service.namespace isn't set” → Falls back to service.name. Trap: Cloud Role Instance comes from service.instance.id.

“Python distro default sampling” → Rate-limited, five traces per second. Trap: It doesn't keep all traces by default.

“Keep about 10% of traces” → microsoft.fixed_percentage with OTEL_TRACES_SAMPLER_ARG=0.1. Trap: The argument is 0.0 to 1.0, not 10; sampling_ratio is the older distro's argument.

“Reduce metrics volume with sampling” → Not possible: sampling applies to traces; metrics are never sampled. Trap: Logs can follow trace sampling, but metrics don't.

“Custom span for a background job must show in requests” → kind = SpanKind.SERVER. Trap: By default a custom span lands in dependencies as InProc.

“Add the same attribute to every span” → SpanProcessor in span_processors, set_attribute in on_start. Trap: Resource attributes describe the service, not each span.

“Exceptions thrown inside a span” → Recorded automatically; record_exception=False to do it manually. Trap: Manual record_exception() is needed only after you disable the automatic recording.

“Latest N rows by time” → | top N by TimeGenerated. Trap: take is arbitrary; sort doesn't limit rows.

“Average per hour over 7 days” → summarize avg(...) by bin(TimeGenerated, 1h). Trap: Grouping by TimeGenerated alone makes a group per millisecond.

“Add a column and keep the others” → extend. Trap: project drops columns you don't list.

“Case-insensitive match” → =~. Trap: == is case-sensitive.

“Left rows with no match on the right” → join kind=leftanti. Trap: inner keeps matches; the default is innerunique.

“Chart the result of a query” → | render ... as the last operator. Trap: Nothing can follow render.

Sources

Checked against Microsoft Learn on November 13, 2026.

← Key Vault & App ConfigurationSecure & Monitor overview →