Skip to content

Add --otel distributed tracing support - #126

Merged
Mike Krüger (mkrueger) merged 2 commits into
mainfrom
dev/mkrueger/distributed-tracing
Jun 15, 2026
Merged

Mike Krüger (mkrueger) merged 2 commits into
mainfrom
dev/mkrueger/distributed-tracing

Conversation

@mkrueger

@mkrueger Mike Krüger (mkrueger) commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds distributed tracing to CosmosShell so requests carry a sampled W3C traceparent that external tracing systems (for example, the Cosmos DB emulator) can use to correlate shell activity. Optionally exports spans over OTLP.

This addresses the distributed-tracing request. File-based diagnostic logging (issue #122) is intentionally out of scope and will follow in a separate PR.

How it works

The Cosmos SDK only emits a traceparent with the sampled flag set (-01) when two things are true: the Azure SDK Azure.Experimental.EnableActivitySource switch is on, and a recorded (sampled) Activity is in scope. This PR wires both up:

  • Sets AppContext switch Azure.Experimental.EnableActivitySource before any CosmosClient is created.
  • Registers an OpenTelemetry TracerProvider with AlwaysOnSampler, listening to the Azure.Cosmos.Operation and CosmosDBShell activity sources, so activities are recorded.
  • Wraps each shell command in a CosmosDBShell root activity, so every command is one trace.
  • Sets CosmosClientTelemetryOptions.DisableDistributedTracing = false explicitly.

CLI

New --otel [endpoint] option (optional value, mirroring --mcp):

  • --otel — enable tracing (always emits a sampled traceparent). Spans are exported only if an OTLP endpoint is configured (see below); otherwise tracing is local-only with no export.
  • --otel <endpoint> — also export spans to that explicit OTLP endpoint.
  • When --otel is given without an endpoint, the endpoint falls back to the standard OTEL_EXPORTER_OTLP_ENDPOINT environment variable; if that is also unset, no spans are exported.

A malformed endpoint (from --otel <endpoint> or OTEL_EXPORTER_OTLP_ENDPOINT) now produces a clean error and a non-zero exit code instead of an unhandled exception.

Changes

  • --otel option + plumbing in Program.cs; help-Otel and otel-error-invalid-endpoint in en.ftl.
  • New Core/TracingBootstrap.cs (provider lifecycle + per-command activity source).
  • ShellInterpreter: enable SDK tracing, per-command root activity.
  • OpenTelemetry + OTLP exporter packages (1.15.3, the patched version clearing advisory NU1902).
  • README, docs/navigation.md, and Runtime/TracingBootstrapTests.cs.

Validation

  • Main project build: 0 warnings, 0 errors.
  • Full test suite: 1293 passed, 0 failed, 74 skipped (pre-existing emulator-dependent skips).

Out of scope

File logging / issue #122, OTEL metrics & logs signals, Azure Monitor / App Insights exporters, Direct-mode traceparent specifics.

Emit a sampled W3C traceparent on Cosmos DB requests so external tracing systems (e.g. the emulator) can correlate shell activity.

- New --otel [endpoint] option (optional value, like --mcp): bare --otel enables tracing; an endpoint (or OTEL_EXPORTER_OTLP_ENDPOINT) also exports spans via OTLP
- TracingBootstrap: sets the Azure.Experimental.EnableActivitySource switch and registers an AlwaysOn TracerProvider listening to Azure.Cosmos.Operation and CosmosDBShell sources so activities are recorded (traceparent flag -01)
- Each command runs inside a CosmosDBShell root activity
- CreateClientOptions sets DisableDistributedTracing=false explicitly
- Add OpenTelemetry + OTLP exporter packages (1.15.3)
- Help text, README, docs/navigation.md, and unit tests

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds optional OpenTelemetry-based distributed tracing to CosmosDBShell via a new --otel [endpoint] startup option, enabling sampled W3C traceparent propagation on Cosmos SDK requests and (optionally) OTLP span export.

Changes:

  • Adds --otel [endpoint] CLI option, wiring initialization/disposal of tracing in Program.cs and help text in en.ftl.
  • Introduces TracingBootstrap to configure an OpenTelemetry TracerProvider and create per-command root activities.
  • Updates docs/README and adds unit tests covering tracing bootstrap behavior.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
README.md Documents the new --otel option in the feature list and options table.
docs/navigation.md Adds --otel and OTEL_EXPORTER_OTLP_ENDPOINT to navigation docs + examples.
Directory.Packages.props Pins OpenTelemetry + OTLP exporter package versions.
CosmosDBShell/Program.cs Parses --otel [endpoint], initializes/disposes tracing, and adds the option to help output.
CosmosDBShell/lang/en.ftl Adds localized help text for --otel.
CosmosDBShell/CosmosDBShell.csproj Adds package references for OpenTelemetry and the OTLP exporter.
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/TracingBootstrap.cs New tracing bootstrapper (provider lifecycle + ActivitySource root activities).
CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellInterpreter.cs Wraps each executed command in a tracing activity; enables Cosmos SDK distributed tracing option.
CosmosDBShell.Tests/Runtime/TracingBootstrapTests.cs Adds tests verifying activity creation/recording and the Azure SDK switch.
Comment thread CosmosDBShell/Program.cs Resolved
Comment thread CosmosDBShell/Program.cs Resolved
@mkrueger
Mike Krüger (mkrueger) merged commit bc6586f into main Jun 15, 2026
8 checks passed
@mkrueger
Mike Krüger (mkrueger) deleted the dev/mkrueger/distributed-tracing branch June 15, 2026 06:50
Mike Krüger (mkrueger) added a commit that referenced this pull request Jun 23, 2026
…127)

