Skip to content

Commit

Permalink
feat: replace OpenTelemetry and multi-manifest Docker image logic wit…
Browse files Browse the repository at this point in the history
…h NuGet package mu88.Shared
  • Loading branch information
mu88 committed Aug 2, 2024
1 parent 07cf6b8 commit 4be013c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 150 deletions.
5 changes: 1 addition & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@
<PackageVersion Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="Microsoft.Playwright" Version="1.45.1" />
<PackageVersion Include="mu88.Shared" Version="0.0.7" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NUnit" Version="4.1.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.2.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.1.118" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageVersion Include="Testcontainers" Version="3.9.0" />
Expand Down
78 changes: 23 additions & 55 deletions src/ScreenshotCreator.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using Microsoft.Extensions.Options;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using mu88.Shared.OpenTelemetry;
using ScreenshotCreator.Api;
using ScreenshotCreator.Logic;
using Creator = ScreenshotCreator.Logic.ScreenshotCreator;

var builder = WebApplication.CreateBuilder(args);

ConfigureOpenTelemetry(builder);
builder.ConfigureOpenTelemetry("ScreenshotCreator");

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Configuration
.AddJsonFile("appsettings.secret.json", true)
.AddKeyPerFile("/run/secrets", true);
.AddJsonFile("appsettings.secret.json", true)
.AddKeyPerFile("/run/secrets", true);
builder.Services
.AddOptions<ScreenshotOptions>()
.Bind(builder.Configuration.GetSection(ScreenshotOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();
.AddOptions<ScreenshotOptions>()
.Bind(builder.Configuration.GetSection(ScreenshotOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();
builder.Services.AddSingleton<IScreenshotCreator, Creator>();
builder.Services.AddSingleton<IPlaywrightHelper, PlaywrightHelper>();
builder.Services.AddSingleton<ImageProcessor>();
Expand All @@ -39,22 +36,22 @@

app.MapGet("latestImage", ReturnImageOrNotFoundAsync);
app.MapGet("createImageNow",
async (HttpContext httpContext, ImageProcessor imageProcessor, IScreenshotCreator creator, IOptions<ScreenshotOptions> options) =>
{
await creator.CreateScreenshotAsync(options.Value.Width, options.Value.Height);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor, options);
});
async (HttpContext httpContext, ImageProcessor imageProcessor, IScreenshotCreator creator, IOptions<ScreenshotOptions> options) =>
{
await creator.CreateScreenshotAsync(options.Value.Width, options.Value.Height);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor, options);
});
app.MapGet("createImageWithSizeNow",
async (uint width,
uint height,
HttpContext httpContext,
ImageProcessor imageProcessor,
IOptions<ScreenshotOptions> options,
IScreenshotCreator creator) =>
{
await creator.CreateScreenshotAsync(width, height);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor, options);
});
async (uint width,
uint height,
HttpContext httpContext,
ImageProcessor imageProcessor,
IOptions<ScreenshotOptions> options,
IScreenshotCreator creator) =>
{
await creator.CreateScreenshotAsync(width, height);
return await ReturnImageOrNotFoundAsync(httpContext, imageProcessor, options);
});

await app.RunAsync();

Expand Down Expand Up @@ -82,33 +79,4 @@ async Task<IResult> ReturnImageOrNotFoundAsync(HttpContext httpContext,
}

return result;
}

static void ConfigureOpenTelemetry(IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services
.AddOpenTelemetry()
.ConfigureResource(c => c.AddService("ScreenshotCreator"))
.WithMetrics(metrics =>
{
metrics
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation();
});

var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}
}
92 changes: 1 addition & 91 deletions src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
<PlaywrightPlatform>all</PlaywrightPlatform>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="mu88.Shared"/>
<PackageReference Include="Swashbuckle.AspNetCore" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ScreenshotCreator.Logic\ScreenshotCreator.Logic.csproj" />
Expand All @@ -26,91 +23,4 @@
<None Remove="*.png" />
<None Remove="*.jpeg" />
</ItemGroup>

<!-- Multi-arch images (see https://github.com/dotnet/sdk-container-builds/issues/87) -->
<PropertyGroup>
<RuntimeIdentifiers>linux-x64;linux-arm64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
<SingleRegistryPublish Include="$(MSBuildProjectFullPath)" AdditionalProperties="ContainerRegistry=%(DestinationRegistry)" />
</ItemGroup>

<Target Name="MultiRidBuild">
<ItemGroup>
<_rids Include="$(RuntimeIdentifiers)" />
<_InnerBuild Include="$(MSBuildProjectFullPath)" AdditionalProperties="RuntimeIdentifier=%(_rids.Identity)" PropertiesToUnset="RuntimeIdentifiers" />
</ItemGroup>
<MSBuild Projects="@(_InnerBuild)" Targets="Build" BuildInParallel="true" />
</Target>

<Target Name="MultiPush" DependsOnTargets="Publish">
<MSBuild Projects="@(SingleRegistryPublish)" Targets="PublishContainer" BuildInParallel="true" />
</Target>

<Target Name="MultiArchPublish">
<Error Text="ContainerRegistry MUST BE SET" Condition="'$(ContainerRegistry)' == ''" />

<ItemGroup>
<!-- first, define the container tags you want to create. these will be the roots of the arch-specific manifests - what users will mostly be using. -->
<_TagsToPublish Include="$(ContainerImageTags)" />

<!-- next, define the architectures you want to publish for. we could have just used RuntimeIdentifiers from above, but I wanted to use golang-style tag parts,
so that these images would look and feel like other container images. -->
<_RIDItems Include="linux-x64" GolangArch="amd64" />
<_RIDItems Include="linux-arm64" GolangArch="arm64" />
</ItemGroup>

<!-- Ideally at this point we'd call some target that would map .NET RIDs to Golang architecture items, but this doesn't currently exist -->
<!-- <MapRidsToGolangArch RIDs="@(_RIDItems)">
<Output TaskParameter="RidsWithArch" ItemName="_RIDItems" />
</MapRidsToGolangArch> -->

<ItemGroup>
<!-- this next part is kind of gross - we're trying to construct an MSBuild property value like 'ContainerImageTags=<tagbase>-<arch>;<tagbase>-<arch>'
this means we need a map of each specific arch to the list of tags we'd like to push for that arch. the easiest way I found to do that is
* create unique items that 'splat' the RIDs across each of the tags
* batch by the RIDs - thus capturing all of the tags in the ItemGroup for that batch. we can now transform that ItemGroup to make our final property string -->

<_RIDSForTag Include="@(_TagsToPublish)" RID="%(_RIDItems.Identity)" GolangArch="%(_RIDItems.GolangArch)" />
<_TagsForRID Include="%(_RIDSForTag.RID)" ContainerTags="@(_RIDSForTag-&gt;'%(Identity)-%(GolangArch)', ';')" />

<!-- now that we have our mappings, we can build this project for each RID, passing the full set of tags to create -->
<_ContainerPublish Include="$(MSBuildProjectFullPath)" AdditionalProperties="RuntimeIdentifier=%(_TagsForRID.Identity);ContainerImageTags=%(_TagsForRID.ContainerTags)" />
</ItemGroup>

<MSBuild Projects="@(_ContainerPublish)" Targets="Publish;PublishContainer" BuildInParallel="true" />

<Message Text="Created architecture-specific images, now creating manifest list" Condition="'$(ContainerRegistry)' != ''" Importance="High" />

<!--
If we pushed to an actual registry we can make a multi-arch image here. We need the image name and the tags to do so.
However, the PublishContainer targt doesn't actually emit the name or tags so we have to reconstruct it.
This is a gap, we should fill it in the SDK.
NOTE: We can only create the multi-arch manifest if we pushed to a registry. This is a fundamental constraint, so that condition has been applied to all of this code
-->
<!-- Call ComputeContainerConfig on this project so that the properties it sets are visible -->
<MSBuild Projects="$(MSBuildThisFile)" Targets="ComputeContainerConfig" Condition="'$(ContainerRegistry)' != ''" />

<!-- To make a manifest we need to know the name of the manifest (e.g. mcr.microsoft.com/dotnet/sdk:8.0) and the names of all of the
architecture-specific images that go into that manifest (e.g. mcr.microsoft.com/dotnet/sdk:8.0-arm64, mcr.microsoft.com/dotnet/sdk:8.0-amd64, etc).
Again, the easiest way to do that is to make add the 'parent' information to some other pience of data that already contains the tag/arch data.
-->

<ItemGroup Condition="'$(ContainerRegistry)' != ''">
<!-- We'll need to group by 'tag' here to get the manifest names (the nice tags). To make that easier, let's add the parent manifest name and the
'final' container name onto each of our already-RID-aware Tag items. -->
<_RIDSForTag>
<FullImageName>$(ContainerRegistry)/$(ContainerRepository):%(Identity)-%(GolangArch)</FullImageName>
<ParentManifestName>$(ContainerRegistry)/$(ContainerRepository):%(Identity)</ParentManifestName>
</_RIDSForTag>
</ItemGroup>

<!-- armed with that data, we can easily batch these Task calls. '%(_RIDSForTag.ParentManifestName)' filters _RIDSForTag down to all items that have the same ParentManifestName.
this means that we can refer to @(_RIDSForTag) here as only the Items for the images for this manifest! From there it's easy to splat the FullImageName onto the CLI calls. -->
<Exec Condition="'$(ContainerRegistry)' != ''" Command="docker manifest create %(_RIDSForTag.ParentManifestName) @(_RIDSForTag-&gt;'%(FullImageName)', ' ') --amend" />
<Exec Condition="'$(ContainerRegistry)' != ''" Command="docker manifest push %(_RIDSForTag.ParentManifestName)" />
<Message Text="Created and pushed manifest list %(_RIDSForTag.ParentManifestName)" Condition="'$(ContainerRegistry)' != ''" Importance="High" />
</Target>
</Project>

0 comments on commit 4be013c

Please sign in to comment.