ActivitySource
SHOULD only be created once and reused throughout the
application lifetime. This
example shows how
ActivitySource
is created as a static
field and then used in the
application. You could also look at this ASP.NET Core
example which shows a more Dependency
Injection friendly way of doing this by extracting the ActivitySource
into a
dedicated class called
Instrumentation which is then
added as a Singleton
service.
As shown in the getting started guide, it
is very easy to manually create Activity
. Due to this, it can be tempting to
create too many activities (eg: for each method call). In addition to being
expensive, excessive activities can also make trace visualization harder.
Instead of manually creating Activity
, check if you can leverage
instrumentation libraries, such as ASP.NET
Core,
HttpClient which will
not only create and populate Activity
with tags(attributes), but also take
care of propagating/restoring the context across process boundaries. If the
Activity
produced by the instrumentation library is missing some information
you need, it is generally recommended to enrich the existing Activity with that
information, as opposed to creating a new one.
Tags such as MachineName
, Environment
etc. which are static throughout the
process lifetime should be be modelled as Resource
, instead of adding them
to each Activity
. Refer to this
doc for details and
examples.
- The
ActivitySource
used to create theActivity
is not added to theTracerProvider
. UseAddSource
method to enable the activity from a givenActivitySource
. TracerProvider
is disposed too early. You need to ensure that theTracerProvider
instance is kept active for traces to be collected. In a typical application, a single TracerProvider is built at application startup, and is disposed of at application shutdown. For an ASP.NET Core application, useAddOpenTelemetry
andWithTraces
methods from theOpenTelemetry.Extensions.Hosting
package to correctly setupTracerProvider
. Here's a sample ASP.NET Core app for reference. For simpler applications such as Console apps, refer to this example.- TODO: Sampling