Skip to content

Commit

Permalink
Merge pull request #643 from humivo/DotnetDocs
Browse files Browse the repository at this point in the history
Update .NET sample app and documentation for metrics
  • Loading branch information
humivo authored Sep 27, 2023
2 parents 76963a9 + 833fb15 commit 87a4c8f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/docs/getting-started/dotnet-sdk.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: 'Getting Started with the .NET SDK on Traces Instrumentation'
title: 'Getting Started with the .NET SDK on Traces and Metrics Instrumentation'
description:
OpenTelemetry provides different language SDKs to instrument code for collecting telemetry data in the application.
In this doc, we will introduce how to use OpenTelemetry .NET SDK for traces instrumentation in the application
In this doc, we will introduce how to use OpenTelemetry .NET SDK for traces and metrics instrumentation in the application
path: '/docs/getting-started/dotnet-sdk'
---

Expand All @@ -16,9 +16,9 @@ In this tutorial, we will introduce how to use OpenTelemetry .NET SDK for manual

## Getting Started

* [Manual Instrumentation on Traces with OpenTelemetry .NET SDK](/docs/getting-started/dotnet-sdk/trace-manual-instr)
* [Manual Instrumentation on Traces and Metrics with OpenTelemetry .NET SDK](/docs/getting-started/dotnet-sdk/manual-instr)


## Sample Code

* [AWS Distro for OpenTelemetry Sample Code with .NET SDK](https://github.com/aws-observability/aws-otel-dotnet/tree/main/integration-test-app)
* [AWS Distro for OpenTelemetry Sample Code with .NET SDK](https://github.com/aws-observability/aws-otel-community/tree/master/sample-apps/dotnet-sample-app)
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
---
title: 'Tracing with the AWS Distro for OpenTelemetry .NET SDK and X-Ray'
title: 'Manual Instrumentation for Traces and Metrics with the AWS Distro for OpenTelemetry .NET SDK'
description:
OpenTelemetry provides different language SDKs to instrument code for collecting telemetry data in the application.
In this tutorial, we will introduce how to use OpenTelemetry .NET SDK for traces instrumentation in the application.
path: '/docs/getting-started/dotnet-sdk/trace-manual-instr'
In this tutorial, we will introduce how to use OpenTelemetry .NET SDK for traces and metrics instrumentation in the application.
path: '/docs/getting-started/dotnet-sdk/manual-instr'
---

import SectionSeparator from "components/MdxSectionSeparator/sectionSeparator.jsx"
import SubSectionSeparator from "components/MdxSubSectionSeparator/subsectionSeparator.jsx"

The AWS Distro for OpenTelemetry .NET SDK contains an extension library for using OpenTelemetry with AWS X-Ray and for instrumenting the AWS SDK. In this tutorial, we will introduce how to manually instrument your application step-by-step using AWS Distro for OpenTelemetry .NET SDK.
The AWS Distro for OpenTelemetry .NET SDK contains an extension library for instrumenting the AWS SDK. In this tutorial, we will introduce how to manually instrument your application for traces and metrics step-by-step using AWS Distro for OpenTelemetry .NET SDK.

<SectionSeparator />

## Requirements

The AWS Distro for OpenTelemetry .NET SDK is compatible for all the officially supported versions of [.NET](https://dotnet.microsoft.com/en-us/download/dotnet) and [.NET Framework](https://dotnet.microsoft.com/en-us/download/dotnet-framework).

**Note**: You’ll also need to have the [AWS Distro for OpenTelemetry Collector](https://aws-otel.github.io/docs/getting-started/collector) running to export traces to X-Ray.
**Note**: You’ll also need to have the [AWS Distro for OpenTelemetry Collector](https://aws-otel.github.io/docs/getting-started/collector) running to export traces and metrics.

<SectionSeparator />

## Installation

In order to instrument your .NET application for tracing, start by downloading the `OpenTelemetry` nuget package to your application.
In order to instrument your .NET application for traces and metrics, start by downloading the `OpenTelemetry` nuget package to your application.

```shell
dotnet add package OpenTelemetry
Expand All @@ -40,7 +40,7 @@ If you plan to call another application instrumented with AWS X-Ray SDK, you’l
dotnet add package OpenTelemetry.Contrib.Extensions.AWSXRay
```

In order to export traces from your application to ADOT Collector, you need to install `OpenTelemetry.Exporter.OpenTelemetryProtocol`.
In order to export traces and metics from your application to ADOT Collector, you need to install `OpenTelemetry.Exporter.OpenTelemetryProtocol`.

```shell
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
Expand All @@ -50,22 +50,29 @@ By default the OpenTelemetry exporter sends data to an OpenTelemetry collector a

<SectionSeparator />

## Setting up the Global Tracer
## Setting up the Global Tracer and Meter

### Sending Traces to AWS X-Ray
### Sending Traces and Metrics

Configure AWS X-Ray ID generator, propagator and OpenTelemetry Protocol (OTLP) exporter globally in your application as follows. Make sure to call `AddXRayTraceId()` in the very **beginning** when creating `TracerProvider`
Also configure the meter provider and add a meter of your choice as well as the OpenTelemetry Protocol (OTLP) exporter.

```csharp
using OpenTelemetry;
using OpenTelemetry.Contrib.Extensions.AWSXRay.Trace;
using OpenTelemetry.Trace;

var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("ActivitySourceName")
.AddXRayTraceId() // for generating AWS X-Ray compliant trace IDs
.AddOtlpExporter() // default address localhost:4317
.Build();

var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("example_meter")
.AddOtlpExporter()
.Build();

Sdk.SetDefaultTextMapPropagator(new AWSXRayPropagator()); // configure AWS X-Ray propagator
```

Expand Down Expand Up @@ -152,8 +159,30 @@ Attributes are converted to metadata by default. If you configure your collector

For more information about the activity API, see the [OpenTelemetry .NET SDK's developer guide](https://github.com/open-telemetry/opentelemetry-dotnet/tree/main/src/OpenTelemetry.Api#instrumenting-a-libraryapplication-with-net-activity-api).

### Creating Metrics

Similarly to Traces, you can create custom metrics in your application using the OpenTelemetry API and SDK.

In the following example application we demonstrate how to use metric instruments to record metrics with a Counter.

```csharp
using System.Threading.Tasks;

Meter meter = new Meter("example_meter", "1.0");

totalTimeSentObserver = meter.CreateCounter<int>("time_alive",
"ms",
"Measures the total time the application has been alive");

while (true) {
var delayTask = Task.Delay(1000);
await Task.Run(() => totalTimeSentObserver.Add(1, new KeyValuePair<string, object>("attribute", "sample")));
await delayTask;
}
```

<SectionSeparator />

## Sample Application

Take a reference to the [sample application](https://github.com/aws-observability/aws-otel-dotnet/tree/main/integration-test-app) that is instrumented by ADOT and OpenTelemetry .NET SDK.
Take a reference to the [sample application](https://github.com/aws-observability/aws-otel-community/tree/master/sample-apps/dotnet-sample-app) that is instrumented by ADOT and OpenTelemetry .NET SDK.

0 comments on commit 87a4c8f

Please sign in to comment.