Package to publish results of an ASP.NET Core health check to Datadog using Datadog's REST API. You can use this package in one of the two ways:
This is where your ASP.NET Core application publishes its own health check results using ASP.NET Core's default publishing service based on IHostedService. The way it will work is at regular intervals (a customisable number), the built in HealthCheckPublisherHostedService will collect health check results from all the health checks registered in your application using another built in HealthCheckService and then call into all the registered IHealthCheckPublisher implementations to publish the results of the health check.
The steps to register the Datadog API publisher are:
services.AddHealthChecks()
.AddCheck<MyAppHealthCheck>("MyAppHealthCheck"); Step 2: Make sure you have the Datadog API key and application key and the following settings exist in your secrets.json (for local dev work) and your CI environment (you MUST have some secure way of providing these into your application)
{
"DatadogApiSettings": {
"ApiKey": "<DATADOG API KEY>",
"ApplicationKey": "<APPLICATION KEY THAT THE API KEY WILL WORK WITH>"
}
}Step 3: Add this package (AspNetCore.HealthCheckPublisher.DataDogApi) to the IHealthChecksBuilder chain
services
.AddHealthChecks()
.AddCheck<MyAppHealthCheck>("MyAppHealthCheck")
.AddDatadogPublisher(
Configuration.GetDatadogApiSettings(),
new HealthReportOptionsBuilder(applicationPrefix: "myapi")
.WithOptionalDefaultMetricTag("environment", Environment.EnvironmentName)
.Build());This is where the you ping the health endpoint on your ASP.NET Core application from an externally hosted service for e.g. a serverless function. This way if the application is unavailable the telemetry won't go down with it. Using this package out of process involves following steps:
Step 1: Register an instance of the health reporter by a call to AddDatadogHealthReporter extension method available on the ServiceCollection passing in an instance of DatadogApiSettings that you can hydrate from IConfiguration:
_services.AddDatadogHealthReporter(
new DatadogApiSettings(
configuration.GetValue<string>("DatadogApiSettingsApiKey"),
configuration.GetValue<string>("DatadogApiSettingsApplicationKey")); Step 3: Resolve the instance of IApplicationHealthReporter from the ServiceCollection either in a scope or via constructor injection
var reporter = scope.ServiceProvider.GetService<IApplicationHealthReporter>(); await reporter.SendHealthReport(
healthReport,
new HealthReportOptionsBuilder(applicationPrefix: "myapi")
.WithOptionalDefaultMetricTag(
"Environment", configuration.GetValue("ASPNETCORE_ENVIRONMENT", "development"))
.Build());