Skip to content

Commit

Permalink
Merge pull request #71 from CommunityToolkit/issue43
Browse files Browse the repository at this point in the history
Golang Integration
  • Loading branch information
aaronpowell authored Oct 8, 2024
2 parents 8c54eea + e620836 commit 5e88805
Show file tree
Hide file tree
Showing 24 changed files with 895 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"ghcr.io/devcontainers/features/docker-in-docker": {},
"ghcr.io/devcontainers/features/node:latest": {},
"ghcr.io/devcontainers/features/python:latest": {}
"ghcr.io/devcontainers/features/python:latest": {},
"ghcr.io/devcontainers/features/go:latest": {}
},

"customizations": {
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jobs:
dotnet-version: |
8.0.x
9.0.x
- uses: actions/setup-go@v5
name: Set up Go
with:
go-version: '^1.22.1'
- uses: actions/setup-java@v4
name: Set up Java
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/dotnet-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jobs:
dotnet-version: |
8.0.x
9.0.x
- uses: actions/setup-go@v5
name: Set up Go
with:
go-version: '^1.22.1'
- uses: actions/setup-java@v4
name: Set up Java
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/dotnet-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
dotnet-version: |
${{ env.DEFAULT_DOTNET_VERSION }}
9.0.x
- uses: actions/setup-go@v5
name: Set up Go
with:
go-version: '^1.22.1'
- uses: actions/setup-java@v4
name: Set up Java
with:
Expand Down
30 changes: 30 additions & 0 deletions Aspire.CommunityToolkit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.Hos
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.Hosting.Ollama.Web", "examples\ollama\Aspire.CommunityToolkit.Hosting.Ollama.Web\Aspire.CommunityToolkit.Hosting.Ollama.Web.csproj", "{0A17021E-5F10-429B-88C8-E4073EADACB6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.Hosting.Golang", "src\Aspire.CommunityToolkit.Hosting.Golang\Aspire.CommunityToolkit.Hosting.Golang.csproj", "{93789F1B-0539-49B5-A585-9F6EF9E88264}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "golang", "golang", "{03950661-2B1F-48F4-91D2-1E6395B710C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.Hosting.Golang.AppHost", "examples\golang\Aspire.CommunityToolkit.Hosting.Golang.AppHost\Aspire.CommunityToolkit.Hosting.Golang.AppHost.csproj", "{3A03552A-252D-4E86-A8D7-135579AAEB85}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.Hosting.Golang.ServiceDefaults", "examples\golang\Aspire.CommunityToolkit.Hosting.Golang.ServiceDefaults\Aspire.CommunityToolkit.Hosting.Golang.ServiceDefaults.csproj", "{C60BCC90-BAEE-4678-ACC9-11553AA64249}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.Hosting.Golang.Tests", "tests\Aspire.CommunityToolkit.Hosting.Golang.Tests\Aspire.CommunityToolkit.Hosting.Golang.Tests.csproj", "{0294714B-A1CC-4F8E-8935-315B3499638F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.OllamaSharp", "src\Aspire.CommunityToolkit.OllamaSharp\Aspire.CommunityToolkit.OllamaSharp.csproj", "{22D81BC2-2659-483A-B7AC-8FCA086A99A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.CommunityToolkit.OllamaSharp.Tests", "tests\Aspire.CommunityToolkit.OllamaSharp.Tests\Aspire.CommunityToolkit.OllamaSharp.Tests.csproj", "{B170B19B-3FFA-4F71-AF12-A1E3955A0F8E}"
Expand Down Expand Up @@ -147,6 +156,22 @@ Global
{0A17021E-5F10-429B-88C8-E4073EADACB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A17021E-5F10-429B-88C8-E4073EADACB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A17021E-5F10-429B-88C8-E4073EADACB6}.Release|Any CPU.Build.0 = Release|Any CPU
{93789F1B-0539-49B5-A585-9F6EF9E88264}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93789F1B-0539-49B5-A585-9F6EF9E88264}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93789F1B-0539-49B5-A585-9F6EF9E88264}.Release|Any CPU.ActiveCfg = Release|Any CPU
{93789F1B-0539-49B5-A585-9F6EF9E88264}.Release|Any CPU.Build.0 = Release|Any CPU
{3A03552A-252D-4E86-A8D7-135579AAEB85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A03552A-252D-4E86-A8D7-135579AAEB85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A03552A-252D-4E86-A8D7-135579AAEB85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A03552A-252D-4E86-A8D7-135579AAEB85}.Release|Any CPU.Build.0 = Release|Any CPU
{C60BCC90-BAEE-4678-ACC9-11553AA64249}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C60BCC90-BAEE-4678-ACC9-11553AA64249}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C60BCC90-BAEE-4678-ACC9-11553AA64249}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C60BCC90-BAEE-4678-ACC9-11553AA64249}.Release|Any CPU.Build.0 = Release|Any CPU
{0294714B-A1CC-4F8E-8935-315B3499638F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0294714B-A1CC-4F8E-8935-315B3499638F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0294714B-A1CC-4F8E-8935-315B3499638F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0294714B-A1CC-4F8E-8935-315B3499638F}.Release|Any CPU.Build.0 = Release|Any CPU
{22D81BC2-2659-483A-B7AC-8FCA086A99A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22D81BC2-2659-483A-B7AC-8FCA086A99A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22D81BC2-2659-483A-B7AC-8FCA086A99A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -184,6 +209,11 @@ Global
{F4602DC8-3C17-4834-B640-9A3B27FE278A} = {14BD8AE7-C8DF-4C7C-8244-7F74C101569D}
{2F037600-2002-4A13-9359-98FB0D2416BE} = {14BD8AE7-C8DF-4C7C-8244-7F74C101569D}
{0A17021E-5F10-429B-88C8-E4073EADACB6} = {14BD8AE7-C8DF-4C7C-8244-7F74C101569D}
{93789F1B-0539-49B5-A585-9F6EF9E88264} = {414151D4-7009-4E78-A5C6-D99EBD1E67D1}
{03950661-2B1F-48F4-91D2-1E6395B710C2} = {8519CC01-1370-47C8-AD94-B0F326B1563F}
{3A03552A-252D-4E86-A8D7-135579AAEB85} = {03950661-2B1F-48F4-91D2-1E6395B710C2}
{C60BCC90-BAEE-4678-ACC9-11553AA64249} = {03950661-2B1F-48F4-91D2-1E6395B710C2}
{0294714B-A1CC-4F8E-8935-315B3499638F} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{22D81BC2-2659-483A-B7AC-8FCA086A99A7} = {414151D4-7009-4E78-A5C6-D99EBD1E67D1}
{B170B19B-3FFA-4F71-AF12-A1E3955A0F8E} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
EndGlobalSection
Expand Down
25 changes: 25 additions & 0 deletions docs/integrations/hosting-golang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# CommunityToolkit.Hosting.Golang

## Overview

This is a .NET Aspire Integration for [Go](https://go.dev/) applications.

## Usage

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var golang = builder.AddGolangApp("golang", "../gin-api");

builder.Build().Run();
```

### Configuration
- `name`- The name of the resource.
- `workingDirectory`- The working directory to use for the command. If null, the working directory of the current process is used.
- `port`- This is the port that will be given to other resource to communicate with this resource. Deafults to `8080`.
- `args`- The optinal arguments to be passed to the executable when it is started.

### OpenTelemetry Configuration

In the [example](../../examples/golang/) folder, you can find an example of how to configure OpenTelemetry in the Go application to use the Aspire dashboard.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>5a6548a5-b5dd-40f0-876a-9e3d4ac91fd1</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Aspire.CommunityToolkit.Hosting.Golang\Aspire.CommunityToolkit.Hosting.Golang.csproj" IsAspireProjectResource="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var builder = DistributedApplication.CreateBuilder(args);

var golang = builder.AddGolangApp("golang", "../gin-api");

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17247;http://localhost:15071",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21120",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22298"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15071",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19155",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20207"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

return builder;
}

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

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}

public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
54 changes: 54 additions & 0 deletions examples/golang/gin-api/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module main

go 1.22.3

require (
github.com/gin-gonic/gin v1.10.0
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.55.0
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0
go.opentelemetry.io/otel/metric v1.30.0
go.opentelemetry.io/otel/sdk v1.30.0
go.opentelemetry.io/otel/sdk/metric v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
google.golang.org/grpc v1.66.1
)

require (
github.com/bytedance/sonic v1.12.3 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/arch v0.10.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 5e88805

Please sign in to comment.