Guides OpenTelemetry observability for CrestApps.Core 2.0: wiring ActivitySource/Meter, configuring exporters, managing tag cardinality, and troubleshooting traces/metrics/logs.
Ships in crestapps-core. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Every CrestApps.Core package reports through one ActivitySource and one Meter, both named CrestApps.Core (CrestAppsTelemetry.ActivitySource and CrestAppsTelemetry.Meter). They use System.Diagnostics only, so the framework packages take no OpenTelemetry dependency. Nothing is recorded until a listener subscribes. The CrestApps.Core.OpenTelemetry package connects them to the OpenTelemetry SDK.
CrestApps.Core.OpenTelemetry in the host and call AddCrestAppsOpenTelemetry() on the OpenTelemetry builder. It registers source and meter names only. It adds no exporter, no ASP.NET Core or HttpClient instrumentation, and no runtime meter.CrestApps.Core spans and metrics do not depend on any option. They are recorded whenever a listener subscribes. The EnableOpenTelemetry AI option only adds the Microsoft.Extensions.AI gen_ai.* telemetry, and EnableSensitiveData only adds prompt and response content to it.src/CrestApps.Core.Docs/docs/core/observability.md in the CrestApps.Core repository). Read it before you chart, alert on or add a name.<ItemGroup>
<PackageReference Include="CrestApps.Core.OpenTelemetry" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
</ItemGroup>
using CrestApps.Core.OpenTelemetry;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
builder.Services.AddOpenTelemetry()
.WithTracing(tracing => tracing
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation())
.WithMetrics(metrics => metrics
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation())
.AddCrestAppsOpenTelemetry()
.UseOtlpExporter();
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});
UseOtlpExporter() sends traces, metrics and logs to OTEL_EXPORTER_OTLP_ENDPOINT, and OTEL_SERVICE_NAME names the service. The Aspire AppHost sets both for every project it runs. When OTLP is the only exporter, skip the block while OTEL_EXPORTER_OTLP_ENDPOINT is empty, as the sample hosts do.IncludeScopes, each log line of a chat turn carries the SessionId and ProfileId scope properties (InteractionId for a Chat Interaction), next to its trace id.builder.Logging.ClearProviders() call, or that call removes it. It runs next to NLog or Serilog.builder.Services.AddOpenTelemetry().AddCrestAppsOpenTelemetry(). AddOpenTelemetry() is safe to call more than once. A host can instead register the same names with AddSource and AddMeter.| Source or meter | Contents |
|---|---|
CrestApps.Core | The framework spans and metrics. Names and tags are in CrestAppsTelemetry (CrestApps.Core.Abstractions) and AITelemetry (CrestApps.Core.AI.Abstractions) |
CrestApps.Core.* | Meters of standalone libraries, such as CrestApps.Core.Templates (TemplateTelemetry.MeterName) with crestapps.core.templates.render.duration |
AICompletionService | The gen_ai.* spans and metrics of the Microsoft.Extensions.AI clients while EnableOpenTelemetry is on. The constant is CrestAppsTelemetry.AIClientSourceName |
OpenAI.* | The OpenAI SDK's own gen_ai.* telemetry, which the Azure OpenAI chat-completions client reports through |
Microsoft.AspNetCore.SignalR.Server, Microsoft.AspNetCore.Http.Connections | The hub invocation a chat turn starts from (spans) and the active connections (meter) |
Experimental.ModelContextProtocol, A2A, A2A.AspNetCore | MCP and A2A SDK client and server operations |
Npgsql | PostgreSQL commands, when the pgvector store is used |
Polly | Retries, timeouts and open circuits of resilience pipelines that have a name and telemetry configured (meter only) |
AddCrestAppsOpenTelemetry() registers all of these names.
One chat turn is one trace. The gen_ai spans appear while EnableOpenTelemetry is on:
crestapps.chat.turn the chat hub received a message
├── crestapps.orchestrate.context the orchestration context is built
│ └── crestapps.rag.preemptive one per preemptive retrieval handler
│ └── crestapps.index.search one per index query
├── crestapps.guardrail.check when the profile has guardrails
└── crestapps.orchestrate the orchestrator reply, also for sub-agents
├── crestapps.orchestrate.plan when the default orchestrator plans its tools
├── chat {model} gen_ai, one per provider round trip
└── execute_tool {tool} gen_ai, one per tool call
└── crestapps.tool.invoke the traced tool invoker, with tool.name and tool.source
Other entry points start their own trace: crestapps.realtime.session (a voice conversation), crestapps.ingest.run (a file source or crawler run), crestapps.ingest.process (one uploaded document), crestapps.mcp.connect (an MCP connection and handshake) and crestapps.background.cycle (one pass of a periodic service). Queued work runs in crestapps.background.item, a new trace with a link back to the request that queued it.
Spans carry the ids that help find a trace: crestapps.session.id, crestapps.profile.id and crestapps.data_source.id (AITelemetry.SessionIdAttribute, ProfileIdAttribute and DataSourceIdAttribute).
Durations are Histogram<double> instruments in seconds with advised bucket boundaries. The outcome tag is ok, error, cancelled or blocked, and some instruments add partial or empty.
| Area | Main instruments (tags) |
|---|---|
| Orchestration and completions | crestapps.core.ai.orchestration.duration and crestapps.core.ai.orchestration.time_to_first_token (orchestrator, outcome), crestapps.core.ai.completion.time_to_first_token and crestapps.core.ai.completion.errors (client, error.type), crestapps.core.ai.planning.duration |
| Tools and handlers | crestapps.core.ai.tool.duration (tool.source, outcome), crestapps.core.ai.tool.denied (reason: access when IAIToolAccessEvaluator refused a Chat Interaction tool, approval when a tool that requires approval was replaced with a "was not run" stand-in in a request that cannot ask), crestapps.core.ai.tool.auto_approved (reason, tool.kind, agent: approval tools a profile's policy ran without asking), crestapps.core.ai.tools.scoped_count, crestapps.core.ai.handler.errors (handler) |
| Safety and limits | crestapps.core.ai.security.prompt_checks (decision, direction), crestapps.core.ai.guardrail.duration, crestapps.core.ai.ratelimit.rejections (limiter), crestapps.core.ai.resilience.retries, crestapps.core.ai.resilience.stream_failures |
| Retrieval and ingestion | crestapps.core.ai.retrieval.duration (mode, outcome), crestapps.core.ai.retrieval.fallbacks, crestapps.core.ai.embedding.fallbacks, crestapps.core.ai.ingestion.duration, crestapps.core.ai.ingestion.run.items (result), crestapps.core.ai.webcrawler.crawl.duration |
| Chat, voice and MCP | crestapps.core.chat.turn.duration, crestapps.core.realtime.session.duration (end_reason), crestapps.core.realtime.sessions.active, crestapps.core.realtime.first_audio_latency, crestapps.core.mcp.client.connect.duration (transport, outcome) |
| Usage | crestapps.core.ai.usage.requests, crestapps.core.ai.usage.tokens (adds token.type) and crestapps.core.ai.usage.duration (operation, client, deployment, purpose, streaming), only with AddCoreAIUsageTracking() |
| Framework | crestapps.core.index.operation.duration (provider, operation, outcome), crestapps.core.store.commit.duration (store), crestapps.core.queue.depth and crestapps.core.queue.wait.duration (queue), crestapps.core.background.item.duration and crestapps.core.background.cycle.duration (service) |
While EnableOpenTelemetry is on, Microsoft.Extensions.AI adds gen_ai.client.operation.duration and gen_ai.client.token.usage under AICompletionService.
The two switches are on DefaultAIOptions. The framework registers a scoped DefaultAIOptions that applies a site's GeneralAIOptions overrides, and framework code takes that scoped instance.
| Option | Default | Effect |
|---|---|---|
EnableOpenTelemetry | false | Adds UseOpenTelemetry(...) with the AICompletionService name to the OpenAI, Azure AI Inference and Ollama completion clients, the Azure OpenAI Responses path, the Claude orchestrator, and the embedding, image, speech-to-text, text-to-speech and realtime clients from IAIClientFactory. Each chat round trip gets a chat {model} span and each tool call an execute_tool span |
EnableSensitiveData | false | Adds prompts, responses and tool arguments to those gen_ai.* spans. They can contain personal data. It applies only while EnableOpenTelemetry is on |
using CrestApps.Core.AI.Models;
builder.Services.Configure<DefaultAIOptions>(options =>
{
options.EnableOpenTelemetry = true;
});
GeneralAIOptions.OverrideEnableOpenTelemetry with EnableOpenTelemetry, and OverrideEnableSensitiveData with EnableSensitiveData. A settings page that saves a value without its flag changes nothing.EnableOpenTelemetry does not reach it. Its gen_ai.* telemetry comes from the SDK after the host calls AppContext.SetSwitch("OpenAI.Experimental.EnableOpenTelemetry", true) at startup. AddCrestAppsOpenTelemetry() collects the OpenAI.* names but does not set the switch.IAIClientFactory.CreateChatClientAsync gets no gen_ai spans by itself. Add UseOpenTelemetry last, so it sits below the tool loop and each round trip and each tool call gets its own span:using CrestApps.Core;
using CrestApps.Core.AI.Tooling;
using Microsoft.Extensions.AI;
var chatClient = await clientFactory.CreateChatClientAsync(deployment, builder =>
{
builder.UseFunctionInvocation(loggerFactory, invocation => invocation.UseTextToolResults());
if (defaultOptions.EnableOpenTelemetry)
{
builder.UseOpenTelemetry(loggerFactory, CrestAppsTelemetry.AIClientSourceName, telemetry => telemetry.EnableSensitiveData = defaultOptions.EnableSensitiveData);
}
});
UseTextToolResults() sends each tool call through AIToolInvocation, the traced invoker that records crestapps.tool.invoke and crestapps.core.ai.tool.duration. A custom tool loop calls AIToolInvocation.InvokeAsync(function, arguments, cancellationToken) itself. A tool that returns its failure to the model as text calls AIToolInvocation.ReportFailure(), so the call still counts as an error.
Each tag value multiplies the series a collector stores. Tag metrics with bounded values only: the provider or client, the operation, the purpose, the outcome, the direction, the orchestrator and other small sets. Never tag a metric with:
Put an id on the span (activity?.SetTag(AITelemetry.SessionIdAttribute, sessionId)) and in the log scope. A tool name goes on the span as tool.name, never on crestapps.core.ai.tool.duration.
EnableSensitiveData. It is off by default, and it adds prompts, responses and tool arguments to the Microsoft.Extensions.AI gen_ai.* spans only.Failures that the pipeline logs and then swallows, so that the request can continue, have their own event ids and a counter. Alert on them without matching message text.
| Range | Area | Examples |
|---|---|---|
| 1000s | A2A | A2ALog |
| 2000s | AI pipeline | 2001 completion failed, 2002 planning failed, 2003 handler skipped, 2004 guardrail check failed, 2101 and 2102 security audit of input and output |
| 4000s | Document processing and ingestion | 4001 a document could not be read, 4002 embeddings failed |
| 5000s | Retrieval | 5001 hierarchical retrieval fell back to chunks |
| 9000s | Sample hosts | The MVC and Blazor samples' own messages |
A new message in framework code uses [LoggerMessage] source generation with the next free id in its area's range, next to the area's existing log class.
Run dotnet run --project src/Startup/CrestApps.Core.Aspire.AppHost and open the dashboard link it prints. The AppHost sets OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_SERVICE_NAME for both sample hosts.
Without the AppHost, start the dashboard alone:
docker run --rm -it -p 18888:18888 -p 4317:18889 mcr.microsoft.com/dotnet/aspire-dashboard:latest
Set OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 and OTEL_SERVICE_NAME=MvcWeb for the host. Open http://localhost:18888 with the login link from the container output.
In the sample's admin Settings page, turn on Enable OpenTelemetry and save, to add the gen_ai spans.
Send a chat message on a profile that has a tool or a data source.
Under Traces, open the trace that contains crestapps.chat.turn and compare it with the tree above. Under Metrics, look for crestapps.core.* and gen_ai.*. Under Structured logs, filter by the trace id and check SessionId, ProfileId and the event ids.
Without a collector, dotnet-counters monitor -n <process name> --counters CrestApps.Core shows the metrics live.
These rules apply to code in CrestApps.Core. Application code uses its own meter and source names, or records into an existing instrument with the documented tags.
CrestAppsTelemetry for framework services, AITelemetry for the AI pipeline. A standalone library that does not reference those abstractions creates its own CrestApps.Core.<Area> meter, like TemplateTelemetry. AddCrestAppsOpenTelemetry() collects it through CrestApps.Core.*.crestapps.core.<area>.<thing>, lowercase, with _ between the words of one part (time_to_first_token). Name a span crestapps.<area>.<operation>, without core (crestapps.tool.invoke).CrestAppsTelemetry.CreateDurationHistogram(name, description), which sets the s unit and the bucket advice. Pass CrestAppsTelemetry.LongDurationBucketBoundaries for work that takes minutes. Give a counter a unit in braces, such as {error}.CrestAppsTelemetry (OutcomeTag, ErrorTypeTag, ProviderTag, OperationTag, ServiceTag, QueueTag, StoreTag) and AITelemetry (ClientTag, ReasonTag, ModeTag, PurposeTag and the others). Add a new constant next to them instead of a string literal. Set outcome from OutcomeOk, OutcomeBlocked or CrestAppsTelemetry.GetOutcome(exception).CrestAppsTelemetry.ActivitySource.StartActivity(...) and use ?. on it, because it is null when nothing listens.CrestAppsTelemetryTests.// In AITelemetry, next to the other instruments.
public static readonly Histogram<double> SummaryDuration = CrestAppsTelemetry.CreateDurationHistogram(
"crestapps.core.ai.summary.duration",
"The duration of summarizing one conversation.");
using var activity = CrestAppsTelemetry.ActivitySource.StartActivity("crestapps.chat.summarize");
activity?.SetTag(AITelemetry.SessionIdAttribute, sessionId);
var startTimestamp = Stopwatch.GetTimestamp();
var outcome = CrestAppsTelemetry.OutcomeOk;
try
{
return await CreateSummaryAsync(sessionId, cancellationToken);
}
catch (Exception ex)
{
outcome = CrestAppsTelemetry.GetOutcome(ex);
activity?.SetStatus(ActivityStatusCode.Error, ex.Message).AddException(ex);
throw;
}
finally
{
AITelemetry.SummaryDuration.Record(
Stopwatch.GetElapsedTime(startTimestamp).TotalSeconds,
new KeyValuePair<string, object>(CrestAppsTelemetry.OutcomeTag, outcome));
}
The complete service and its test are in observability-examples.md.
The request that queued an item has usually ended when a worker picks it up. Capture its context when the item is queued, and start the worker's span with StartLinkedActivity. The span starts a new trace with a link back, not a child span:
// On the work item, set when it is created.
public ActivityContext QueuedContext { get; } = Activity.Current?.Context ?? default;
public long QueuedTimestamp { get; } = Stopwatch.GetTimestamp();
var queueTag = new KeyValuePair<string, object>(CrestAppsTelemetry.QueueTag, "reports");
// When the item is written to the channel.
CrestAppsTelemetry.QueueDepth.Add(1, queueTag);
// When the worker reads it.
CrestAppsTelemetry.QueueDepth.Add(-1, queueTag);
CrestAppsTelemetry.QueueWaitDuration.Record(Stopwatch.GetElapsedTime(workItem.QueuedTimestamp).TotalSeconds, queueTag);
using var activity = CrestAppsTelemetry.StartLinkedActivity("crestapps.background.item", workItem.QueuedContext);
Record the item on CrestAppsTelemetry.BackgroundItemDuration with service, work_item_type and outcome, and each pass of a periodic service on BackgroundCycleDuration.
An async iterator resumes with its consumer's Activity.Current, not with the span it started. Restore the span after each yield return, or the spans started for the next item get the consumer's span as their parent:
using var activity = CrestAppsTelemetry.ActivitySource.StartActivity("crestapps.example.stream");
await foreach (var update in updates.WithCancellation(cancellationToken))
{
yield return update;
if (activity is not null)
{
Activity.Current = activity;
}
}
Record the duration in a finally block. It also runs when the consumer stops early.
Listen with MeterListener and ActivityListener. No OpenTelemetry SDK is needed. The CrestApps.Core tests share a TelemetryCollector in tests/CrestApps.Core.Tests/Core/Telemetry, and observability-examples.md has a complete test.
var measurements = new ConcurrentQueue<string>();
var activities = new ConcurrentQueue<Activity>();
using var meterListener = new MeterListener
{
InstrumentPublished = (instrument, listener) =>
{
if (instrument.Meter.Name == CrestAppsTelemetry.SourceName)
{
listener.EnableMeasurementEvents(instrument);
}
},
};
meterListener.SetMeasurementEventCallback<double>((instrument, value, tags, state) => measurements.Enqueue(instrument.Name));
meterListener.Start();
using var activityListener = new ActivityListener
{
ShouldListenTo = source => source.Name == CrestAppsTelemetry.SourceName,
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllDataAndRecorded,
ActivityStopped = activities.Enqueue,
};
ActivitySource.AddActivityListener(activityListener);
StartActivity returns null unless a listener samples the span, so the code under test must use activity?..CrestApps.Core.Templates has its own name. Match it in InstrumentPublished as well.CrestAppsTelemetryTests checks that every documented instrument is published, and that every duration is a Histogram<double> in seconds with bucket advice.crestapps.* spans or crestapps.core.* metrics: the names are not registered, or nothing exports them. Call AddCrestAppsOpenTelemetry() and add an exporter. UseOtlpExporter() needs OTEL_EXPORTER_OTLP_ENDPOINT.gen_ai spans: EnableOpenTelemetry is off, or a site saved it without OverrideEnableOpenTelemetry. For Azure OpenAI chat completions, set the OpenAI.Experimental.EnableOpenTelemetry switch. A chat client from CreateChatClientAsync needs its own UseOpenTelemetry.gen_ai spans have no prompts: EnableSensitiveData is off, which is the default.Activity.Current after yield return.crestapps.background.item.SessionId: IncludeScopes is off, or the OpenTelemetry log provider was added before ClearProviders()..RequireApproval() or an approval kind) and the request cannot ask; crestapps.core.ai.tool.denied counts it with reason=approval. See crestapps-core-ag-ui.HttpClients, so each request is written under System.Net.Http.HttpClient.<name> (for example CrestApps.OpenAI). Raise those categories to Warning to quiet them.crestapps.core.ai.usage.tokens to gen_ai.client.token.usage, or adds every token.type. See crestapps-core-ai-usage-metering.EnableOpenTelemetry now covers more than chat: the embedding, image, speech and realtime clients, the Claude orchestrator and the Azure OpenAI Responses path. The gen_ai source keeps the name AICompletionService, so an existing AddSource("AICompletionService") keeps working.ProviderAICompletionClient<TProvider> and ClaudeOrchestrator take the scoped DefaultAIOptions instead of IOptions<DefaultAIOptions>, so a site's settings reach them. Only code that constructs them directly changes.OverrideEnableOpenTelemetry and OverrideEnableSensitiveData with the values. Before, the sample hosts' Enable OpenTelemetry switch had no effect.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/crestapps-core:crestapps-core-observabilityfrom crestapps-core
npx claudepluginhub crestapps/crestapps.agentskills --plugin crestapps-core51 skills in this plugin
/crestapps-core-a2aSkill for Agent-to-Agent (A2A) protocol support in CrestApps.Core on the A2A 1.0 SDK. Covers AddA2AClient and AddCoreAIA2AClient with Entity Framework Core or YesSql connection stores, A2AConnection and A2AConnectionMetadata authentication, AIProfileA2AMetadata, the agent card cache and A2AToolRegistryProvider, the listAvailableAgents, findAgentForTask and findToolsForTask tools, building an A2A host with IAgentHandler, A2AServer, ITaskStore and MapA2A, A2A 1.0 agent cards with supportedInterfaces published at /.well-known/agent-card.json, A2AHostOptions authentication and ExposeAgentsAsSkill, system agents exposed through the host, and upgrading from the 0.3 SDK./crestapps-core-ag-uiSkill for running the CrestApps.Core 2.0 AI chat as an AG-UI agent. Covers the CrestApps.Core.AI.AGUI package (AGUIRunEventWriter, AGUIRunRequest, AGUIJson), AIChatHubCore.RunAgent over SignalR and MapAGUIChat with Server-Sent Events, the RunAgentInput members and streamed events, tool approvals with RequireApproval or an approval tool kind, ApprovalRequiredAIFunction, interrupts and resume, approvals inside agents, page context with crestAppsAGUI.registerContextProvider, shared state with registerStateProvider and update_shared_state, frontend tools, stored tool calls (ChatTurnToolActivity), runs that continue on the next page, AGUIChatOptions.DisconnectedRunTimeout, the protocol and sendPageContext chat options, and the AICompletionContextKeys and ChatResponseHandlerContext members. Use it when showing tool calls in the chat, asking a person before a tool runs, sharing page state with the agent, connecting CopilotKit or another AG-UI client, or troubleshooting approvals and continued runs.Other plugins' skills for the same job