Skip to content

Golang Integration #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a7f24c3
adds support for golang
tommasodotNET Oct 1, 2024
18e368d
adds example for go usage
tommasodotNET Oct 1, 2024
089851d
adds tracing to go example
tommasodotNET Oct 1, 2024
5ed08ca
simplifies tracing using otel middleware only
tommasodotNET Oct 2, 2024
2307d15
refactors GolangAppExecutableResource
tommasodotNET Oct 2, 2024
9527997
moves hosting-golang md and improves doc
tommasodotNET Oct 2, 2024
72a34ab
adds testing for golang apphost
tommasodotNET Oct 2, 2024
a0417fb
improves test for AppHost golang
tommasodotNET Oct 3, 2024
336aa59
adds test for golang executable creation
tommasodotNET Oct 3, 2024
625d196
adds go setup in cicd
tommasodotNET Oct 3, 2024
3d9a578
adds go to the devcontainer
tommasodotNET Oct 3, 2024
fdfa20d
adds go extension to devcontainer
tommasodotNET Oct 3, 2024
731d5c9
updates golang version
tommasodotNET Oct 3, 2024
ebb3af1
fixes unused reference
tommasodotNET Oct 7, 2024
1973e2f
Merge pull request #72 from tommasodotNET/issue43
tommasodotNET Oct 7, 2024
4dc3c13
adds set up go to all workflows
tommasodotNET Oct 8, 2024
1c6bfe3
Update .devcontainer/devcontainer.json
tommasodotNET Oct 8, 2024
0fa2e5b
Merge pull request #76 from tommasodotNET/issue43
tommasodotNET Oct 8, 2024
5616cd6
Merge branch 'main' into issue43
tommasodotNET Oct 8, 2024
d42a731
avoid using path normalizer
tommasodotNET Oct 8, 2024
f65bd7f
Merge pull request #80 from tommasodotNET/issue43
tommasodotNET Oct 8, 2024
6dad4de
uses pathnormalizer and combine
tommasodotNET Oct 8, 2024
fdb4a4f
Merge branch 'issue43' of github.com:tommasodotNET/Aspire into issue43
tommasodotNET Oct 8, 2024
e620836
Merge pull request #81 from tommasodotNET/issue43
tommasodotNET Oct 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading