Skip to content

Commit

Permalink
Add Probes
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Nov 10, 2023
1 parent 25e40e0 commit ce8846c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/Publish Probes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Release Probes

on:
repository_dispatch:
push:
tags:
- "Probes-v[0-9]+.[0-9]+.[0-9]+"

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NUGET_APIKEY: ${{ secrets.NUGET_APIKEY}}

jobs:

Release:
name: Release
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v2

- name: Verify commit
run: |
git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
git branch --remote --contains | grep origin/main
- name: Set version
run: echo "VERSION=${GITHUB_REF/refs\/tags\/Probes-v/}" >> $GITHUB_ENV

- name: Build
run: |
dotnet build src/BitzArt.Probes/BitzArt.Probes.csproj --configuration Release /p:Version=${VERSION}
dotnet pack src/BitzArt.Probes/BitzArt.Probes.csproj --configuration Release /p:Version=${VERSION} --no-build --output .
- name: Push
run: dotnet nuget push BitzArt.Probes.${VERSION}.nupkg --source https://api.nuget.org/v3/index.json --api-key ${NUGET_APIKEY}
7 changes: 7 additions & 0 deletions Miscellaneous.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.OpenTelemetry.Boile
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "OpenTelemetry", "OpenTelemetry", "{E47B8516-234F-4D06-957C-B24278490706}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BitzArt.Probes", "src\BitzArt.Probes\BitzArt.Probes.csproj", "{014EF2B0-4D13-4A35-ADE7-006689C5EC3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -88,6 +90,10 @@ Global
{A258E652-1BB2-4A90-9ED4-6248A09FD379}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A258E652-1BB2-4A90-9ED4-6248A09FD379}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A258E652-1BB2-4A90-9ED4-6248A09FD379}.Release|Any CPU.Build.0 = Release|Any CPU
{014EF2B0-4D13-4A35-ADE7-006689C5EC3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{014EF2B0-4D13-4A35-ADE7-006689C5EC3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{014EF2B0-4D13-4A35-ADE7-006689C5EC3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{014EF2B0-4D13-4A35-ADE7-006689C5EC3D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -107,6 +113,7 @@ Global
{8AC27A66-E451-4BA5-BE17-F1CA452B24EB} = {5BA67B39-E0AA-4A92-B619-0E7ADCDA6871}
{A258E652-1BB2-4A90-9ED4-6248A09FD379} = {E47B8516-234F-4D06-957C-B24278490706}
{E47B8516-234F-4D06-957C-B24278490706} = {2638AACB-7314-4962-87BE-93876EEED788}
{014EF2B0-4D13-4A35-ADE7-006689C5EC3D} = {2638AACB-7314-4962-87BE-93876EEED788}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49BCA0AC-45AF-4DE6-B691-30A0F2959200}
Expand Down
24 changes: 24 additions & 0 deletions src/BitzArt.Probes/BitzArt.Probes.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<PackageId>BitzArt.Probes</PackageId>
<Authors>BitzArt</Authors>
<Description>Default probes and healthchecks for web APIs</Description>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/BitzArt/Miscellaneous</RepositoryUrl>
<PackageProjectUrl>https://github.com/BitzArt/Miscellaneous</PackageProjectUrl>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="7.1.0" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/BitzArt.Probes/Extensions/AddServiceInfoOptionsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using BitzArt.Probes;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace BitzArt;

public static class AddServiceInfoOptionsExtension
{
public static IHostApplicationBuilder AddServiceInfoOptions(this IHostApplicationBuilder builder)
{
builder.Services.AddServiceInfoOptions(builder.Configuration);
return builder;
}

public static IServiceCollection AddServiceInfoOptions(this IServiceCollection services, IConfiguration configuration)
{
var options = configuration
.GetRequiredSection(ServiceInfoOptions.SectionName)
.Get<ServiceInfoOptions>()!;

services.AddSingleton(options);

return services;
}
}
33 changes: 33 additions & 0 deletions src/BitzArt.Probes/Extensions/MapProbesExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using BitzArt.Probes;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace BitzArt;

public static class MapProbesExtension
{
public static IEndpointRouteBuilder MapRoutes(this IEndpointRouteBuilder builder)
{
builder.MapHealthChecks("_health", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});

var infoOptions = builder.ServiceProvider.GetService<ServiceInfoOptions>();

if (infoOptions is null)
{
builder.MapGet("/", () => "It's good to be alive.");
}
else
{
var info = new ServiceInfoResponse(infoOptions);
builder.MapGet("/", () => info);
}

return builder;
}
}
12 changes: 12 additions & 0 deletions src/BitzArt.Probes/Models/ServiceInfoResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace BitzArt.Probes;

internal class ServiceInfoResponse(ServiceInfoOptions options)
{
[JsonPropertyName("serviceName")]
public string? ServiceName { get; set; } = options.Name;

[JsonPropertyName("version")]
public string? Version { get; set; } = options.Version;
}
9 changes: 9 additions & 0 deletions src/BitzArt.Probes/Options/ServiceInfoOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace BitzArt.Probes;

public class ServiceInfoOptions
{
public const string SectionName = "ServiceInfo";

public string? Name { get; set; }
public string? Version { get; set; }
}

0 comments on commit ce8846c

Please sign in to comment.