├── global.json ├── local ├── Dockerfile ├── NOTES.md ├── test.http ├── tye │ ├── .env │ ├── tye-container.yaml │ ├── tye-mixing.yaml │ └── tye.yaml ├── infra │ ├── compose.tracing.yaml │ ├── config-files │ │ ├── prometheus.yaml │ │ ├── grafana-datasources.yaml │ │ └── otel-collector.yaml │ ├── compose.collector.yaml │ └── docker-compose.observability.yaml └── build.cake ├── http-requests └── client-simulator │ └── hello.http ├── doc-images ├── grafana-loki.png ├── grafana-jaeger.png ├── grafana-zipkin.png ├── grafana-prometheus.png └── visualstudio-run-http-request.png ├── src ├── ClientApps │ └── ClientApps.ClientSimulator │ │ ├── Requests │ │ └── ClientApps.ClientSimulator.http │ │ ├── appsettings.Development.json │ │ ├── Properties │ │ └── launchSettings.json │ │ ├── GrpcServices │ │ ├── WeatherProxyServices │ │ │ └── WeatherGreetingGrpcService.cs │ │ ├── GrpcServiceOptions.cs │ │ └── GrpcServicesRegistration.cs │ │ ├── ClientApps.ClientSimulator.csproj │ │ ├── appsettings.json │ │ └── Program.cs ├── Microservices │ └── Microservices.WeatherService │ │ ├── appsettings.Development.json │ │ ├── GrpcServices │ │ ├── GreetingService │ │ │ └── GreetingGrpcService.cs │ │ └── GrpcServicesRegistration.cs │ │ ├── appsettings.json │ │ ├── Properties │ │ └── launchSettings.json │ │ ├── Microservices.WeatherService.csproj │ │ └── Program.cs ├── __grpc │ └── weather-service │ │ └── greet.proto └── BuildingBlocks │ └── BuildingBlocks.Observability │ ├── Options │ └── ObservabilityOptions.cs │ ├── BuildingBlocks.Observability.csproj │ ├── Middlewares │ └── TraceIdResponseHeaderMiddleware.cs │ └── ObservabilityRegistration.cs ├── .dockerignore ├── .justfile ├── Directory.Build.props ├── Directory.Packages.props ├── README.md ├── DNSLN.sln └── .gitignore /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "8.0.303" 4 | } 5 | } -------------------------------------------------------------------------------- /local/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/dotnet/aspnet:7.0.5-alpine3.17-amd64 2 | 3 | RUN apk add libgdiplus -------------------------------------------------------------------------------- /http-requests/client-simulator/hello.http: -------------------------------------------------------------------------------- 1 | 2 | 3 | @url = http://localhost:6002 4 | 5 | ### 6 | GET {{url}}/hello -------------------------------------------------------------------------------- /doc-images/grafana-loki.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimcuhoang/practical-net-otelcollector/HEAD/doc-images/grafana-loki.png -------------------------------------------------------------------------------- /doc-images/grafana-jaeger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimcuhoang/practical-net-otelcollector/HEAD/doc-images/grafana-jaeger.png -------------------------------------------------------------------------------- /doc-images/grafana-zipkin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimcuhoang/practical-net-otelcollector/HEAD/doc-images/grafana-zipkin.png -------------------------------------------------------------------------------- /doc-images/grafana-prometheus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimcuhoang/practical-net-otelcollector/HEAD/doc-images/grafana-prometheus.png -------------------------------------------------------------------------------- /doc-images/visualstudio-run-http-request.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kimcuhoang/practical-net-otelcollector/HEAD/doc-images/visualstudio-run-http-request.png -------------------------------------------------------------------------------- /local/NOTES.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | https://blog.knoldus.com/docker-manifest-a-peek-into-images-manifest-json-files/ 4 | 5 | https://www.aspecto.io/blog/opentelemetry-collector-guide/ -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/Requests/ClientApps.ClientSimulator.http: -------------------------------------------------------------------------------- 1 | @ClientSimulator_HostAddress = http://localhost:6002 2 | 3 | 4 | GET {{ClientSimulator_HostAddress}}/hello 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /local/test.http: -------------------------------------------------------------------------------- 1 | 2 | ### Retrieve all repositories 3 | 4 | GET http://localhost:5010/v2/_catalog 5 | 6 | 7 | ### Manifests specific image by name & tag 8 | 9 | GET http://localhost:5010/v2/net7-alpine-base/manifests/latest -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /local/tye/.env: -------------------------------------------------------------------------------- 1 | 2 | # Observability 3 | 4 | ObservabilityOptions__CollectorUrl=http://localhost:4317 5 | ObservabilityOptions__Serilog__MinimumLevel__Default=Information 6 | 7 | # GrpcServiceOptions__WeatherService__Name=tye-weather -------------------------------------------------------------------------------- /local/infra/compose.tracing.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | jaeger: 3 | image: jaegertracing/all-in-one:latest 4 | ports: 5 | - "16686:16686" # Jaeger UI 6 | networks: 7 | - practical-otel-net 8 | 9 | zipkin: 10 | image: openzipkin/zipkin:latest 11 | ports: 12 | - "9412:9411" 13 | networks: 14 | - practical-otel-net -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | **/.dockerignore 2 | **/.env 3 | **/.git 4 | **/.gitignore 5 | **/.vs 6 | **/.vscode 7 | **/*.*proj.user 8 | **/azds.yaml 9 | **/charts 10 | **/bin 11 | **/obj 12 | **/Dockerfile 13 | **/Dockerfile.develop 14 | **/docker-compose.yml 15 | **/docker-compose.*.yml 16 | **/*.dbmdl 17 | **/*.jfm 18 | **/secrets.dev.yaml 19 | **/values.dev.yaml 20 | **/.toolstarget 21 | **/node_modules/ 22 | setup-secret.sh 23 | -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/GrpcServices/GreetingService/GreetingGrpcService.cs: -------------------------------------------------------------------------------- 1 | using Grpc.Core; 2 | 3 | namespace Microservices.WeatherService.GrpcServices.GreetingService; 4 | 5 | public class GreetingGrpcService : Greeter.GreeterBase 6 | { 7 | public override Task SayHello(HelloRequest request, ServerCallContext context) 8 | => Task.FromResult(new HelloReply 9 | { 10 | Message = $"WeatherService response to {request.Name}" 11 | }); 12 | } 13 | -------------------------------------------------------------------------------- /local/infra/config-files/prometheus.yaml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s 3 | scrape_timeout: 10s 4 | evaluation_interval: 1m 5 | 6 | scrape_configs: 7 | - job_name: prometheus 8 | honor_timestamps: true 9 | scrape_interval: 1s 10 | scrape_timeout: 1s 11 | metrics_path: /metrics 12 | scheme: http 13 | follow_redirects: true 14 | static_configs: 15 | - targets: 16 | - collector:8889 17 | - collector:8888 18 | labels: 19 | group: otel-collector 20 | -------------------------------------------------------------------------------- /src/__grpc/weather-service/greet.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | option csharp_namespace = "Microservices.WeatherService"; 4 | 5 | package greet; 6 | 7 | // The greeting service definition. 8 | service Greeter { 9 | // Sends a greeting 10 | rpc SayHello (HelloRequest) returns (HelloReply); 11 | } 12 | 13 | // The request message containing the user's name. 14 | message HelloRequest { 15 | string name = 1; 16 | } 17 | 18 | // The response message containing the greetings. 19 | message HelloReply { 20 | string message = 1; 21 | } 22 | -------------------------------------------------------------------------------- /src/BuildingBlocks/BuildingBlocks.Observability/Options/ObservabilityOptions.cs: -------------------------------------------------------------------------------- 1 | namespace BuildingBlocks.Observability.Options; 2 | 3 | public class ObservabilityOptions 4 | { 5 | public string ServiceName { get; set; } = default!; 6 | public string CollectorUrl { get; set; } = @"http://localhost:4317"; 7 | 8 | public bool EnabledTracing { get; set; } = false; 9 | public bool EnabledMetrics { get; set; } = false; 10 | 11 | public Uri CollectorUri => new(this.CollectorUrl); 12 | 13 | public string OtlpLogsCollectorUrl => $"{this.CollectorUrl}/v1/logs"; 14 | } 15 | -------------------------------------------------------------------------------- /.justfile: -------------------------------------------------------------------------------- 1 | # use PowerShell instead of sh: 2 | set shell := ["powershell.exe", "-c"] 3 | 4 | hello: 5 | Write-Host "Hello, world!" 6 | 7 | start-infra: 8 | docker compose -f ./local/infra/docker-compose.observability.yaml up -d 9 | 10 | stop-infra: 11 | docker compose -f ./local/infra/docker-compose.observability.yaml down -v 12 | 13 | start-weather: 14 | clear;dotnet run --project ./src/Microservices/Microservices.WeatherService/Microservices.WeatherService.csproj 15 | 16 | start-client: 17 | clear;dotnet run --project src/ClientApps/ClientApps.ClientSimulator/ClientApps.ClientSimulator.csproj -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | CS8618 8 | 9 | true 10 | 11 | true 12 | DefaultContainer 13 | mcr.microsoft.com/dotnet/aspnet:8.0 14 | latest 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ObservabilityOptions": { 3 | "ServiceName": "Weather", 4 | "EnabledTracing": true, 5 | "EnabledMetrics": true, 6 | "CollectorUrl": "http://localhost:4317", 7 | "Serilog": { 8 | "MinimumLevel": { 9 | "Default": "Information", 10 | "Override": { 11 | "Microsoft": "Warning", 12 | "System": "Warning", 13 | "Microsoft.Hosting.Lifetime": "Information", 14 | "Microsoft.EntityFrameworkCore": "Error", 15 | "Microsoft.EntityFrameworkCore.Database.Command": "Information", 16 | "Grpc": "Error" 17 | } 18 | } 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "http": { 4 | "commandName": "Project", 5 | "dotnetRunMessages": true, 6 | "launchBrowser": false, 7 | "applicationUrl": "http://localhost:5195", 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | }, 12 | "https": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": false, 16 | "applicationUrl": "https://localhost:7029;http://localhost:5195", 17 | "environmentVariables": { 18 | "ASPNETCORE_ENVIRONMENT": "Development" 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "http": { 4 | "commandName": "Project", 5 | "dotnetRunMessages": true, 6 | "launchBrowser": false, 7 | "applicationUrl": "http://localhost:5249", 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | }, 12 | "https": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": false, 16 | "applicationUrl": "https://localhost:7264;http://localhost:5249", 17 | "environmentVariables": { 18 | "ASPNETCORE_ENVIRONMENT": "Development" 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/BuildingBlocks/BuildingBlocks.Observability/BuildingBlocks.Observability.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/GrpcServices/GrpcServicesRegistration.cs: -------------------------------------------------------------------------------- 1 | 2 | using Microservices.WeatherService.GrpcServices.GreetingService; 3 | 4 | namespace Microservices.WeatherService.GrpcServices; 5 | 6 | public static class GrpcServicesRegistration 7 | { 8 | public static WebApplicationBuilder AddGrpcServices(this WebApplicationBuilder builder) 9 | { 10 | builder.Services.AddGrpc(options => 11 | { 12 | options.EnableDetailedErrors = true; 13 | }); 14 | 15 | return builder; 16 | } 17 | 18 | public static WebApplication MapGrpcServices(this WebApplication webApplication) 19 | { 20 | webApplication.MapGrpcService(); 21 | 22 | return webApplication; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/GrpcServices/WeatherProxyServices/WeatherGreetingGrpcService.cs: -------------------------------------------------------------------------------- 1 | using Microservices.WeatherService; 2 | 3 | namespace ClientApps.SimulateClientApp.GrpcServices.WeatherProxyServices; 4 | 5 | public interface IWeatherGreetingGrpcService 6 | { 7 | Task SayHello(HelloRequest request, CancellationToken cancellationToken = default); 8 | } 9 | 10 | public class WeatherGreetingGrpcService(Greeter.GreeterClient client) : IWeatherGreetingGrpcService 11 | { 12 | private readonly Greeter.GreeterClient _client = client; 13 | 14 | public async Task SayHello(HelloRequest request, CancellationToken cancellationToken = default) 15 | { 16 | return await this._client.SayHelloAsync(request, cancellationToken: cancellationToken); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/Microservices.WeatherService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | weather-service 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/ClientApps.ClientSimulator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | weather-client 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ObservabilityOptions": { 3 | "ServiceName": "Weather-Client", 4 | "EnabledTracing": true, 5 | "EnabledMetrics": true, 6 | "CollectorUrl": "http://localhost:4317", 7 | "Serilog": { 8 | "MinimumLevel": { 9 | "Default": "Information", 10 | "Override": { 11 | "Microsoft": "Warning", 12 | "System": "Warning", 13 | "Microsoft.Hosting.Lifetime": "Information", 14 | "Microsoft.EntityFrameworkCore": "Error", 15 | "Microsoft.EntityFrameworkCore.Database.Command": "Information", 16 | "Grpc": "Error" 17 | } 18 | } 19 | } 20 | }, 21 | "GrpcServiceOptions": { 22 | "WeatherService": { 23 | "Name": "Weather", 24 | "ServiceUrl": "http://localhost:6001", 25 | "ServiceGrpcUrl": "http://localhost:16001" 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Directory.Packages.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /local/infra/compose.collector.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | collector: 3 | image: otel/opentelemetry-collector-contrib:latest 4 | command: [ "--config=/etc/otel-collector.yaml" ] 5 | volumes: 6 | - ./config-files/otel-collector.yaml:/etc/otel-collector.yaml 7 | ports: 8 | - "4317:4317" # OTLP over gRPC receiver 9 | - "4318:4318" # OTLP over HTTP receiver 10 | # - "9464" # Prometheus exporter 11 | # - "8888" # metrics endpoint 12 | # - 1888:1888 # pprof extension 13 | # - 13133:13133 # health_check extension 14 | # - 55679:55679 # zpages extension 15 | # - 8888:8888 # Prometheus metrics exposed by the collector 16 | # - 8889:8889 # Prometheus exporter metrics 17 | # - 4317:4317 # OTLP gRPC receiver 18 | # - 4318:4318 # OTLP http receiver 19 | depends_on: 20 | - jaeger 21 | - zipkin 22 | - prometheus 23 | - loki 24 | networks: 25 | - practical-otel-net -------------------------------------------------------------------------------- /local/tye/tye-container.yaml: -------------------------------------------------------------------------------- 1 | # tye application configuration file 2 | # read all about it at https://github.com/dotnet/tye 3 | # 4 | # when you've given us a try, we'd love to know what you think: 5 | # https://aka.ms/AA7q20u 6 | # 7 | 8 | name: practical-otel-net 9 | 10 | network: practical-otel-net 11 | 12 | services: 13 | - name: tye-weather-container 14 | image: weather-service:latest 15 | bindings: 16 | - name: http 17 | protocol: http 18 | port: 6001 19 | containerPort: 6001 20 | - name: grpc 21 | protocol: http 22 | port: 16001 23 | env: 24 | - ObservabilityOptions__CollectorUrl=http://collector:4317 25 | 26 | - name: tye-client-simulator-container 27 | image: weather-client:latest 28 | bindings: 29 | - name: http 30 | protocol: http 31 | port: 6002 32 | containerPort: 6002 33 | - name: grpc 34 | protocol: http 35 | port: 16002 36 | env: 37 | - GrpcServiceOptions__WeatherService__Name=tye-weather-container 38 | - ObservabilityOptions__CollectorUrl=http://collector:4317 39 | 40 | 41 | -------------------------------------------------------------------------------- /local/tye/tye-mixing.yaml: -------------------------------------------------------------------------------- 1 | # tye application configuration file 2 | # read all about it at https://github.com/dotnet/tye 3 | # 4 | # when you've given us a try, we'd love to know what you think: 5 | # https://aka.ms/AA7q20u 6 | # 7 | 8 | name: practical-otel-net 9 | 10 | network: practical-otel-net 11 | 12 | services: 13 | - name: tye-weather 14 | project: ../../src/Microservices/Microservices.WeatherService/Microservices.WeatherService.csproj 15 | bindings: 16 | - name: http 17 | protocol: http 18 | port: 6001 19 | - name: grpc 20 | protocol: http 21 | port: 16001 22 | env_file: 23 | - ./.env 24 | 25 | - name: tye-client-simulator-container 26 | image: weather-client:latest 27 | bindings: 28 | - name: http 29 | protocol: http 30 | port: 6002 31 | containerPort: 6002 32 | - name: grpc 33 | protocol: http 34 | port: 16002 35 | env_file: 36 | - ./.env 37 | env: 38 | - GrpcServiceOptions__WeatherService__Name=tye-weather 39 | - ObservabilityOptions__CollectorUrl=http://collector:4317 40 | 41 | -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/GrpcServices/GrpcServiceOptions.cs: -------------------------------------------------------------------------------- 1 | namespace ClientApps.ClientSimulator.GrpcServices; 2 | 3 | public class GrpcServiceOptions 4 | { 5 | public GrpcServiceDependency WeatherService { get; set; } 6 | 7 | public class GrpcServiceDependency 8 | { 9 | public string Name { get; set; } 10 | public string ServiceUrl { get; set; } 11 | public string ServiceGrpcUrl { get; set; } 12 | 13 | public void AsGrpcClient(IServiceCollection services) where TClient: class 14 | { 15 | services.AddGrpcClient((serviceProvider, client) => 16 | { 17 | var uri = new Uri(this.ServiceGrpcUrl); 18 | 19 | var loggerFactory = serviceProvider.GetRequiredService(); 20 | var logger = loggerFactory.CreateLogger(); 21 | logger.LogInformation($"{this}: {uri}"); 22 | 23 | client.Address = uri; 24 | }); 25 | } 26 | 27 | public override string ToString() => $"{this.Name}-{this.ServiceGrpcUrl}"; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /local/infra/config-files/grafana-datasources.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: 1 2 | 3 | datasources: 4 | - name: Prometheus 5 | type: prometheus 6 | access: proxy 7 | orgId: 1 8 | url: http://prometheus:8080 9 | basicAuth: false 10 | isDefault: false 11 | version: 1 12 | editable: false 13 | jsonData: 14 | tlsSkipVerify: true 15 | 16 | - name: Zipkin 17 | type: zipkin 18 | url: http://zipkin:9411 19 | access: proxy 20 | orgId: 1 21 | version: 1 22 | apiVersion: 1 23 | isDefault: false 24 | 25 | - name: Jaeger 26 | type: jaeger 27 | url: http://jaeger:16686 28 | access: proxy 29 | orgId: 1 30 | version: 1 31 | apiVersion: 1 32 | isDefault: false 33 | 34 | - name: Loki 35 | type: loki 36 | access: proxy 37 | orgId: 1 38 | url: http://loki:3100 39 | basicAuth: false 40 | isDefault: true 41 | version: 1 42 | editable: false 43 | apiVersion: 1 44 | jsonData: 45 | derivedFields: 46 | - datasourceUid: otlp 47 | matcherRegex: (?:"traceid"):"(\w+)" 48 | name: TraceID 49 | url: $${__value.raw} -------------------------------------------------------------------------------- /local/tye/tye.yaml: -------------------------------------------------------------------------------- 1 | # tye application configuration file 2 | # read all about it at https://github.com/dotnet/tye 3 | # 4 | # when you've given us a try, we'd love to know what you think: 5 | # https://aka.ms/AA7q20u 6 | # 7 | 8 | name: practical-otel-net 9 | 10 | network: practical-otel-net 11 | 12 | services: 13 | - name: tye-weather 14 | project: ../../src/Microservices/Microservices.WeatherService/Microservices.WeatherService.csproj 15 | bindings: 16 | - name: http 17 | protocol: http 18 | port: 6001 19 | - name: grpc 20 | protocol: http 21 | port: 16001 22 | env_file: 23 | - ./.env 24 | 25 | - name: tye-client-simulator 26 | project: ../../src/ClientApps/ClientApps.ClientSimulator/ClientApps.ClientSimulator.csproj 27 | bindings: 28 | - name: http 29 | protocol: http 30 | port: 6002 31 | - name: grpc 32 | protocol: http 33 | port: 16002 34 | env: 35 | - GrpcServiceOptions__WeatherService__Name=tye-weather 36 | - GrpcServiceOptions__WeatherService__ServiceGrpcUrl=http://localhost:16001 37 | env_file: 38 | - ./.env 39 | 40 | -------------------------------------------------------------------------------- /src/BuildingBlocks/BuildingBlocks.Observability/Middlewares/TraceIdResponseHeaderMiddleware.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Http; 3 | using Serilog.Context; 4 | using System.Diagnostics; 5 | 6 | namespace BuildingBlocks.Observability.Middlewares; 7 | 8 | public class TraceIdResponseHeaderMiddleware(RequestDelegate next) 9 | { 10 | private readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next)); 11 | 12 | [DebuggerStepThrough] 13 | public async Task Invoke(HttpContext context) 14 | { 15 | var traceId = Activity.Current!.TraceId.ToString(); 16 | 17 | using (LogContext.PushProperty("TraceId", traceId.ToString())) 18 | { 19 | context.Response.Headers.Append("TraceId", traceId); 20 | 21 | await _next(context); 22 | } 23 | } 24 | } 25 | 26 | public static class TraceIdResponseHeaderMiddlewareResgistration 27 | { 28 | public static WebApplication UseTraceIdResponseHeader(this WebApplication builder) 29 | { 30 | builder.UseMiddleware(); 31 | 32 | return builder; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/GrpcServices/GrpcServicesRegistration.cs: -------------------------------------------------------------------------------- 1 | using ClientApps.ClientSimulator.GrpcServices; 2 | using ClientApps.SimulateClientApp.GrpcServices.WeatherProxyServices; 3 | using Microservices.WeatherService; 4 | 5 | namespace ClientApps.SimulateClientApp.GrpcServices; 6 | 7 | public static class GrpcServicesRegistration 8 | { 9 | public static WebApplicationBuilder AddGrpcServices(this WebApplicationBuilder builder) 10 | { 11 | builder.Services.AddGrpc(); 12 | 13 | var configuration = builder.Configuration; 14 | var grpcServiceOptions = new GrpcServiceOptions(); 15 | 16 | configuration 17 | .GetRequiredSection(nameof(GrpcServiceOptions)) 18 | .Bind(grpcServiceOptions); 19 | 20 | grpcServiceOptions 21 | .WeatherService.AsGrpcClient(builder.Services); 22 | 23 | builder.Services 24 | .AddScoped(); 25 | 26 | return builder; 27 | } 28 | 29 | public static WebApplication MapGrpcServices(this WebApplication webApplication) 30 | { 31 | return webApplication; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /local/infra/docker-compose.observability.yaml: -------------------------------------------------------------------------------- 1 | include: 2 | - compose.collector.yaml 3 | - compose.tracing.yaml 4 | 5 | services: 6 | 7 | prometheus: 8 | image: prom/prometheus:latest 9 | volumes: 10 | - ./config-files/prometheus.yaml:/etc/prometheus/prometheus.yml 11 | command: 12 | - '--config.file=/etc/prometheus/prometheus.yml' 13 | - '--web.listen-address=:8080' 14 | ports: 15 | - "8081:8080" 16 | environment: 17 | - config.file=/etc/prometheus/prometheus.yml 18 | networks: 19 | - practical-otel-net 20 | 21 | loki: 22 | image: grafana/loki:latest 23 | command: [ "-config.file=/etc/loki/local-config.yaml" ] 24 | networks: 25 | - practical-otel-net 26 | 27 | grafana: 28 | image: grafana/grafana:latest 29 | ports: 30 | - "3000:3000" 31 | volumes: 32 | - ./config-files/grafana-datasources.yaml:/etc/grafana/provisioning/datasources/datasources.yaml 33 | environment: 34 | - GF_AUTH_ANONYMOUS_ENABLED=true 35 | - GF_AUTH_ANONYMOUS_ORG_ROLE=Admin 36 | - GF_AUTH_DISABLE_LOGIN_FORM=true 37 | depends_on: 38 | - jaeger 39 | - zipkin 40 | - prometheus 41 | - loki 42 | - collector 43 | networks: 44 | - practical-otel-net 45 | 46 | networks: 47 | practical-otel-net: 48 | name: practical-otel-net 49 | -------------------------------------------------------------------------------- /local/infra/config-files/otel-collector.yaml: -------------------------------------------------------------------------------- 1 | receivers: 2 | otlp: 3 | protocols: 4 | http: 5 | endpoint: 0.0.0.0:4318 6 | grpc: 7 | endpoint: 0.0.0.0:4317 8 | 9 | processors: 10 | batch: 11 | timeout: 1s 12 | 13 | resource: 14 | attributes: 15 | - action: insert 16 | key: loki.resource.labels 17 | value: service.name, service.namespace 18 | - action: insert 19 | key: loki.format 20 | value: json 21 | 22 | exporters: 23 | debug: 24 | verbosity: normal 25 | 26 | prometheus: 27 | endpoint: 0.0.0.0:8889 28 | namespace: test-space 29 | resource_to_telemetry_conversion: 30 | enabled: true 31 | enable_open_metrics: true 32 | 33 | otlp/jaeger: 34 | endpoint: jaeger:4317 35 | tls: 36 | insecure: true 37 | 38 | zipkin: 39 | endpoint: "http://zipkin:9411/api/v2/spans" 40 | format: proto 41 | 42 | loki: 43 | endpoint: http://loki:3100/loki/api/v1/push 44 | default_labels_enabled: 45 | exporter: false 46 | job: true 47 | 48 | 49 | extensions: 50 | health_check: 51 | pprof: 52 | endpoint: :1888 53 | zpages: 54 | endpoint: :55679 55 | 56 | service: 57 | extensions: [pprof, zpages, health_check] 58 | pipelines: 59 | traces: 60 | receivers: [otlp] 61 | processors: [batch, resource] 62 | exporters: [debug, otlp/jaeger, zipkin] 63 | 64 | metrics: 65 | receivers: [otlp] 66 | processors: [batch] 67 | exporters: [debug, prometheus] 68 | 69 | logs: 70 | receivers: [otlp] 71 | processors: [batch] 72 | exporters: [debug, loki] 73 | 74 | -------------------------------------------------------------------------------- /src/Microservices/Microservices.WeatherService/Program.cs: -------------------------------------------------------------------------------- 1 | using BuildingBlocks.Observability; 2 | using BuildingBlocks.Observability.Middlewares; 3 | using Microservices.WeatherService.GrpcServices; 4 | using Microsoft.AspNetCore.Server.Kestrel.Core; 5 | using Serilog; 6 | 7 | var webApplicationBuilder = WebApplication.CreateBuilder(args); 8 | 9 | var environment = webApplicationBuilder.Environment; 10 | var configuration = webApplicationBuilder.Configuration; 11 | 12 | webApplicationBuilder.Configuration 13 | .SetBasePath(environment.ContentRootPath) 14 | .AddJsonFile("appsettings.json", false, true) 15 | .AddEnvironmentVariables() 16 | .AddCommandLine(args); 17 | 18 | webApplicationBuilder 19 | .AddObservability() 20 | .AddGrpcServices(); 21 | 22 | webApplicationBuilder.WebHost 23 | .CaptureStartupErrors(true) 24 | .UseKestrel(options => 25 | { 26 | options.AddServerHeader = false; 27 | 28 | // We have to separate into different ports because we don't use TLS 29 | options.ListenAnyIP(6001, listeningOptions => 30 | { 31 | listeningOptions.Protocols = HttpProtocols.Http1; 32 | }); 33 | options.ListenAnyIP(16001, listeningOptions => 34 | { 35 | listeningOptions.Protocols = HttpProtocols.Http2; 36 | }); 37 | }); 38 | 39 | 40 | using var app = webApplicationBuilder.Build(); 41 | 42 | app.UseSerilogRequestLogging(); 43 | 44 | app 45 | .UseTraceIdResponseHeader() 46 | .MapGrpcServices(); 47 | 48 | app.MapGet("/", () => "WeatherService"); 49 | 50 | await app.RunAsync(); 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # .NET with OpenTelemetry Collector 2 | 3 | ## Overview 4 | 5 | - Use [OpenTelemetry](https://opentelemetry.io) to collect traces and metrics and [Serilog](https://serilog.net) to collect log's messages; then all of them will only export to [OpenTelemetry Collector](https://opentelemetry.io/docs/collector/) (OTEL collector in short). 6 | 7 | - The collector then exports to 8 | - [Zipkin](https://zipkin.io/) and/or [Jaeger](https://www.jaegertracing.io/) for tracings 9 | - [Prometheus](https://prometheus.io/) for metrics 10 | - [Loki](https://github.com/grafana/loki) for log's messages 11 | 12 | - All of them are visualized on [Grafana](https://grafana.com/) 13 | 14 | ## Quick Start 15 | 16 | 1. Starting infrastructure from docker compose 17 | 18 | ```bash 19 | docker compose -f ./local/infra/docker-compose.observability.yaml up -d 20 | ``` 21 | 22 | 2. Starting `weather-service` 23 | 24 | ```bash 25 | dotnet run --project ./src/Microservices/Microservices.WeatherService/Microservices.WeatherService.csproj 26 | ``` 27 | 28 | 3. Starting `client-service` 29 | 30 | ```bash 31 | dotnet run --project src/ClientApps/ClientApps.ClientSimulator/ClientApps.ClientSimulator.csproj 32 | ``` 33 | 34 | 3. Observe 35 | 36 | - Making some requests to `http://localhost:6002/hello` or execute via Visual Studio 37 | 38 | ![Execute http via Visual Studio](./doc-images/visualstudio-run-http-request.png) 39 | 40 | - Access `grafana` at `http://localhost:3000` to explore 4 datasources: Jaeger, Zipkin, Prometheus & Loki 41 | 42 | ![Grafana Jaeger](./doc-images/grafana-jaeger.png) 43 | 44 | ![Grafana Zipkin](./doc-images/grafana-zipkin.png) 45 | 46 | ![Grafana Prometheus](./doc-images/grafana-prometheus.png) 47 | 48 | ![Grafana Loki](./doc-images/grafana-loki.png) 49 | 50 | 51 | ## Development 52 | 53 | ### Register & configure 54 | 55 | - Refer to [ObservabilityRegistration.cs](./src/BuildingBlocks/BuildingBlocks.Observability/ObservabilityRegistration.cs) 56 | 57 | - There are the following methods that named as its functionality 58 | - `AddTracing` 59 | - `AddMetrics` 60 | - `AddSerilog` 61 | 62 | - There is only one `public` method which is `AddObservability`. This is used in [Program.cs](./src/Microservices/Microservices.WeatherService/Program.cs) 63 | 64 | ```csharp 65 | 66 | var webApplicationBuilder = WebApplication.CreateBuilder(args); 67 | 68 | webApplicationBuilder.AddObservability(); 69 | 70 | // Other lines of code 71 | 72 | ``` 73 | 74 | --- 75 | 76 | ## Give a Star! :star2: 77 | 78 | If you liked this project or if it helped you, please give a star :star: for this repository. Thank you!!! 79 | 80 | --- 81 | 82 | ## Resources 83 | 84 | - [Automatic Instrumentation of Containerized .NET Applications With OpenTelemetry](https://www.twilio.com/blog/automatic-instrumentation-of-containerized-dotnet-applications-with-opentelemetry) 85 | 86 | - [Containerize a .NET app with dotnet publish](https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container) 87 | 88 | - [Built-In Container Support for .NET 7 – Dockerize .NET Applications without Dockerfile!](https://codewithmukesh.com/blog/built-in-container-support-for-dotnet-7/) 89 | 90 | - [Install the .NET SDK or the .NET Runtime on Alpine](https://learn.microsoft.com/en-us/dotnet/core/install/linux-alpine) -------------------------------------------------------------------------------- /src/ClientApps/ClientApps.ClientSimulator/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Server.Kestrel.Core; 2 | using BuildingBlocks.Observability; 3 | using ClientApps.SimulateClientApp.GrpcServices; 4 | using ClientApps.SimulateClientApp.GrpcServices.WeatherProxyServices; 5 | using Serilog; 6 | 7 | var webApplicationBuilder = WebApplication.CreateBuilder(args); 8 | 9 | var environment = webApplicationBuilder.Environment; 10 | var configuration = webApplicationBuilder.Configuration; 11 | 12 | webApplicationBuilder.Configuration 13 | .SetBasePath(environment.ContentRootPath) 14 | .AddJsonFile("appsettings.json", false, true) 15 | .AddEnvironmentVariables() 16 | .AddCommandLine(args); 17 | 18 | webApplicationBuilder 19 | .AddObservability() 20 | .AddGrpcServices(); 21 | 22 | webApplicationBuilder.Services 23 | // https://stackoverflow.com/a/71933535 24 | // TLDR: AddEndpointsApiExplorer for minimal API 25 | .AddEndpointsApiExplorer() 26 | .AddSwaggerGen(swagger => 27 | { 28 | // https://stackoverflow.com/a/71202135 29 | // https://stackoverflow.com/a/53521371 30 | swagger.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo 31 | { 32 | Title = "Practical .NET with OTEL Collector", 33 | Version = "v1", 34 | Contact = new Microsoft.OpenApi.Models.OpenApiContact 35 | { 36 | Email = "kim.cuhoang@gmail.com", 37 | Name = "Kim CH" 38 | } 39 | }); 40 | }); 41 | 42 | 43 | webApplicationBuilder.WebHost 44 | .CaptureStartupErrors(true) 45 | .UseKestrel(options => 46 | { 47 | options.AddServerHeader = false; 48 | 49 | // We have to separate into different ports because we don't use TLS 50 | options.ListenAnyIP(6002, listeningOptions => 51 | { 52 | listeningOptions.Protocols = HttpProtocols.Http1; 53 | }); 54 | options.ListenAnyIP(16002, listeningOptions => 55 | { 56 | listeningOptions.Protocols = HttpProtocols.Http2; 57 | }); 58 | }); 59 | 60 | 61 | using var app = webApplicationBuilder.Build(); 62 | 63 | app.UseSerilogRequestLogging(); 64 | 65 | //https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?source=recommendations&view=aspnetcore-7.0&tabs=visual-studio 66 | app 67 | .UseSwagger() 68 | .UseSwaggerUI(swagger => 69 | { 70 | swagger.SwaggerEndpoint("/swagger/v1/swagger.json", ".NET OTEL v1"); 71 | }); 72 | 73 | app.MapGrpcServices(); 74 | 75 | app 76 | .MapGet("/", () => TypedResults.Redirect("/swagger", permanent: true)) 77 | .ExcludeFromDescription(); 78 | 79 | app 80 | .MapGet("/hello", async (IWeatherGreetingGrpcService weatherGreetingService, CancellationToken cancellationToken) => 81 | { 82 | var result = await weatherGreetingService.SayHello(new Microservices.WeatherService.HelloRequest 83 | { 84 | Name = "SimulateClientApp" 85 | }, cancellationToken); 86 | 87 | return TypedResults.Ok(result); 88 | }) 89 | .WithName("Test-02") 90 | .WithOpenApi(); ; 91 | 92 | 93 | await app.RunAsync(); -------------------------------------------------------------------------------- /DNSLN.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31903.59 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__", "__", "{3BA20F7B-F3D0-455F-8006-45B6D0D02AA5}" 7 | ProjectSection(SolutionItems) = preProject 8 | .gitignore = .gitignore 9 | .justfile = .justfile 10 | Directory.Build.props = Directory.Build.props 11 | Directory.Packages.props = Directory.Packages.props 12 | global.json = global.json 13 | README.md = README.md 14 | EndProjectSection 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BuildingBlocks", "BuildingBlocks", "{EDBDCA9F-D712-4A08-B5D0-27DEC3E51102}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuildingBlocks.Observability", "src\BuildingBlocks\BuildingBlocks.Observability\BuildingBlocks.Observability.csproj", "{466255F6-E9F1-4731-9366-3BAD749EA3BB}" 19 | EndProject 20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microservices", "Microservices", "{8C0BDCCC-2892-498D-B586-52E9797F90B2}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microservices.WeatherService", "src\Microservices\Microservices.WeatherService\Microservices.WeatherService.csproj", "{F4292C51-22A0-48FD-BFF2-2CD76ECA1C98}" 23 | EndProject 24 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ClientApps", "ClientApps", "{CB1D050A-3DDC-46E5-B05D-E37DC0124941}" 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientApps.ClientSimulator", "src\ClientApps\ClientApps.ClientSimulator\ClientApps.ClientSimulator.csproj", "{75ED9BA8-BAEE-44CB-9A43-A9B177A93688}" 27 | EndProject 28 | Global 29 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 30 | Debug|Any CPU = Debug|Any CPU 31 | Release|Any CPU = Release|Any CPU 32 | EndGlobalSection 33 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 34 | {466255F6-E9F1-4731-9366-3BAD749EA3BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {466255F6-E9F1-4731-9366-3BAD749EA3BB}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {466255F6-E9F1-4731-9366-3BAD749EA3BB}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {466255F6-E9F1-4731-9366-3BAD749EA3BB}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {F4292C51-22A0-48FD-BFF2-2CD76ECA1C98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {F4292C51-22A0-48FD-BFF2-2CD76ECA1C98}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {F4292C51-22A0-48FD-BFF2-2CD76ECA1C98}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {F4292C51-22A0-48FD-BFF2-2CD76ECA1C98}.Release|Any CPU.Build.0 = Release|Any CPU 42 | {75ED9BA8-BAEE-44CB-9A43-A9B177A93688}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 43 | {75ED9BA8-BAEE-44CB-9A43-A9B177A93688}.Debug|Any CPU.Build.0 = Debug|Any CPU 44 | {75ED9BA8-BAEE-44CB-9A43-A9B177A93688}.Release|Any CPU.ActiveCfg = Release|Any CPU 45 | {75ED9BA8-BAEE-44CB-9A43-A9B177A93688}.Release|Any CPU.Build.0 = Release|Any CPU 46 | EndGlobalSection 47 | GlobalSection(SolutionProperties) = preSolution 48 | HideSolutionNode = FALSE 49 | EndGlobalSection 50 | GlobalSection(NestedProjects) = preSolution 51 | {466255F6-E9F1-4731-9366-3BAD749EA3BB} = {EDBDCA9F-D712-4A08-B5D0-27DEC3E51102} 52 | {F4292C51-22A0-48FD-BFF2-2CD76ECA1C98} = {8C0BDCCC-2892-498D-B586-52E9797F90B2} 53 | {75ED9BA8-BAEE-44CB-9A43-A9B177A93688} = {CB1D050A-3DDC-46E5-B05D-E37DC0124941} 54 | EndGlobalSection 55 | GlobalSection(ExtensibilityGlobals) = postSolution 56 | SolutionGuid = {C80F53CC-BD55-451A-918C-E7F0FDC848AF} 57 | EndGlobalSection 58 | EndGlobal 59 | -------------------------------------------------------------------------------- /src/BuildingBlocks/BuildingBlocks.Observability/ObservabilityRegistration.cs: -------------------------------------------------------------------------------- 1 | using BuildingBlocks.Observability.Options; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using OpenTelemetry; 6 | using OpenTelemetry.Metrics; 7 | using OpenTelemetry.Resources; 8 | using OpenTelemetry.Trace; 9 | using Serilog; 10 | using Serilog.Settings.Configuration; 11 | using Serilog.Sinks.OpenTelemetry; 12 | 13 | namespace BuildingBlocks.Observability; 14 | 15 | public static class ObservabilityRegistration 16 | { 17 | public static WebApplicationBuilder AddObservability(this WebApplicationBuilder builder) 18 | { 19 | var configuration = builder.Configuration; 20 | 21 | ObservabilityOptions observabilityOptions = new(); 22 | 23 | configuration 24 | .GetRequiredSection(nameof(ObservabilityOptions)) 25 | .Bind(observabilityOptions); 26 | 27 | builder.AddSerilog(observabilityOptions); 28 | builder.Services 29 | .AddOpenTelemetry() 30 | .ConfigureResource(resource => resource.AddService(observabilityOptions.ServiceName)) 31 | .AddMetrics(observabilityOptions) 32 | .AddTracing(observabilityOptions); 33 | 34 | return builder; 35 | } 36 | private static OpenTelemetryBuilder AddTracing(this OpenTelemetryBuilder builder, ObservabilityOptions observabilityOptions) 37 | { 38 | if (!observabilityOptions.EnabledTracing) return builder; 39 | 40 | builder.WithTracing(tracing => 41 | { 42 | tracing 43 | .SetErrorStatusOnException() 44 | .SetSampler(new AlwaysOnSampler()) 45 | .AddAspNetCoreInstrumentation(options => 46 | { 47 | options.RecordException = true; 48 | }); 49 | 50 | tracing 51 | .AddOtlpExporter(_ => 52 | { 53 | _.Endpoint = observabilityOptions.CollectorUri; 54 | _.ExportProcessorType = ExportProcessorType.Batch; 55 | _.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc; 56 | }); 57 | }); 58 | 59 | return builder; 60 | } 61 | 62 | private static OpenTelemetryBuilder AddMetrics(this OpenTelemetryBuilder builder, ObservabilityOptions observabilityOptions) 63 | { 64 | builder.WithMetrics(metrics => 65 | { 66 | metrics 67 | .AddAspNetCoreInstrumentation(); 68 | 69 | metrics 70 | .AddOtlpExporter(_ => 71 | { 72 | _.Endpoint = observabilityOptions.CollectorUri; 73 | _.ExportProcessorType = ExportProcessorType.Batch; 74 | _.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc; 75 | }); 76 | }); 77 | 78 | return builder; 79 | } 80 | 81 | private static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builder, ObservabilityOptions observabilityOptions) 82 | { 83 | var services = builder.Services; 84 | var configuration = builder.Configuration; 85 | 86 | services.AddSerilog((sp, serilog) => 87 | { 88 | serilog 89 | .ReadFrom.Configuration(configuration, new ConfigurationReaderOptions 90 | { 91 | SectionName = $"{nameof(ObservabilityOptions)}:{nameof(Serilog)}" 92 | }) 93 | .ReadFrom.Services(sp) 94 | .Enrich.FromLogContext() 95 | .Enrich.WithProperty("ApplicationName", observabilityOptions.ServiceName) 96 | .WriteTo.Console(); 97 | 98 | serilog 99 | .WriteTo.OpenTelemetry(c => 100 | { 101 | c.Endpoint = observabilityOptions.CollectorUrl; 102 | c.Protocol = OtlpProtocol.Grpc; 103 | c.IncludedData = IncludedData.TraceIdField | IncludedData.SpanIdField | IncludedData.SourceContextAttribute; 104 | c.ResourceAttributes = new Dictionary 105 | { 106 | {"service.name", observabilityOptions.ServiceName}, 107 | {"index", 10}, 108 | {"flag", true}, 109 | {"value", 3.14} 110 | }; 111 | }); 112 | }); 113 | 114 | return builder; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /local/build.cake: -------------------------------------------------------------------------------- 1 | #addin nuget:?package=Cake.Docker&version=1.2.0 2 | 3 | var target = Argument("target", "Default"); 4 | 5 | ////////////////////////////////////////////////////////////////////// 6 | // Variables 7 | ////////////////////////////////////////////////////////////////////// 8 | 9 | var sourceFolder = @"../src"; 10 | 11 | var tyeFolder = @"./tye"; 12 | 13 | var dockerComposesPath = @"./infra"; 14 | 15 | var dockerComposeFiles = new[] 16 | { 17 | "docker-compose.observability.yaml" 18 | }; 19 | 20 | var nameOfDockerImageBase = "localhost:5010/net7-alpine-base:latest"; 21 | 22 | ////////////////////////////////////////////////////////////////////// 23 | // TASKS 24 | ////////////////////////////////////////////////////////////////////// 25 | 26 | TaskSetup(taskSetupContext => { Console.Clear(); }); 27 | 28 | Task("DockerComposeDev-Down").Does(context => 29 | { 30 | context.Environment.WorkingDirectory = context.MakeAbsolute(Directory(dockerComposesPath)); 31 | 32 | var settings = new DockerComposeDownSettings 33 | { 34 | Volumes = true, 35 | Files = dockerComposeFiles 36 | }; 37 | 38 | context.DockerComposeDown(settings); 39 | }); 40 | 41 | Task("DockerCompose-Up").Does(context => 42 | { 43 | context.Environment.WorkingDirectory = context.MakeAbsolute(Directory(dockerComposesPath)); 44 | 45 | var settings = new DockerComposeUpSettings 46 | { 47 | DetachedMode = true, 48 | Files = dockerComposeFiles 49 | }; 50 | 51 | context.DockerComposeUp(settings); 52 | }); 53 | 54 | Task("Infra-Start").Does(context => 55 | { 56 | context.Environment.WorkingDirectory = context.MakeAbsolute(Directory(dockerComposesPath)); 57 | var settings = new DockerComposeSettings 58 | { 59 | Files = dockerComposeFiles 60 | }; 61 | 62 | context.DockerComposeStart(settings); 63 | 64 | }); 65 | 66 | Task("Stop-And-Remove-Local-Registry").Does(() => { 67 | 68 | // docker container stop registry && docker container rm -v registry 69 | 70 | DockerRm(new DockerContainerRmSettings { 71 | Force = true, 72 | Volumes = true 73 | }, "registry"); 74 | }); 75 | 76 | Task("Start-Local-Registry") 77 | .IsDependentOn("Stop-And-Remove-Local-Registry") 78 | .Does(() => 79 | { 80 | // docker run -d -p 5010:5000 --restart always --name registry registry:2 81 | DockerRun(new DockerContainerRunSettings { 82 | Detach = true, 83 | Publish = new[] { "5010:5000" }, 84 | Name = "registry", 85 | Restart = "always" 86 | }, "registry:2", string.Empty); 87 | }); 88 | 89 | Task("Build-Image-Base") 90 | .IsDependentOn("Start-Local-Registry") 91 | .Does(() => 92 | { 93 | DockerBuild(new DockerImageBuildSettings { 94 | File = "./Dockerfile", 95 | Tag = new [] { nameOfDockerImageBase } 96 | }, "../"); 97 | 98 | DockerPush(nameOfDockerImageBase); 99 | }); 100 | 101 | Task("Build-Docker-Images") 102 | .DoesForEach(new[] {"Microservices", "ClientApps"}, folder => 103 | { 104 | var csprojFiles = GetFiles($@"{sourceFolder}/{folder}/**/*.csproj"); 105 | 106 | // Uncomment if do not want to use own custom image "localhost:5010/net7-alpine-base:latest" 107 | // 108 | // nameOfDockerImageBase = "mcr.microsoft.com/dotnet/aspnet:7.0.5"; 109 | // nameOfDockerImageBase = "mcr.microsoft.com/dotnet/aspnet:7.0.5-alpine3.17-amd64"; 110 | 111 | foreach(var csprojFile in csprojFiles) 112 | { 113 | DotNetPublish(csprojFile.FullPath, new DotNetPublishSettings{ 114 | ArgumentCustomization = builder => builder 115 | .Append("--os linux") 116 | .Append("--arch x64") 117 | .Append("-c Release") 118 | // .Append("-p:ContainerRuntimeIdentifier=linux-x64") 119 | .Append($"-p:ContainerBaseImage={nameOfDockerImageBase}") 120 | .Append("-p:ContainerImageTags=latest"), 121 | WorkingDirectory = csprojFile.GetDirectory() 122 | }); 123 | } 124 | }); 125 | 126 | 127 | Task("Tye").Does(() => { 128 | 129 | var tyeFileName = "tye.yaml"; 130 | // tyeFileName = "tye-container.yaml"; 131 | // tyeFileName = "tye-mixing.yaml"; 132 | 133 | var logDirectory = System.IO.Path.Combine(tyeFolder, @".logs"); 134 | var tyeFilePath = System.IO.Path.Combine(tyeFolder, tyeFileName); 135 | 136 | if (DirectoryExists(logDirectory)) 137 | { 138 | DeleteDirectory(logDirectory, new DeleteDirectorySettings { 139 | Recursive = true, 140 | Force = true 141 | }); 142 | } 143 | 144 | StartProcess("tye", new ProcessSettings { 145 | Arguments = new ProcessArgumentBuilder() 146 | .Append("run") 147 | .Append(tyeFilePath) 148 | // .Append("--watch") 149 | .Append("--dashboard") 150 | } 151 | ); 152 | 153 | }); 154 | 155 | ////////////////////////////////////////////////////////////////////// 156 | // EXECUTION 157 | ////////////////////////////////////////////////////////////////////// 158 | 159 | Task("Default").Does(() => Information("Dotnet Practical")); 160 | 161 | RunTarget(target); 162 | 163 | 164 | 165 | ////////////////////////////////////////////////////////////////////// 166 | // Function 167 | ////////////////////////////////////////////////////////////////////// -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | 400 | 401 | local/tools 402 | local/tye/.tye --------------------------------------------------------------------------------