Skip to content

Latest commit

 

History

History
359 lines (254 loc) · 18.2 KB

File metadata and controls

359 lines (254 loc) · 18.2 KB

OpenTelemetry .NET Automatic Instrumentation

SlackNuGetNuGet

This project adds OpenTelemetry instrumentation to .NET applications without having to modify their source code.


Warning

The following documentation refers to the in-development version of OpenTelemetry .NET Automatic Instrumentation. Docs for the latest version (1.11.0) can be found in opentelemetry.io or here.


Quick start

If you'd like to try the instrumentation on an existing application before learning more about the configuration options and the project, use the recommended installation method described at Using the OpenTelemetry.AutoInstrumentation NuGet packages or use the appropriate install script:

Note

The NuGet packages are the recommended way to deploy automatic instrumentation, but they can't be used in all cases. See Limitations for details.

To see the telemetry from your application directly on the standard output, set the following environment variables to console before launching your application:

  • OTEL_TRACES_EXPORTER
  • OTEL_METRICS_EXPORTER
  • OTEL_LOGS_EXPORTER

For a demo using docker compose, clone this repository and follow the examples/demo/README.md.

Components

OpenTelemetry .NET Automatic Instrumentation is built on top of OpenTelemetry .NET:

You can find all references in OpenTelemetry.AutoInstrumentation.csproj and OpenTelemetry.AutoInstrumentation.AdditionalDeps/Directory.Build.props.

To automatically instrument applications, the OpenTelemetry .NET Automatic Instrumentation does the following:

  1. Injects and configures the OpenTelemetry .NET SDK into the application.
  2. Adds OpenTelemetry Instrumentation to key packages and APIs used by the application.

You can enable the OpenTelemetry .NET Automatic Instrumentation as a .NET Profiler to inject additional instrumentations of this project at runtime, using a technique known as monkey-patching. When enabled, the OpenTelemetry .NET Automatic Instrumentation generates traces for libraries that don't already generate traces using the OpenTelemetry .NET SDK.

See design.md for an architectural overview.

Status

The versioning information and stability guarantees can be found in the versioning documentation.

Compatibility

OpenTelemetry .NET Automatic Instrumentation should work with all officially supported operating systems and versions of .NET.

The minimal supported version of .NET Framework is 4.6.2.

Supported processor architectures are:

CI tests run against the following operating systems:

Instrumented libraries and frameworks

See config.md#instrumented-libraries-and-frameworks.

Get started

Considerations on scope

Instrumenting self-contained applications is supported through NuGet packages. Note that a self-contained application is automatically generated in .NET 7+ whenever the dotnet publish or dotnet build command is used with a Runtime Identifier (RID) parameter, for example when -r or --runtime is used when running the command.

Install using NuGet packages

The NuGet packages are the recommended way to deploy automatic instrumentation, but they can't be used in all cases. To install using the NuGet packages, see Using the OpenTelemetry.AutoInstrumentation NuGet packages. See Limitations for incompatible scenarios.

Install manually

To install the automatic instrumentation manually, download and extract the appropriate binaries from the latest release.

Note

The path where you put the binaries is referenced as $INSTALL_DIR.

Instrument a .NET application

When running your application, make sure to:

  1. Set the resources.
  2. Set the environment variables from the table below.
Environment variable.NET versionValue
COR_ENABLE_PROFILING.NET Framework1
COR_PROFILER.NET Framework{918728DD-259F-4A6A-AC2B-B85E1B658318}
COR_PROFILER_PATH_32.NET Framework$INSTALL_DIR/win-x86/OpenTelemetry.AutoInstrumentation.Native.dll
COR_PROFILER_PATH_64.NET Framework$INSTALL_DIR/win-x64/OpenTelemetry.AutoInstrumentation.Native.dll
CORECLR_ENABLE_PROFILING.NET1
CORECLR_PROFILER.NET{918728DD-259F-4A6A-AC2B-B85E1B658318}
CORECLR_PROFILER_PATH.NET on Linux glibc$INSTALL_DIR/linux-x64/OpenTelemetry.AutoInstrumentation.Native.so
CORECLR_PROFILER_PATH.NET on Linux musl$INSTALL_DIR/linux-musl-x64/OpenTelemetry.AutoInstrumentation.Native.so
CORECLR_PROFILER_PATH.NET on macOS$INSTALL_DIR/osx-x64/OpenTelemetry.AutoInstrumentation.Native.dylib
CORECLR_PROFILER_PATH_32.NET on Windows$INSTALL_DIR/win-x86/OpenTelemetry.AutoInstrumentation.Native.dll
CORECLR_PROFILER_PATH_64.NET on Windows$INSTALL_DIR/win-x64/OpenTelemetry.AutoInstrumentation.Native.dll
DOTNET_ADDITIONAL_DEPS.NET$INSTALL_DIR/AdditionalDeps
DOTNET_SHARED_STORE.NET$INSTALL_DIR/store
DOTNET_STARTUP_HOOKS.NET$INSTALL_DIR/net/OpenTelemetry.AutoInstrumentation.StartupHook.dll
OTEL_DOTNET_AUTO_HOMEAll versions$INSTALL_DIR

Note

Some settings can be omitted on .NET. For more information, see config.md.

Important

Starting in .NET 8, the environment variable DOTNET_EnableDiagnostics=0 disables all diagnostics, including the CLR Profiler facility which is needed to launch the instrumentation, if not using .NET Startup hooks. Ensure that DOTNET_EnableDiagnostics=1, or if you'd like to limit diagnostics only to the CLR Profiler, you may set both DOTNET_EnableDiagnostics=1 and DOTNET_EnableDiagnostics_Profiler=1 while setting other diagnostics features to 0. See this issue for more guidance.

