Skip to content

Commit

Permalink
Add configuration for tracing log (Green-Software-Foundation#531)
Browse files Browse the repository at this point in the history
Signed-off-by: Yasumasa Suenaga <[email protected]>
  • Loading branch information
YaSuenag authored Nov 5, 2024
1 parent ead82a2 commit b502723
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 20 deletions.
10 changes: 10 additions & 0 deletions casdk-docs/docs/tutorial-extras/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ InstrumentationKey. For more details, please refer to
AppInsights_InstrumentationKey="AppInsightsInstrumentationKey"
```

#### Configuring telemetry log

WebAPI configures [console exporter of OpenTelemetry](https://opentelemetry.io/docs/languages/net/exporters/#console) by default.
You can configure whether the exporter is enabled with `EnableTelemetryLogging`.
Set `false` if you want to reduce the log.

```bash
CarbonAwareVars__EnableTelemetryLogging=false
```

### Prometheus exporter for emissions data

> DISCLAIMER: The `/metrics` Prometheus exporter is currently unsupported, and is used for internal GSF needs, and may change in the future. It will retrieve _all_ emissions data and create heavy load on your data API's. It is turned off by default.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Extensions.Options;
using CarbonAware.WebApi.Metrics;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace CarbonAware.WebApi.Configuration;

Expand Down Expand Up @@ -32,7 +34,23 @@ public static void AddMonitoringAndTelemetry(this IServiceCollection services, I
// Can be extended in the future to support a different provider like Zipkin, Prometheus etc
}

var enableTelemetryLogging = envVars?.EnableTelemetryLogging ?? true;
if (enableTelemetryLogging)
{
const string serviceName = "CarbonAware.WebAPI";
const string serviceVersion = "1.0.0";

services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddConsoleExporter()
.AddSource(serviceName)
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName: serviceName, serviceVersion: serviceVersion))
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation());
}
}

public static IServiceCollection AddCarbonExporter(this IServiceCollection services, IConfiguration configuration)
Expand Down
18 changes: 0 additions & 18 deletions src/CarbonAware.WebApi/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,11 @@
using GSF.CarbonAware.Configuration;
using GSF.CarbonAware.Exceptions;
using Microsoft.OpenApi.Models;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Metrics;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;

// Define constants to initialize tracing with
var serviceName = "CarbonAware.WebAPI";
var serviceVersion = "1.0.0";

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddConsoleExporter()
.AddSource(serviceName)
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName: serviceName, serviceVersion: serviceVersion))
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation());

// Add services to the container.
builder.Services.AddControllers(options =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using NUnit.Framework.Internal;
using OpenTelemetry.Trace;
using Microsoft.ApplicationInsights;

namespace CarbonAware.WepApi.UnitTests;

Expand Down Expand Up @@ -79,8 +81,8 @@ public void AddMonitoringAndTelemetry_DoesNotAddServices_WithoutTelemetryProvide

// Act & Assert
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration));
Assert.That(services.Count, Is.EqualTo(0));
}
Assert.Null(services.BuildServiceProvider().GetService<TelemetryClient>());
}

[Test]
public void AddCarbonExporter_AddsServices_IsEnabledInConfiguration()
Expand Down Expand Up @@ -156,4 +158,58 @@ public void CreateConsoleLogger_ReturnsILogger()
// Assert
Assert.That(logger, Is.Not.Null);
}

[Test]
public void EnableTelemetryLogging_AddsServices_WithoutConfiguration()
{
// Arrange
var services = new ServiceCollection();

var inMemorySettings = new Dictionary<string, string> { };
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();

// Act & Assert
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration));
Assert.NotNull(services.BuildServiceProvider().GetService<TracerProvider>());
}

[Test]
public void EnableTelemetryLogging_AddsServices_IsEnabledInConfiguration()
{
// Arrange
var services = new ServiceCollection();

var inMemorySettings = new Dictionary<string, string>
{
{ "CarbonAwareVars:EnableTelemetryLogging", "true" }
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();

// Act & Assert
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration));
Assert.NotNull(services.BuildServiceProvider().GetService<TracerProvider>());
}

[Test]
public void EnableTelemetryLogging_AddsServices_IsDisabledInConfiguration()
{
// Arrange
var services = new ServiceCollection();

var inMemorySettings = new Dictionary<string, string>
{
{ "CarbonAwareVars:EnableTelemetryLogging", "false" }
};
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();

// Act & Assert
Assert.DoesNotThrow(() => services.AddMonitoringAndTelemetry(configuration));
Assert.Null(services.BuildServiceProvider().GetService<TracerProvider>());
}
}
2 changes: 2 additions & 0 deletions src/CarbonAware/src/CarbonAwareVariablesConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ internal class CarbonAwareVariablesConfiguration

public string TelemetryProvider { get; set; }

public Boolean EnableTelemetryLogging { get; set; }

public Boolean EnableCarbonExporter { get;set; }

public Boolean VerboseApi {get; set;}
Expand Down

0 comments on commit b502723

Please sign in to comment.