## Summary

Implements [issue
#122](#122). Adds a
`--diagnostics [path]` CLI option that writes a timestamped diagnostic
log file so users can capture shell activity for troubleshooting,
performance analysis, and auditing.

This is the file-based diagnostic logging that was deliberately left out
of scope of the distributed-tracing PR (#126); the two features are
independent.

## CLI

```
cosmosdbshell --diagnostics              # log to a timestamped file in the config directory
cosmosdbshell --diagnostics mylog.log    # log to a custom file path
```

`--diagnostics` takes an optional value (mirroring `--mcp`). With no
value it writes to `diagnostics-<yyyyMMdd-HHmmss>.log` in the shell
configuration directory; with a value it writes to that path.

## What gets logged

- Session metadata header: start timestamp, machine name, OS, runtime
version
- Connection events: endpoint URL and connection mode
- Command execution: every command with a timestamp
- Command results: `[OK]`/`[FAIL]` with elapsed time in milliseconds
- Errors: exception type and message for failed commands

### Sample output

```
# CosmosDB Shell Diagnostic Log
# Started: 2026-06-11T14:30:00.0000000-07:00
# Machine: DEVBOX
# OS: Microsoft Windows NT 10.0.22631.0
# Runtime: 10.0.0
--------------------------------------------------------------------------------
[14:30:01.234] [CONNECT ] https://myaccount.documents.azure.com:443/ (mode=Direct)
[14:30:02.456] [CMD     ] dir
[14:30:02.789] [RESULT  ] [OK] 333.2ms | dir
[14:30:05.100] [CMD     ] cat nonexistent
[14:30:05.102] [ERROR   ] cat nonexistent -> ShellException: Item not found
[14:30:05.102] [RESULT  ] [FAIL] 2.1ms | cat nonexistent
```

## Changes

- New `Core/DiagnosticLog.cs`: thread-safe file writer; serializes
writes, swallows IO failures so diagnostics never disrupt the session.
- `ShellInterpreter`: `EnableDiagnostics(path)`, per-command timing +
result/error logging in `ExecuteCommandAsync`, connection event in
`Connect`, disposal in `Dispose`.
- `Program.cs`: `--diagnostics [path]` option + plumbing;
`CosmosShellOptions.EnableDiagnostics`/`DiagnosticsPath`.
- `help-Diagnostics`, `diagnostics-enabled`, `diagnostics-error-create`
strings in `en.ftl`.
- `README.md`, `docs/navigation.md`, and
`Runtime/DiagnosticLogTests.cs`.

## Validation

- Build: 0 warnings, 0 errors.
- Full test suite: 1298 passed, 0 failed, 74 skipped (pre-existing
emulator-dependent skips).

Closes #122
Mike Krüger (mkrueger) added a commit that referenced this pull request Jul 1, 2026
….4 (#143)

## Summary

Rewrites the `1.1.114-preview` changelog section so it accurately
documents **all** changes merged since `1.1.4-preview`, not just the
server-side programming commands and two breaking changes.

PR #142 only stamped the release heading. This fills in the body.

## What changed

The `1.1.114-preview` entry now covers every PR merged on top of 1.1.4:

- **Highlights** of the major additions.
- **New features**: `theme` (#83), `filter` (#67), `watch`/`tail`
(#115), `edit` (#117), `index` (#116), `throughput` (#130),
`import`/`export` (#95), `sproc`/`udf`/`trigger` (#124), `--diagnostics`
(#127), `--otel` (#126).
- **Improvements**: incomplete-construct highlighting (#93),
theme-routed colors and JSON token highlighting (#97), source-caret
diagnostics (#99), refreshed prompt (#133), `ls` summaries (#129, #139).
- **Security**: MCP hardening (#120), CodeQL alerts in connect flow
(#132).
- **Fixes**: `;`-separator REPL highlighting (#141).
- **Breaking changes** (unchanged): `indexpolicy` removed/aliased to
`index` (#140); `theme edit --editor` removed.
- **Documentation** (#123) and **Build & pipeline** (#128, #121).

Items already shipped in 1.1.4 are not re-listed.

## Validation

- Documentation-only change; descriptions verified against
`docs/commands.md` and `docs/navigation.md`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants