-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from CommunityToolkit/issue43
Golang Integration
- Loading branch information
Showing
24 changed files
with
895 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
20 changes: 20 additions & 0 deletions
20
...unityToolkit.Hosting.Golang.AppHost/Aspire.CommunityToolkit.Hosting.Golang.AppHost.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
5 changes: 5 additions & 0 deletions
5
examples/golang/Aspire.CommunityToolkit.Hosting.Golang.AppHost/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
29 changes: 29 additions & 0 deletions
29
...ples/golang/Aspire.CommunityToolkit.Hosting.Golang.AppHost/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
examples/golang/Aspire.CommunityToolkit.Hosting.Golang.AppHost/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ting.Golang.ServiceDefaults/Aspire.CommunityToolkit.Hosting.Golang.ServiceDefaults.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
111 changes: 111 additions & 0 deletions
111
examples/golang/Aspire.CommunityToolkit.Hosting.Golang.ServiceDefaults/Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.