Processors
A processor sees every record before it is exported, and can change it or drop it: add a tenant id to every
span, rename a noisy log, drop health checks and static files, strip an attribute only your app knows is
sensitive. On the .NET side it is one small interface with three optional methods; in the browser it is a
plain function, next to setAttributes for context every record should carry.
IBelemetryProcessor
OnSpan, OnLog and OnMetric each receive the record - a
BelemetrySpan, BelemetryLogRecord or BelemetryMetric, every member of
which may be changed - and return false to drop it. All three have default implementations that
keep the record, so a processor implements only what it cares about. Add processors with
options.AddProcessor; they run in the order they were added, after the built-in privacy rules.
public sealed class TenantProcessor(ITenantAccessor tenants) : IBelemetryProcessor
{
public bool OnSpan(BelemetrySpan span)
{
span.Attributes.Set("tenant.id", tenants.Current);
return span.Name != "GET /health"; // false drops it
}
public bool OnLog(BelemetryLogRecord log) => log.Body?.Contains("noise") is not true;
// OnMetric is not implemented: the default keeps every metric.
}
builder.Services.AddBitBelemetryServices(options =>
{
options.AddProcessor(new TenantProcessor(tenantAccessor));
});
Bit.Belemetry category, so a broken filter does not pass for
missing data. A record a processor drops is counted as filtered on the
Bit.Belemetry meter.
Which records they see
Processors run on every record of the .NET pipeline:
ILoggerentries,Activityspans andMetermetrics.IBelemetrycalls on WebAssembly and Hybrid, where they go through the .NET pipeline.- Browser records relayed to .NET with the Relay transport (the default in Hybrid) - so one processor can enrich page views, clicks and fetch spans as well.
- Everything the server pipeline collects, when it is on.
With the Direct transport (the default outside Hybrid) the browser script exports its own records, and .NET
never sees them. Shape those in the page instead: with a browser processor (below), or with the browser
options - IgnoreUrls, IgnoreErrorMessages, data-bit-belemetry-ignore and the
privacy rules.
In the browser
BitBelemetry.addProcessor(fn) runs fn(record) on every browser record as it is
exported (or relayed to .NET), after consent and the privacy rules. Change the record in place, or return
false to drop it; one that throws is skipped for that record. It returns the function that removes
it, and may be called before the script starts - a processor added right after the script loaded still sees
the initial page view. The record is the browser record model: a span has traceId,
spanId, name, kind, startTime, endTime,
status and events; a log record has time, severityNumber,
body and eventName; both have attributes, scope and
feature.
BitBelemetry.setAttributes(attributes) adds attributes to every record from now on - app context
such as a tenant or a feature flag. Calls merge; a null value removes one key, and
setAttributes(null) removes them all. They go through the privacy rules like any attribute, a
record's own attributes win, and the consent-governed ids (session.id,
enduser.pseudo.id, user.id) cannot be set this way - use setUser.
- Processor
- not added
- Global attributes
- none set from this page
Then make records
Nothing yet - add the processor, then press the buttons (clicks need Interactions, the event needs Events).
Open a record to read its attributes. With the processor added, every record carries
app.release.channel and the noisy button's clicks never arrive; with the attributes set, every
record carries app.tenant and feature.checkout_v2. Both live in the script, not in the
page: they stay for the rest of the visit, on every page of this site, until you remove them or reload.
// Anywhere after the module loaded - a script tag, a JS initializer, or IJSRuntime from .NET.
const remove = BitBelemetry.addProcessor(record => {
// Drop: clicks on an element the dashboards do not care about.
if (record.eventName === 'browser.user_interaction' && record.attributes['target.name'] === 'demo.processors.noise') return false;
// Enrich: the release channel this page was served from.
record.attributes['app.release.channel'] = 'beta';
});
// Context for every record from now on (merged with what was set before).
BitBelemetry.setAttributes({ 'app.tenant': 'contoso', 'feature.checkout_v2': true });
BitBelemetry.setAttributes({ 'feature.checkout_v2': null }); // removes one key
BitBelemetry.setAttributes(null); // removes them all
remove(); // the processor is gone
A real one: DemoNoiseFilter
This site runs the processor below on both of its pipelines, the WebAssembly one and the server's. The live
feeds poll /api/telemetry every two seconds with HttpClient, and the .NET side
exports to /otlp; without it every reader would see a span and the framework's request logs for each
of those (it keeps a warning or an error, though), and the server would
record a request span for every static file and every piece of the WebAssembly payload. The browser script
does not need it: the same URLs are in its IgnoreUrls.
No .NET HTTP spans yet - press the first button; the second never shows up here.
public sealed class DemoNoiseFilter : IBelemetryProcessor
{
private static readonly string[] _noise =
[
.. DemoTelemetry.SelfUrls, // "/otlp/", "/api/telemetry"
"/_framework/", "/_content/", "/_blazor", "/images/", "/.well-known/", "/_vs/", "/browserLink",
];
// The site's stylesheets, scripts and icons, served at the root under fingerprinted names.
private static readonly string[] _staticFiles = [".css", ".js", ".map", ".svg", ".png", ".ico", ".woff2"];
public bool OnSpan(BelemetrySpan span)
{
var url = span.Attributes.GetString(BelemetrySemanticConventions.UrlFull)
?? span.Attributes.GetString(BelemetrySemanticConventions.UrlPath)
?? span.Attributes.GetString(BelemetrySemanticConventions.HttpRoute);
return url is null || IsNoise(url) is false;
}
public bool OnLog(BelemetryLogRecord log)
{
// Warnings and errors are kept: a failure of the receiver or the feed is worth seeing.
if (log.Severity >= BelemetrySeverity.Warn) return true;
// The framework's own logs of those requests: ASP.NET Core's carry the path (RequestPath in the
// scope, Path in the hosting logs), HttpClient's the URL (Uri). Read by category, so an app log's
// own Path is never mistaken for one.
var category = log.Scope.Name;
var url = log.Attributes.GetString("RequestPath")
?? (category.StartsWith("Microsoft.AspNetCore.", StringComparison.Ordinal) ? log.Attributes.GetString("Path") : null)
?? (category.StartsWith("System.Net.Http.HttpClient", StringComparison.Ordinal) ? log.Attributes.GetString("Uri") : null);
return url is null || IsNoise(url) is false;
}
private static bool IsNoise(string url)
{
foreach (var noise in _noise)
{
if (url.Contains(noise, StringComparison.OrdinalIgnoreCase)) return true;
}
var path = url.AsSpan();
var end = path.IndexOfAny('?', '#');
if (end >= 0) path = path[..end];
foreach (var extension in _staticFiles)
{
if (path.EndsWith(extension, StringComparison.OrdinalIgnoreCase)) return true;
}
return false;
}
}
// Shared by the client and the server (Services/DemoTelemetry.cs). Dropping is noise reduction, not privacy:
// the session id each poll of /api/telemetry carries is redacted on every record that still names it.
options.Privacy.RedactQueryParameters.Add("sessionId");
options.AddProcessor(new DemoNoiseFilter());Enrich, rewrite, drop
Enriching is setting attributes. Rewriting is assigning members: a span's Name, a log's
Body or Severity, a metric's points. Dropping is returning false. The
same processor can do all three, for any of the three signals.
API
IBelemetryProcessor
Interface
Inspects, enriches or drops records on the .NET side before they are queued for export - including records relayed from the browser.
| Name | Type and default | Description |
|---|---|---|
OnSpan(BelemetrySpan span) |
bool |
Called for every finished span. Return false to drop it. |
OnLog(BelemetryLogRecord log) |
bool |
Called for every log record or event. Return false to drop it. |
OnMetric(BelemetryMetric metric) |
bool |
Called for every collected metric. Return false to drop it. |