I am trying to get open telemetry traces inside application insights. When I run code on windows(tried on multiple) everything works fine, but whenever code is run on linux (tried docker alpine and VM ubuntu) microservice is not starting normally. Sometimes it dies, sometimes after 6 minutes it starts as normal and sends logs. No logs are being displayed service just does nothing.
Do anybody has any ideas how to fix this to work inside linux docker?
Used framework version .NET7
Installed packages:
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.6" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="7.0.4" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
Code sample:
services.AddOpenTelemetry().UseAzureMonitor(options => {
options.ConnectionString = config.ApplicationInsights.ConnectionString;
options.SamplingRatio = 0.1F;
});
services.ConfigureOpenTelemetryTracerProvider((sp, builder) =>
builder.ConfigureResource(resourceBuilder =>
resourceBuilder.AddAttributes(new Dictionary<string, object> {
{ "Service", config.MicroserviceName }
}))
.AddNpgsql()
.SetSampler(new AlwaysOnSampler()));
Do anybody has any ideas how to fix this to work inside linux docker?
Here I tried to achieve the above requirement by using the below steps:
Created a sample dotnet application with microservice configuration.
Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Azure.Monitor.OpenTelemetry.Exporter;
using OpenTelemetry.Trace;
using Microsoft.Extensions.Logging;
namespace SampleDotNetApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Add Application Insights Telemetry
services.AddApplicationInsightsTelemetry();
// Add OpenTelemetry Tracing
services.AddOpenTelemetryTracing((sp, builder) =>
{
builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddNpgsqlInstrumentation()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(Configuration["MicroserviceName"]))
.AddAzureMonitorTraceExporter(config =>
{
config.ConnectionString = Configuration["ApplicationInsights:ConnectionString"];
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// Log an informational trace message
logger.LogInformation("This is an informational trace log.");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["SampleDotNetApp.csproj", "./"]
RUN dotnet restore "SampleDotNetApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "SampleDotNetApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SampleDotNetApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SampleDotNetApp.dll"]
docker-compose.yml: Provide your AppInsight connection string in the below Environment section.
version: '3.8'
services:
mymicroservice:
build:
context: .
dockerfile: Dockerfile
environment:
- ApplicationInsights__InstrumentationKey= your_InstrumentationKey
- ApplicationInsights__ConnectionString= your_ConnectionString
ports:
- "8080:80"
Configuring linux Docker: Solution<Add
Docker Status:
Running status:
Now, here Iam able to see the generated traces and logs that are sent to Azure Application Insights.