Marten has built in support for emitting Open Telemetry spans and events, as well as for exporting metrics using System.Diagnostics.Metrics. This information should be usable with any monitoring system (Honeycomb, DataDog, Prometheus, SigNoz, Project Aspire, etc.) that supports Open Telemetry.
Heads up, .NET processes will not actually emit any metrics or Open Telemetry data they are collecting unless there are both configured exporters and you have explicitly configured your application to emit this information. And note that you have to explicitly export both metrics and Open Telemetry activity tracing independently. That all being said, here's a sample of configuring the exporting -- this case just exporting information to a Project Aspire dashboard in the end:
// This is passed in by Project Aspire. The exporter usage is a little
// different for other tools like Prometheus or SigNoz
var endpointUri = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
Console.WriteLine("OLTP endpoint: " + endpointUri);
builder.Services.AddOpenTelemetry().UseOtlpExporter();
builder.Services.AddOpenTelemetry()
.WithTracing(tracing =>
{
tracing.AddSource("Marten");
})
.WithMetrics(metrics =>
{
metrics.AddMeter("Marten");
});Note, you'll need a reference to the OpenTelemetry.Extensions.Hosting Nuget for that
extension method.
::: warning
This connection tracking is bypassed by some uncommonly used operations, but will apply to basically every common
operation done with IQuerySession or IDocumentSession.
:::
It's often important just to track how many connections your application is using, and how long connections are being used. To that end, you can opt into emitting Open Telemetry spans named marten.connection with this option:
using var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
// Track Marten connection usage
opts.OpenTelemetry.TrackConnections = TrackLevel.Normal;
});This option will also tag events on the activity for any exceptions that happened from executing database commands. At a minimum, this option might help you spot performance issues due to chattiness between your application servers and the database.
There is also a verbose mode that will also tag Open Telemetry activity events for all the Marten operations
(storing documents, appending events, etc.) performed by an IDocumentSession.SaveChangesAsync() call immediately
after successfully committing a database transaction. This mode is probably most appropriate for troubleshooting
or performance testing where the extra information being emitted might help you spot database usage issues. That
option is shown below:
using var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
// Track Marten connection usage *and* all the "write" operations
// that Marten does with that connection
opts.OpenTelemetry.TrackConnections = TrackLevel.Verbose;
});Marten exports a metrics counter for the events appended to Marten's event store functionality. This is on by default
since Marten 10; before that you opted in with TrackEventCounters(), which still works. The switch is:
using var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
// Track the number of events being appended to the system.
// On by default since Marten 10, set to false to opt out
opts.OpenTelemetry.EnableEventCounters = true;
});This is adding a Counter metric named marten.event.append.
This metric has tags for:
event.type- the Marten name for the type of the event within its configurationtenant.id- the tenant id for which the event was captured. If you are not using multi-tenancy, this value will be "DEFAULT" and can be just ignored
The marten.event.append counter and the marten.fetch_for_writing.events_replayed histogram below are the two
metrics Marten 10 turned on by default. opts.RestoreV9Defaults() turns both off again, along with the other
Marten 10 default changes. To turn them off without the rest, set
opts.OpenTelemetry.EnableEventCounters and opts.OpenTelemetry.EnableFetchForWritingMetrics to false.
You can add counters of your own that are recorded after every successful SaveChangesAsync(). Each one is created
on the Marten meter, and your callback receives the counter and the IChangeSet that was just committed, with
its inserted, updated and deleted documents and its appended events:
var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
// A counter on Marten's "Marten" meter, recorded after every
// successful SaveChangesAsync()
opts.OpenTelemetry.ExportCounterOnChangeSets<long>(
"billing.invoices.inserted",
"invoices",
(counter, commit) =>
{
var invoices = commit.Inserted.OfType<ConfigDocsInvoice>().Count();
if (invoices > 0)
{
counter.Add(invoices);
}
});
});The callback runs after the transaction has committed, so an exception thrown from it is logged rather than
reported to the caller of SaveChangesAsync(), and the other counters are still recorded. marten.event.append is
itself one of these counters.
The cost of a command handler that uses FetchForWriting is
mostly the cost of rebuilding the aggregate, and that in turn is however many events Marten had to read
back and fold. How many that is depends entirely on the aggregate's projection lifecycle, and it is not
something you can see from the outside. Marten measures it by default since 10.0. Before that you opted in with
TrackFetchForWritingMetrics(), which still works. The switch is:
using var store = DocumentStore.For(opts =>
{
opts.Connection("some connection string");
// How many events does each FetchForWriting() call actually replay?
// On by default since Marten 10, set to false to opt out
opts.OpenTelemetry.EnableFetchForWritingMetrics = true;
});This adds a Histogram
named marten.fetch_for_writing.events_replayed, recorded once per FetchForWriting() call, with tags for:
aggregate.type- the .NET type name of the aggregate that was fetchedfetch.plan- which plan served the call:Live,InlineorAsync
Reading it is straightforward, because each plan has a characteristic shape:
fetch.plan |
What the histogram shows |
|---|---|
Live |
The full stream length, every single time — the aggregate is rebuilt from scratch on each fetch |
Async |
Only the events the async daemon has not projected yet, so this doubles as a per-fetch view of daemon lag |
Inline |
Always zero — the stored snapshot is current by construction |
The histogram's own count gives you fetch frequency for free, so a sum by (aggregate.type) tells you
which aggregates dominate your event replay, and the Live percentiles tell you what switching one of
them to Inline would actually save before you commit to that migration.
::: tip
With EnableFetchForWritingMetrics = false the histogram is never created, so turning it off leaves a null check
on the fetch path and nothing more. With it on and nothing listening to the meter, a fetch checks the histogram's
Enabled flag and records nothing. Both tag values are cached per aggregate type, so a recorded fetch
allocates nothing on the heap either.
:::
See Open Telemetry and Metrics within the Async Daemon documentation.
The daemon also has a counter named marten.daemon.late_events, with one count for each event that committed inside a
sequence range the high water mark had already skipped. It is tagged with:
event.type- the event type's nametenant.id- the event's tenant idskip.reason- why the range was skipped:Proven,LiveTransaction,WallClockorManual, orUnknownwhen no reason was recorded
A healthy store never records one, so the counter is only created when the first late event is found. It needs
opts.Events.EnableAdvancedAsyncTracking, which is on by default, and RestoreV9Defaults() does not turn it off. See
Events that commit late.