Shell scripts

You can install OpenTelemetry .NET Automatic Instrumentation and instrument your .NET application using the provided Shell scripts.

Note

On macOS coreutils is required.

Example usage:

# Download the bash script curl -sSfL https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/v1.11.0/otel-dotnet-auto-install.sh -O # Install core files sh ./otel-dotnet-auto-install.sh # Enable execution for the instrumentation script chmod +x $HOME/.otel-dotnet-auto/instrument.sh # Setup the instrumentation for the current shell session.$HOME/.otel-dotnet-auto/instrument.sh # Run your application with instrumentation OTEL_SERVICE_NAME=myapp OTEL_RESOURCE_ATTRIBUTES=deployment.environment=staging,service.version=1.0.0 ./MyNetApp

NOTE: for air-gapped environments you can provide either the installation archive directly with:

LOCAL_PATH=<PATH_TO_ARCHIVE> sh ./otel-dotnet-auto-install.sh

or the folder with the archives, this has the added benefit that the install script will determine the correct archive to choose.

DOWNLOAD_DIR=<PATH_TO_FOLDER_WITH_ARCHIVES> sh ./otel-dotnet-auto-install.sh

otel-dotnet-auto-install.sh script uses environment variables as parameters:

ParameterDescriptionRequiredDefault value
OTEL_DOTNET_AUTO_HOMELocation where binaries are to be installedNo$HOME/.otel-dotnet-auto
OS_TYPEPossible values: linux-glibc, linux-musl, macos, windowsNoCalculated
ARCHITECTUREPossible values for Linux: x64, arm64NoCalculated
TMPDIR(deprecated) prefer DOWNLOAD_DIRNo$(mktemp -d)
DOWNLOAD_DIRFolder to download the archive to. Will use local archive if it already existsNo$TMPDIR or $(mktemp -d)
LOCAL_PATHFull path the archive to use for installation. (ideal for air-gapped scenarios)NoCalculated
VERSIONVersion to downloadNo1.11.0

instrument.sh script uses environment variables as parameters:

ParameterDescriptionRequiredDefault value
ENABLE_PROFILINGWhether to set the .NET CLR Profiler, possible values: true, falseNotrue
OTEL_DOTNET_AUTO_HOMELocation where binaries are to be installedNo$HOME/.otel-dotnet-auto
OS_TYPEPossible values: linux-glibc, linux-musl, macos, windowsNoCalculated
ARCHITECTUREPossible values for Linux: x64, arm64NoCalculated

PowerShell module (Windows)

On Windows, you should install OpenTelemetry .NET Automatic Instrumentation and instrument your .NET application using the provided PowerShell module. Example usage (run as administrator):

# Download the module$module_url="https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/v1.11.0/OpenTelemetry.DotNet.Auto.psm1"$download_path=Join-Path$env:temp"OpenTelemetry.DotNet.Auto.psm1"Invoke-WebRequest-Uri $module_url-OutFile $download_path-UseBasicParsing # Import the module to use its functionsImport-Module$download_path# Install core files (online vs offline method)Install-OpenTelemetryCoreInstall-OpenTelemetryCore-LocalPath "C:\Path\To\OpenTelemetry.zip"# Set up the instrumentation for the current PowerShell sessionRegister-OpenTelemetryForCurrentSession-OTelServiceName "MyServiceDisplayName"# Run your application with instrumentation .\MyNetApp.exe

You can get usage information by calling:

# List all available commandsGet-Command-Module OpenTelemetry.DotNet.Auto # Get command's usage informationGet-HelpInstall-OpenTelemetryCore-Detailed

Updating OpenTelemetry installation:

# Import the previously downloaded module. After an update the module is found in the default install directory.# Note: It's best to use the same version of the module for installation and uninstallation to ensure proper removal.Import-Module"C:\Program Files\OpenTelemetry .NET AutoInstrumentation\OpenTelemetry.DotNet.Auto.psm1"# If IIS was previously registered, use RegisterIIS = $true.Update-OpenTelemetryCore-RegisterIIS $true# If Windows services were previously registered, these must be re-registered manually.Unregister-OpenTelemetryForWindowsService-WindowsServiceName MyServiceName Update-OpenTelemetryCoreRegister-OpenTelemetryForWindowsService-WindowsServiceName MyServiceName -OTelServiceName MyOtelServiceName

Warning

The PowerShell module works only on PowerShell 5.1 which is the one installed by default on Windows.

Instrument a container

You can find our demonstrative example that uses Docker Compose here.

You can also consider using the Kubernetes Operator for OpenTelemetry Collector.

Instrument a Windows Service running a .NET application

See windows-service-instrumentation.md.

Instrument an ASP.NET application deployed on IIS

See iis-instrumentation.md.

Configuration

See config.md.

Manual instrumentation

See manual-instrumentation.md.

Log to trace correlation

See log-trace-correlation.md.

Troubleshooting

See troubleshooting.md.

Contact

See CONTRIBUTING.md.

Contributing

See CONTRIBUTING.md.

Community Roles

Maintainers (@open-telemetry/dotnet-instrumentation-maintainers):

Approvers (@open-telemetry/dotnet-instrumentation-approvers):

Emeritus Maintainer/Approver/Triager:

Learn more about roles in the community repository.

close