Skip to content

Commit

Permalink
add otel support
Browse files Browse the repository at this point in the history
  • Loading branch information
gevorg.kesyan committed Jan 29, 2024
1 parent 503b071 commit 9111c18
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 153 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ There is a full description of section that is needed in appsettings.json.
},
"EnableMetrics": true,
"Metrics": {
"MapPath": "/metrics",
"Port": 82,
"UseDefaultCollectors": false,
"LogsWriteFailCounterName": "logs_write_fail_counter",
"LogsWriteSuccessCounterName": "logs_write_success_counter",
"LogsSizeKbCounterName": "logs_size_kb_counter"
Expand Down Expand Up @@ -107,11 +104,9 @@ There is a full description of section that is needed in appsettings.json.
---

If you need to enable metrics provide the additional section.
You already need to add OpenTelemetry nuget packages and register services in Startup.cs.
- `EnableMetrics` (default = false) - if `true` turn on metrics exporting
- `Metrics` - could be fulfilled if `EnableMetrics: true`
- `MapPath` (default = "/metrics") - endpoint for exporting metrics
- `Port` (default = null) - port for exporting metrics
- `UseDefaultCollectors` (default = false) - if `true` use default Prometheus collectors in addition
- `LogsWriteFailCounterName` (default = 'logs_write_fail_counter') - metric counter name for exporting (broken logs)
- `LogsWriteSuccessCounterName` (default = 'logs_write_success_counter') - metric counter name for exporting (correct logs)
- `LogsSizeKbCounterName` (default = 'logs_size_kb_counter') - metric counter name for exporting (correct logs size in kb)
Expand Down Expand Up @@ -156,9 +151,19 @@ services.AddDirectToLokiLogging(configuration, c =>
c.BaseAddress = new Uri("http://loki.net:3100"));
```

3) `Optional`: if it is needed to skip logs in your RequestResponseLoggingMiddleware use
3) `Optional (1)`: if it is needed to skip logs in your RequestResponseLoggingMiddleware use
`RequestResponseLoggingFilter(httpContext)` method like an aid in determining which logs should be skipped.

4) `Optional (2)` if you need to export logs metrics, you should add
```
services.AddOpenTelemetry()
.WithMetrics(builder => builder
.AddPrometheusExporter(...)
.AddDirectLokiLoggingMeter();
);
```
in startup.cs file.



## Usage
Expand Down
24 changes: 24 additions & 0 deletions samples/AspNetCore.WebApi/MetricsStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

namespace WebApi;

internal sealed class MetricsStartup : IStartupFilter
{
private readonly IServiceProvider _serviceProvider;

public MetricsStartup(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseOpenTelemetryPrometheusScrapingEndpoint();
next(app);
};
}
}
13 changes: 13 additions & 0 deletions samples/AspNetCore.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
using Aerx.Serilog.Sinks.Loki.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using WebApi;
using WebApi.Models;

Expand All @@ -17,6 +21,15 @@
c.Timeout = TimeSpan.FromSeconds(5);
});

builder.Services.AddMetrics();
builder.Services.AddOpenTelemetry()
.WithMetrics(b =>
{
b.AddPrometheusExporter(options => options.ScrapeEndpointPath = "/metrics")
.AddDirectLokiLoggingMeter();
});

builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IStartupFilter, MetricsStartup>());
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();

Expand Down
10 changes: 9 additions & 1 deletion samples/AspNetCore.WebApi/WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics" Version="8.0.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.7.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="0.5.0-beta.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Prometheus.Client" Version="5.2.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.5.0" />
Expand Down
4 changes: 2 additions & 2 deletions samples/AspNetCore.WebApi/appsettings.Prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
},
"Loki": {
"AppName": "web-app",
"BatchPostingLimit": 100,
"ParallelismFactor": 4,
"BatchPostingLimit": 10,
"ParallelismFactor": 1,
"Labels": {
"app": "web-app"
},
Expand Down
5 changes: 3 additions & 2 deletions src/Aerx.Serilog.Sinks.Loki/Aerx.Serilog.Sinks.Loki.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.7.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Prometheus.Client" Version="5.2.0" />
<PackageReference Include="Prometheus.Client.Abstractions" Version="5.2.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.3.2" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 10 additions & 16 deletions src/Aerx.Serilog.Sinks.Loki/Extensions/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
using Aerx.Serilog.Sinks.Loki.Logger;
using Aerx.Serilog.Sinks.Loki.Metrics;
using Aerx.Serilog.Sinks.Loki.Options;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Prometheus.Client;
using Prometheus.Client.Collectors;
using OpenTelemetry.Metrics;
using Serilog.Formatting;
using Serilog.Formatting.Json;

Expand Down Expand Up @@ -82,23 +80,19 @@ public static IServiceCollection AddDirectToLokiLogging(this IServiceCollection
services.TryAddSingleton<LokiHttpClientPooledObjectPolicy>();
services.TryAddSingleton<LokiSink>();
services.TryAddSingleton<ILoggerProvider, DirectLogToLokiLoggerProvider>();

var registry = new CollectorRegistry();
var factory = new MetricFactory(registry);

services.TryAddSingleton<ICollectorRegistry>(registry);
services.TryAddSingleton<IMetricFactory>(factory);
services.TryAddSingleton<IMetricService, MetricService>();

var enableMetrics = configuration.GetValue<bool>($"{Constants.Loki}:{Constants.EnableMetrics}");
if (enableMetrics)
{
services.TryAddEnumerable(ServiceDescriptor.Transient<IStartupFilter, MetricsStartup>());
}
services.TryAddSingleton<IMeterService, MeterService>();

return services;
}

/// <summary>
/// Add metrics for open telemetry
/// </summary>
/// <param name="builder">Meter provider from OpenTelemetry lib</param>
/// <returns></returns>
public static MeterProviderBuilder AddDirectLokiLoggingMeter(this MeterProviderBuilder builder) =>
builder.AddMeter(MeterService.MeterName);

/// <summary>
/// Add direct to loki logging
/// Required properties in configuration
Expand Down
85 changes: 0 additions & 85 deletions src/Aerx.Serilog.Sinks.Loki/Extensions/MetricsStartup.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Aerx.Serilog.Sinks.Loki/HttpClient/LokiGzipHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Aerx.Serilog.Sinks.Loki.HttpClient;

public class LokiGzipHttpClient : ILokiHttpClient
public sealed class LokiGzipHttpClient : ILokiHttpClient
{
private static readonly MediaTypeHeaderValue ContentType = MediaTypeHeaderValue.Parse(Constants.JsonContentType);
private static readonly JsonSerializerSettings CamelCase = new()
Expand Down Expand Up @@ -58,7 +58,7 @@ public async Task<LokiPushResponse> Push(LokiBatch batch)
return result;
}

public virtual ILokiHttpClient SetTenantId(string tenantId)
public ILokiHttpClient SetTenantId(string tenantId)
{
if (string.IsNullOrEmpty(tenantId) || _httpClient.DefaultRequestHeaders.Contains(Constants.TenantIdHeader))
{
Expand Down
10 changes: 5 additions & 5 deletions src/Aerx.Serilog.Sinks.Loki/Logger/LokiSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public LokiSink(
ITextFormatter textFormatter,
ILokiBatchFormatter batchFormatter,
LokiHttpClientPooledObjectPolicy pooledObjectPolicy,
IMetricService metricService)
IMeterService meterService)
{
var pool = new DefaultObjectPoolProvider
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public LokiSink(
}
catch (Exception e)
{
metricService.ObserveOnLogsWriteFail(messages.Length);
meterService.ObserveOnLogsWriteFail(messages.Length);
SelfLog.WriteLine($"Transformation block exception: {e}");
return new LokiBatch();
}
Expand Down Expand Up @@ -93,13 +93,13 @@ public LokiSink(
}
else
{
metricService.ObserveOnLogsWriteSuccess(batchSize);
metricService.ObserveOnLogsBatchSize(result.ContentSizeInKb);
meterService.ObserveOnLogsWriteSuccess(batchSize);
meterService.ObserveOnLogsBatchSize(result.ContentSizeInKb);
}
}
catch (Exception)
{
metricService.ObserveOnLogsWriteFail(batchSize);
meterService.ObserveOnLogsWriteFail(batchSize);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Aerx.Serilog.Sinks.Loki.Metrics;

public interface IMetricService
public interface IMeterService
{
void ObserveOnLogsWriteFail(int failLogsCount);

Expand Down
Loading

0 comments on commit 9111c18

Please sign in to comment.