Appearance
Gryd.Observability
Gryd.Observability provides the shared activity source, metrics collector, telemetry enrichers, and MediatR behavior. Gryd.Observability.OpenTelemetry adds OpenTelemetry instrumentation and exporters.
Registration
Register the Core services even when no exporter is configured:
csharp
using Gryd.Observability.Extensions;
using Gryd.Observability.OpenTelemetry.Extensions;
builder.Services.AddGrydObservability(builder.Configuration);
builder.Services.AddGrydOpenTelemetry(builder.Configuration);The default IMetricsCollector writes to a System.Diagnostics.Metrics.Meter named from the final Observability:ServiceName and versioned from Observability:ServiceVersion. It is registered with TryAdd, so an explicitly registered collector is preserved. The default collector owns its meter and disposes it with the service provider. Core metrics work without OpenTelemetry. A blank service name consistently resolves to gryd-service, and a blank version resolves to 1.0.0 for the default source, meter, and OpenTelemetry resource.
All AddGrydOpenTelemetry overloads use the final IOptions<ObservabilityOptions> resolved from dependency injection. OpenTelemetry subscribes to the actual IActivitySourceProvider.Source.Name and the default meter name. When enabled, it also subscribes automatically to GrydAudit.Recording for the audit write-loss counter, to Gryd.API.Idempotency for the idempotency filter counters and to Gryd.Infrastructure.AfterCommit for the failed after-commit effects counter. The service resource uses the final service name and version. Export and Prometheus scraping remain the host's responsibility, and the counter is an observation rather than durable audit accounting. An explicit OpenTelemetry:Enabled = false configures no Gryd exporter. A custom collector that publishes through a different meter remains responsible for its own meter subscription. If the registered activity source cannot be resolved or has no name, provider construction surfaces that registration error instead of silently subscribing to a different source.
Other module meters are not added automatically. Register only the meters for modules used by the host through the configureMetrics callback:
| Meter | Module |
|---|---|
GrydFiles.Scanner | Files scanner |
GrydFiles.Sweeps | Files background sweeps |
GrydAuth.Security.SecretProtection | Auth KMS secret protection |
csharp
using OpenTelemetry.Metrics;
using Gryd.Observability.OpenTelemetry.Extensions;
builder.Services.AddGrydOpenTelemetry(
builder.Configuration,
configureMetrics: metrics =>
{
metrics.AddMeter("GrydFiles.Scanner");
metrics.AddMeter("GrydFiles.Sweeps");
metrics.AddMeter("GrydAuth.Security.SecretProtection");
});This registers meters with the in-process provider; it does not install a collector, configure scraping, or create alert rules. The diagnostic redaction used by selected cache and Auth logs is not global exception or exporter redaction. Exception details can still contain application data, and the Npgsql filter below only removes the documented SQL attributes.
The optional tracing and metrics builder callbacks run synchronously when AddGrydOpenTelemetry is called, while the service collection is still writable. They are host-controlled SDK configuration and still run when Gryd export is disabled; a callback can add its own listeners or exporters. Register custom builder integrations there, since callbacks that need to add services must not be delayed until provider construction. Gryd adds the native ASP.NET Core and HttpClient activity sources and metric meters only when their final instrumentation flags are enabled. The ASP.NET request enricher is attached only when that source is enabled and observes request diagnostics without changing activity sampling. It adds HTTP method, server address and port, scheme, path, protocol version, endpoint route, response status, user-agent, and the configured correlation and tenant headers. A 5xx response marks the server activity as an error. Handled exceptions and the hosting unhandled-exception diagnostic event add exception type, message, and stack trace to the activity and an exception event. These exception details can contain application data; do not place secrets or personal data in exception messages. Request and response bodies are not captured. Gryd creates its selected exporter and metric reader from final options as the provider is built. Disabling Gryd OpenTelemetry leaves Gryd's default sources, meters, and exporters unsubscribed; it does not override host callback configuration. Native HttpClient activities provide standard request and transport status data; this enricher adds no custom HttpClient exception details.
Export destinations
OTLP uses gRPC over HTTP/2. OtlpEndpoint defaults to http://localhost:4317 and sends both traces and metrics there. Set MetricsOtlpEndpoint to route metrics to a separate OTLP receiver; in OTLP mode, a nonblank dedicated value overrides the shared endpoint for metrics.
Jaeger mode sends traces to JaegerEndpoint, which also defaults to http://localhost:4317. Jaeger's OTLP receiver accepts traces; metrics use a separate backend. Metrics are exported only when MetricsOtlpEndpoint names a separate metrics-capable OTLP receiver; when it is absent or blank, Gryd reports that Jaeger mode is traces-only and does not add a metrics exporter. See the Jaeger API documentation for its OTLP and legacy trace receiver endpoints.
| Exporter | Traces | Metrics |
|---|---|---|
otlp | OtlpEndpoint | MetricsOtlpEndpoint when nonblank, otherwise OtlpEndpoint |
jaeger | JaegerEndpoint | MetricsOtlpEndpoint when nonblank; otherwise not exported |
console | Console | Console |
prometheus | Console | Configure scraping separately |
These examples assume that the referenced receivers are running. 4319 below is an example of a separately configured metrics collector listener, not a default port. For Jaeger plus a separate collector, configure both destinations:
json
{
"Observability": {
"OpenTelemetry": {
"Enabled": true,
"Exporter": "jaeger",
"JaegerEndpoint": "http://localhost:4317",
"MetricsOtlpEndpoint": "http://localhost:4319"
}
}
}For a generic OpenTelemetry Collector that receives both signals on one OTLP gRPC listener:
json
{
"Observability": {
"OpenTelemetry": {
"Enabled": true,
"Exporter": "otlp",
"OtlpEndpoint": "http://localhost:4317"
}
}
}Migrate legacy Jaeger configurations that used JaegerEndpoint = http://localhost:14268/api/traces by pointing it at the Jaeger OTLP gRPC receiver, typically http://localhost:4317. Move any separate metrics receiver previously set in OtlpEndpoint to MetricsOtlpEndpoint. Trace delivery continues to use JaegerEndpoint. Required trace or shared OTLP endpoints that are blank or invalid fail provider startup with a property-specific configuration error. A blank MetricsOtlpEndpoint is treated as absent: OTLP mode falls back to OtlpEndpoint, while Jaeger mode stays traces-only.
Configured OTLP gRPC destinations must be absolute HTTP or HTTPS URLs with a host. User information, query strings, and fragments are rejected; a URI path prefix is allowed. This validates the address shape, not whether a remote receiver supports the selected protocol. Port numbers do not select the protocol: this API configures OTLP gRPC, so HTTP/protobuf on port 4318 is not selected by it. Gryd explicitly assigns the configured endpoint and gRPC protocol to the exporter, overriding those two general OTEL exporter environment settings. Other exporter options, such as headers, timeouts, TLS, and compression, are not replaced by this endpoint selection.
PostgreSQL tracing
PostgreSQL command tracing is opt-in through the Npgsql driver's ActivitySource named Npgsql. Set Observability:OpenTelemetry:InstrumentNpgsql to true to subscribe. This covers EF Core commands when the context uses the Npgsql provider, direct Npgsql commands, batches, and COPY operations. It does not add EF Core-level spans and does not instrument other database providers. EF Core applications must reference Npgsql.EntityFrameworkCore.PostgreSQL; direct-driver applications must reference Npgsql. Gryd does not install or supply the consuming application's database provider. The driver creates spans for executed commands, so no separate instrumentation package is needed. This tracing change requires no schema migration.
csharp
using Gryd.Observability.OpenTelemetry.Extensions;
using Microsoft.EntityFrameworkCore;
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("AppDatabase")));
builder.Services.AddGrydOpenTelemetry(builder.Configuration);The Generic Host starts the provider when the application starts. A test that builds a plain ServiceProvider should resolve TracerProvider to construct it.
json
{
"Observability": {
"OpenTelemetry": {
"InstrumentNpgsql": true,
"CaptureNpgsqlStatements": false
}
}
}All overloads read the final IOptions<ObservabilityOptions> after normal configuration and post-configuration. Both InstrumentNpgsql and CaptureNpgsqlStatements default to false; the old InstrumentEntityFramework property remains for configuration compatibility but is a no-op. Migrate by setting InstrumentNpgsql when the application uses PostgreSQL. Its existing true default does not enable tracing.
When Npgsql tracing is enabled, Gryd removes db.query.text, db.statement, and db.query.parameter.* attributes at span end by default, after the driver has populated them. Set CaptureNpgsqlStatements to true only when SQL capture is intended. SQL can contain inline literal values; bound parameters are not collected separately. This setting is a narrow SQL attribute filter, not general sanitization: driver exception events and status descriptions, custom span names, and host-added processors or exporters can still contain application data. An independently configured host pipeline can also observe Npgsql activities. Multiple TracerProvider instances share the same Activity, so a processor that removes attributes can affect what another provider sees. Keep secrets and personal data out of SQL literals and exception messages, and review every configured exporter and processor.
Metric collector contract
IncrementCounter records a long counter. RecordHistogram records a double histogram. RecordDuration records a double histogram with the ms unit. SetGauge records an absolute double value with the .NET synchronous gauge instrument. Instruments are reused by name, kind, and unit. Applications should use stable instrument names and bounded tag values; arbitrary IDs, payloads, recipient data, URLs, secrets, exception text, and personal data do not belong in metric tags.
Collector calls and other observation work must not change an accepted business result, replace an operation exception, or turn cancellation into failure. TelemetryObservation.Try is for telemetry actions only; the operation delegate must run outside it. TelemetryActivityScope.Start safely creates and closes spans. Pass an explicit ActivityContext for worker parentage; passing default(ActivityContext) creates a root without inheriting an unrelated ambient trace or baggage. Legacy operations that omit the explicit context continue to use the ambient parent.
Telemetry is an operational observation, not a durable accounting ledger. A measurement can be missing during process termination, exporter failure, or listener failure. Queue depth and download count are unavailable unless a real sampler or completed download signal exists.