Migration from Activities
Migration from Activities
Section titled “Migration from Activities”This guide covers converting hand-written ActivitySource/Activity code to generated telemetry interfaces. For the fastest path, use the IDE refactorings — this page documents the manual fallback.
Before
Section titled “Before”using System.Diagnostics;
public class OrderService{ static readonly ActivitySource _source = new("OrderService");
public void PlaceOrder(int orderId) { using var activity = _source.StartActivity("PlaceOrder", ActivityKind.Internal); activity?.AddEvent(new ActivityEvent("Validated")); }}using System.Diagnostics;using Purview.Telemetry;
[ActivitySource]public interface IOrderServiceTracing{ [Activity] Activity? PlaceOrder(int orderId);
[Event] void Validated(Activity? activity);}
public class OrderService(IOrderServiceTracing tracing){ public void PlaceOrder(int orderId) { using var activity = tracing.PlaceOrder(orderId); tracing.Validated(activity); }}Register the interface in DI:
services.AddOrderServiceTracing();Common conversions
Section titled “Common conversions”| Original code | Generated method signature |
|---|---|
_source.StartActivity("PlaceOrder") | [Activity] Activity? PlaceOrder(); |
_source.StartActivity("Order", ActivityKind.Internal) | [Activity(ActivityKind.Internal)] Activity? Order(); |
activity.AddEvent(new ActivityEvent("Loaded")) | [Event] void Loaded(Activity? activity); |
activity.SetBaggage("tenant", tenantId) | [Context] void SetTenant(Activity? activity, [Baggage] string tenantId); |
activity.SetTag("tenant", tenantId) | [Context] void SetTenant(Activity? activity, [Tag] string tenantId); |
Return types
Section titled “Return types”- Activity-starting methods should return
Activity?so callers can dispose and reuse the Activity. - Event and Context methods should accept the
Activity?as their first parameter and typically returnvoid.
Tags and baggage
Section titled “Tags and baggage”Use [Tag] and [Baggage] on parameters to control how values are attached — see Tags, Baggage, and Parameter Attributes.
Naming
Section titled “Naming”The ActivitySource name defaults to the assembly name with casing preserved. Set [ActivitySource("MyApp")] to override. v4+ converts tag/baggage keys to snake_case by default; revert with [assembly: TelemetryGeneration(NamingConvention = NamingConvention.Legacy)].
Checklist
Section titled “Checklist”- Add the
Purview.Telemetry.SourceGeneratorpackage. - Create the
[ActivitySource]interface (or run the ActivitySource refactoring). - Replace
ActivitySourcefields andStartActivity/AddEvent/SetTag/SetBaggagecalls with the interface. - Register with
services.Add{InterfaceNameWithoutI}(). - Rebuild and review the Diagnostics.
Next steps
Section titled “Next steps”- Refactorings — the shipped IDE conversions
- Activities — full attribute reference
- Multi-Targeting — combine with Logging and Metrics