---
# System prepended metadata

title: OpenTelemetry - dotnet auto instrumentation
tags: [' docker', ' containerize', ' OpenTelemetry', ' dockerfile', .net]

---

---
tags: .net, OpenTelemetry, dockerfile, docker, containerize
---

# OpenTelemetry - dotnet auto instrumentation

* [Github opentelemetry-dotnet-instrumentation](https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation)

:::info
k8s 部署用的 helm 檔案可查閱 [我的 Github](https://github.com/YuChia-Wei/application-helm-template)
裡面有設定在部署時自動套入所需要的環境參數
:::

## dockerfile

```dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0-bullseye-slim AS build
ARG BuildConfiguration=Debug
WORKDIR /src
COPY ["Sample.Api/Sample.WebApi.csproj", "Sample.Api/"]
RUN dotnet restore "Sample.Api/Sample.WebApi.csproj"
COPY . .
WORKDIR "/src/Sample.Api"
RUN dotnet build "Sample.WebApi.csproj" -c $BuildConfiguration -o /app/build

FROM build AS publish
ARG BuildConfiguration=Debug
RUN dotnet publish "Sample.WebApi.csproj" -c $BuildConfiguration -o /app/publish

#因為公司環境下，docker builde 時無法使用 curl 下載該成品，故使用折衷的方式處理
FROM mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim AS otel
RUN apt-get update && apt-get install unzip -y
#opentelemetry-dotnet-instrumentation 的發布檔案
ARG ARCHIVE=opentelemetry-dotnet-instrumentation-linux-glibc.zip
#opentelemetry-dotnet-instrumentation 的發布版本
ARG OTEL_VERSION=0.5.0
ADD https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/releases/download/$OTEL_VERSION/$ARCHIVE otel-dotnet-instrumentation.zip
RUN unzip otel-dotnet-instrumentation.zip /otel-dotnet-auto

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY --from=otel /otel-dotnet-auto /otel-dotnet-auto
ENTRYPOINT ["dotnet", "Sample.WebApi.dll"]
```

## 執行時需要的環境參數

```
CORECLR_ENABLE_PROFILING: '1'
CORECLR_PROFILER: '{918728DD-259F-4A6A-AC2B-B85E1B658318}'
CORECLR_PROFILER_PATH: /otel-dotnet-auto/OpenTelemetry.AutoInstrumentation.Native.so
DOTNET_ADDITIONAL_DEPS: /otel-dotnet-auto/AdditionalDeps
DOTNET_SHARED_STORE: /otel-dotnet-auto/store
DOTNET_STARTUP_HOOKS: /otel-dotnet-auto/net/OpenTelemetry.AutoInstrumentation.StartupHook.dll
OTEL_DOTNET_AUTO_HOME: /otel-dotnet-auto
OTEL_DOTNET_AUTO_INTEGRATIONS_FILE: /otel-dotnet-auto/integrations.json
OTEL_DOTNET_AUTO_METRICS_ADDITIONAL_SOURCES: sample-api
OTEL_DOTNET_AUTO_TRACES_ADDITIONAL_SOURCES: sample-api
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://tempo-distributor.kube-monitor.svc.cluster.local:4317'
OTEL_EXPORTER_OTLP_PROTOCOL: grpc
OTEL_RESOURCE_ATTRIBUTES: "service.version=docker-image-name:imagetag, service.namespace=service-namespace"
OTEL_SERVICE_NAME: sample-api
```

## os type memo

:::warning
雖然 aspnet:7.0-alpine 在 opentelemetry-dotnet-instrumentation 的安裝用 SH 檔判斷下會使用 linux-musl 的版本，但是我實務使用時會出現執行錯誤的問題，但是使用 linux-glibc 版本就正常，所以我自己是統一使用 glibc 版本來發布 image
:::

| OS                     | Type        |
| ---------------------- | ----------- |
| alpine                 | linux-musl  |
| debian (bullseye-slim) | linux-glibc |
| ubuntu (jammy)         | linux-glibc |
```