Skip to content

miccore/serviceDefaults

Repository files navigation

Miccore.Clean.ServiceDefaults

NuGet NuGet Downloads License: MIT

Shared service defaults for .NET Aspire applications including OpenTelemetry, Health Checks, Resilience, and Service Discovery configurations.

Features

  • 📊 OpenTelemetry Integration - Complete telemetry setup with logging, metrics, and tracing
  • 🏥 Health Checks - Pre-configured health check endpoints (/health and /alive)
  • 🔄 Resilience - HTTP resilience handlers with retry policies
  • 🔍 Service Discovery - Built-in service discovery support
  • 🌐 Gateway Extensions - API Gateway specific configurations (caching, compression)

Supported Frameworks

  • .NET 8.0
  • .NET 9.0
  • .NET 10.0

Installation

dotnet add package Miccore.Clean.ServiceDefaults

Or via Package Manager Console:

Install-Package Miccore.Clean.ServiceDefaults

Quick Start

1. Add Service Defaults to your application

In your Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add service defaults (OpenTelemetry, Health Checks, Service Discovery, Resilience)
builder.AddServiceDefaults();

// Add your other services...
builder.Services.AddControllers();

var app = builder.Build();

// Map default endpoints (health checks)
app.MapDefaultEndpoints();

// Map your other endpoints...
app.MapControllers();

app.Run();

2. Available Extension Methods

AddServiceDefaults()

Configures the complete set of service defaults:

builder.AddServiceDefaults();

This includes:

  • OpenTelemetry (logging, metrics, tracing)
  • Health checks
  • Service discovery
  • HTTP resilience

MapDefaultEndpoints()

Maps the health check endpoints:

app.MapDefaultEndpoints();

Exposes:

  • /health - Full health check
  • /alive - Liveness probe

3. Gateway-Specific Configuration

For API Gateway services, use additional gateway extensions:

builder.AddServiceDefaults();
builder.AddGatewayDefaults(); // Adds caching, compression, etc.

Configuration

OpenTelemetry

The package automatically configures OpenTelemetry based on environment variables:

Variable Description
OTEL_EXPORTER_OTLP_ENDPOINT OTLP exporter endpoint
OTEL_SERVICE_NAME Service name for telemetry

Health Checks

Health check endpoints are automatically mapped:

  • /health - Returns detailed health status
  • /alive - Simple liveness check for Kubernetes probes

Example: Complete Setup

using Miccore.Clean.ServiceDefaults;

var builder = WebApplication.CreateBuilder(args);

// Add all service defaults
builder.AddServiceDefaults();

// Add application services
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure pipeline
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

// Map health endpoints
app.MapDefaultEndpoints();

// Map your API endpoints
app.MapGet("/api/hello", () => "Hello World!");

app.Run();

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues, please open an issue on GitHub.

About

Add open telemetry to projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages