Skip to main content
İzleme tarafı iki ayrı pakete bölünür:
  • BuildingBlocks.Monitoring.Prometheusprometheus-net tabanlı, basit HTTP metrik toplama ve /metrics endpoint’i.
  • BuildingBlocks.Observability — OpenTelemetry tabanlı tam pipeline: metrics + tracing, instrumentation flag’leri, health-check → Prometheus gauge köprüsü ve master kill-switch.
Prometheus/Grafana/Seq yığınının operasyonel kurulumu için bkz. Observability.

BuildingBlocks.Monitoring.Prometheus

prometheus-net.AspNetCore üzerine üç extension.
MetotTipEtki
AddPrometheusMonitoring()IServiceCollectionUseHttpClientMetrics() — giden HTTP metrikleri
UsePrometheusMonitoring()IApplicationBuilderUseHttpMetrics() — gelen HTTP metrik middleware’i
MapPrometheusMonitoring()IEndpointRouteBuilderMapMetrics()/metrics endpoint’i
builder.Services.AddPrometheusMonitoring();

var app = builder.Build();
app.UsePrometheusMonitoring();
app.MapPrometheusMonitoring();   // /metrics

BuildingBlocks.Observability

OTel pipeline’ını tek çağrıyla kurar: AddDiyanetObservability(configuration). Observability:Enabled = false ise hiçbir şey register edilmez (early-return) — paket “yokmuş gibi” davranır, sıfır maliyet.
MetotTipEtki
AddDiyanetObservability(configuration)IServiceCollectionResource attribute’ları + metrics/tracing + health publisher
MapDiyanetObservabilityEndpoints()IEndpointRouteBuilderMetrics.Endpoint (default /metrics) Prometheus scrape

ObservabilityOptions

Observability bölümüne bind edilir. Üç label (Project, Service, environment) merkezi Grafana’da çoklu projeyi tek template ile yönetmek için kontrattır.
ÜyeVarsayılanAçıklama
EnabledfalseMaster kill-switch. false → hiçbir OTel kaydı yok
Project"unknown"Grafana label project
Service"api"Grafana label service
MetricsMetricsOptions alt bloğu
TracingTracingOptions alt bloğu (default kapalı)
HealthChecksAsMetricstrue/health/ready sonuçlarını gauge’a yayınla
MetricsOptions: Enabled (true), Endpoint (/metrics), AspNetCore (true), HttpClient (true), Runtime (true), EfCore (false — cardinality riski), Redis (false). TracingOptions: Enabled (false), SampleRatio (0.1), OtlpEndpoint (http://localhost:4317).

Instrumentation flag’leri

AddDiyanetObservability her instrumentation’ı yalnızca ilgili flag açıksa ekler:
otel.WithMetrics(m =>
{
    if (options.Metrics.AspNetCore) m.AddAspNetCoreInstrumentation();
    if (options.Metrics.HttpClient) m.AddHttpClientInstrumentation();
    if (options.Metrics.Runtime)    m.AddRuntimeInstrumentation();
    if (options.Metrics.EfCore)     m.AddMeter("Microsoft.EntityFrameworkCore");
    if (options.Metrics.Redis)      m.AddMeter("OpenTelemetry.Instrumentation.StackExchangeRedis");
    if (options.HealthChecksAsMetrics) m.AddMeter(HealthCheckMetricsPublisher.MeterName);
    m.AddPrometheusExporter();
});

if (options.Tracing.Enabled)
    otel.WithTracing(t =>
    {
        t.SetSampler(new TraceIdRatioBasedSampler(Math.Clamp(options.Tracing.SampleRatio, 0, 1)));
        t.AddAspNetCoreInstrumentation();
        t.AddHttpClientInstrumentation();
        t.AddOtlpExporter(o => o.Endpoint = new Uri(options.Tracing.OtlpEndpoint));
    });

Health-check → gauge köprüsü

HealthChecksAsMetrics = true iken HealthCheckMetricsPublisher (IHealthCheckPublisher) kaydedilir. ASP.NET sağlık altyapısı bunu periyodik tetikler (5 sn delay, 30 sn period); her sonuç bir ObservableGauge’a yazılır ve Prometheus scrape ile okunur:
diyanet_healthcheck_status{check="postgres", tags="ready,db"} 2
# 2 = Healthy, 1 = Degraded, 0 = Unhealthy
Bu, K8s readiness probe için kullanılan /health/ready endpoint’inden bağımsız çalışır (biri probe, diğeri metrik). Grafana alarm örneği:
diyanet_healthcheck_status{check="postgres"} == 0                 # kritik
avg_over_time(diyanet_healthcheck_status[5m]) < 1.5               # flapping

Konfigürasyon

{
  "Observability": {
    "Enabled": true,
    "Project": "diyanet",
    "Service": "api",
    "Metrics": { "Enabled": true, "AspNetCore": true, "HttpClient": true, "Runtime": true, "EfCore": false, "Redis": false },
    "Tracing": { "Enabled": false, "SampleRatio": 0.1, "OtlpEndpoint": "http://tempo:4317" },
    "HealthChecksAsMetrics": true
  }
}
/metrics dış dünyaya açık olmamalı — internal port veya ingress allowlist arkasında olmalı. Public exposure cardinality bombardımanı ve internal endpoint enumeration riski taşır (ör. location /metrics { allow 10.0.0.0/8; deny all; }).
Müşteri matrisi: Prometheus/Grafana yoksa Enabled=false (tek satırla komple kapalı, BuildingBlocks.HealthChecks yeter). Standart prod: Enabled=true, Tracing=false. SRE ekibi: Tracing=true.

İlgili

Observability

Prometheus + Grafana + Tempo yığını.

Logging

Serilog + Seq structured logging.

Operasyon

Docker ve ortam yönetimi.

Env değişkenleri

OBSERVABILITY_* ve GRAFANA_* anahtarları.