Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resources doc for .NET #1795

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions content/en/docs/instrumentation/net/resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
title: "Resources"
weight: 6
description:
---

A [resource][] represents the entity producing telemetry as resource attributes.
For example, a process producing telemetry that is running in a container on
Kubernetes has a Pod name, a namespace, and possibly a deployment name. All
three of these attributes can be included in the resource.

In your observability backend, you can use resource information to better
investigate interesting behavior. For example, if your trace or metrics data
indicate latency in your system, you can narrow it down to a specific container,
pod, or kubernetes deployment.

## Setup

Follow the instructions in the [Getting Started][], so that you have a running
.NET app exporting data to the console.

## Adding resources with environment variables

You can use the `OTEL_RESOURCE_ATTRIBUTES` environment variable to inject
cartermp marked this conversation as resolved.
Show resolved Hide resolved
resources into your application. The .NET SDK will automatically detect these
resrouces.
cartermp marked this conversation as resolved.
Show resolved Hide resolved

The following example adds [Service][], [Host][] and [OS][] resource attributes
via environment variables, running unix programs like `uname` to generate the
resource data.

```console
env OTEL_RESOURCE_ATTRIBUTES="service.name=resource-tutorial-dotnet,service.namespace=tutorial,service.version=1.0,service.instance.id=`uuidgen`,host.name=`HOSTNAME`,host.type=`uname -m`,os.name=`uname -s`,os.version=`uname -r`" dotnet run

Activity.TraceId: d1cbb7787440cc95b325835cb2ff8018
Activity.SpanId: 2ca007300fcb3068
Activity.TraceFlags: Recorded
Activity.ActivitySourceName: tutorial-dotnet
Activity.DisplayName: SayHello
Activity.Kind: Internal
Activity.StartTime: 2022-10-02T13:31:12.0175090Z
Activity.Duration: 00:00:00.0003920
Activity.Tags:
foo: 1
bar: Hello, World!
baz: [1,2,3]
Resource associated with Activity:
service.name: resource-tutorial-dotnet
service.namespace: tutorial
service.version: 1.0
service.instance.id: 93B14BAD-813D-48EE-9FB1-2ADFD07C5E78
host.name: myhost
host.type: arm64
os.name: Darwin
os.version: 21.6.0
```

## Adding resources in code

You can also add custom resources in code by attaching them to a
`ResourceBuilder`.

The following example builds on the [getting started] sample and adds two custom
resources, `environment.name` and `team.name` in code:

```csharp
using System.Diagnostics;
using System.Collections.Generic;

using OpenTelemetry;
using OpenTelemetry.Trace;
using OpenTelemetry.Resources;

var resourceBuilder =
ResourceBuilder
.CreateDefault()
.AddAttributes(new Dictionary<string, object>
pellared marked this conversation as resolved.
Show resolved Hide resolved
{
["environment.name"] = "production",
["team.name"] = "backend"
});

var sourceName = "tutorial-dotnet";

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.SetResourceBuilder(resourceBuilder)
.AddConsoleExporter()
.Build();

var MyActivitySource = new ActivitySource(sourceName);

using var activity = MyActivitySource.StartActivity("SayHello");
cartermp marked this conversation as resolved.
Show resolved Hide resolved
activity?.SetTag("foo", 1);
activity?.SetTag("bar", "Hello, World!");
activity?.SetTag("baz", new int[] { 1, 2, 3 });
```

If you run the same command as in [Adding resources with environment
variables](#adding-resources-with-environment-variables), you'll see the
`environment.name` and `team.name` resources in the resource list:

```console
env OTEL_RESOURCE_ATTRIBUTES="service.name=resource-tutorial-dotnet,service.namespace=tutorial,service.version=1.0,service.instance.id=`uuidgen`,host.name=`HOSTNAME`,host.type=`uname -m`,os.name=`uname -s`,os.version=`uname -r`" dotnet run

Activity.TraceId: d1cbb7787440cc95b325835cb2ff8018
Activity.SpanId: 2ca007300fcb3068
Activity.TraceFlags: Recorded
Activity.ActivitySourceName: tutorial-dotnet
Activity.DisplayName: SayHello
Activity.Kind: Internal
Activity.StartTime: 2022-10-02T13:31:12.0175090Z
Activity.Duration: 00:00:00.0003920
Activity.Tags:
foo: 1
bar: Hello, World!
baz: [1,2,3]
Resource associated with Activity:
environment.name: production
team.name: backend
service.name: resource-tutorial-dotnet
service.namespace: tutorial
service.version: 1.0
service.instance.id: 28976A1C-BF02-43CA-BAE0-6E0564431462
host.name: pcarter
host.type: arm64
os.name: Darwin
os.version: 21.6.0
```

**Note**: If you set your resource attributes via environment variable and code,
any values set via the environment variable take precedence.
cartermp marked this conversation as resolved.
Show resolved Hide resolved

## Next steps

There are more resource detectors you can add to your configuration, for example
to get details about your [Cloud] environment or [Deployment][].

[resource]: /docs/reference/specification/resource/sdk/
[getting started]: /docs/instrumentation/net/getting-started/
[process and process runtime resources]:
/docs/reference/specification/resource/semantic_conventions/process/
[host]: /docs/reference/specification/resource/semantic_conventions/host/
[cloud]: /docs/reference/specification/resource/semantic_conventions/cloud/
[deployment]:
/docs/reference/specification/resource/semantic_conventions/deployment_environment/
[service]: /docs/reference/specification/resource/semantic_conventions/#service
[os]: /docs/reference/specification/resource/semantic_conventions/os/