Skip to content

Commit

Permalink
Added Grafana and Loki for logging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed May 7, 2022
1 parent 3cdcabf commit 7cd9966
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,6 @@ src/AdminPanel/wwwroot/content/css/app.css
src/AdminPanel/wwwroot/content/css/app.min.css
src/AdminPanel/wwwroot/content/js/app.js
src/AdminPanel/wwwroot/content/js/app.js.map

# Data
.data/
3 changes: 3 additions & 0 deletions src/Dapr/Common/DaprService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// </copyright>

using Microsoft.AspNetCore.Hosting;
using Serilog;

namespace MUnique.OpenMU.Dapr.Common;

Expand Down Expand Up @@ -51,6 +52,8 @@ public static WebApplicationBuilder CreateBuilder(string serviceName, string[] a
c.SwaggerDoc("v1", new OpenApiInfo { Title = serviceName, Version = "v1" });
});

builder.UseLoki(serviceName);

return builder;
}
}
31 changes: 31 additions & 0 deletions src/Dapr/Common/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
namespace MUnique.OpenMU.Dapr.Common;

using System.Net;

using Serilog.Debugging;
using Serilog.Filters;
using Serilog;
using Serilog.Sinks.Grafana.Loki;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MUnique.OpenMU.Interfaces;
using MUnique.OpenMU.Network;
using MUnique.OpenMU.Persistence;
Expand Down Expand Up @@ -72,6 +78,31 @@ public static IServiceCollection PublishManageableServer<TServer>(this IServiceC
return services;
}

public static WebApplicationBuilder UseLoki(this WebApplicationBuilder builder, string serviceName)
{
// We just want to transmit some static labels, as suggested in the best practice in the Loki documentation
var includeLabels = new[] { "Account", "Character", "Connection", "ServiceName", "SourceContext" };

var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.WithProperty("ServiceName", serviceName)
.Enrich.FromLogContext()
.WriteTo
.GrafanaLoki(
uri: "http://loki:3100",
filtrationLabels: includeLabels,
filtrationMode: LokiLabelFiltrationMode.Include)
.Filter.ByExcluding(Matching.FromSource("Microsoft")) // We don't want all of the ASP.NET logging, because that really keeps loki pretty busy
.CreateLogger();

SelfLog.Enable(Console.Error);

builder.Host.ConfigureLogging((_, loggingBuilder) => loggingBuilder.ClearProviders());
builder.Host.UseSerilog(logger);
builder.Host.ConfigureLogging((_, loggingBuilder) => loggingBuilder.AddConsole());
return builder;
}

public static WebApplication BuildAndConfigure(this WebApplicationBuilder builder, bool addBlazor = false)
{
var pathBase = Environment.GetEnvironmentVariable("PATH_BASE");
Expand Down
2 changes: 2 additions & 0 deletions src/Dapr/Common/MUnique.OpenMU.Dapr.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc8" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0" />
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="7.1.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.376">
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions src/docker-compose.dcproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
<None Include=".htpasswd" />
<None Include="loki-config.yaml" />
<None Include="nginx.conf" />
</ItemGroup>
</Project>
62 changes: 62 additions & 0 deletions src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,68 @@ services:
- adminPanel
- gameServer1
- zipkin

grafana:
image: grafana/grafana:latest
container_name: grafana
environment:
- GF_PATHS_PROVISIONING=/etc/grafana/provisioning
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
depends_on:
- loki
entrypoint:
- sh
- -euc
- |
mkdir -p /etc/grafana/provisioning/datasources
cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
apiVersion: 1
datasources:
- name: Loki
type: loki
access: proxy
url: http://loki:3100
#jsonData:
# httpHeaderName1: "X-Scope-OrgID"
#secureJsonData:
# httpHeaderValue1: "tenant1"
EOF
/run.sh
ports:
- "3000:3000"

loki:
image: grafana/loki:2.5.0
container_name: loki
command: "-config.file=/etc/loki/config.yaml -target=all"
ports:
- "3100:3100"
- 7946
- 9095
volumes:
- ./loki-config.yaml:/etc/loki/config.yaml
depends_on:
- minio

minio:
image: minio/minio
entrypoint:
- sh
- -euc
- |
mkdir -p /data/loki-data && \
mkdir -p /data/loki-ruler && \
minio server /data
environment:
- MINIO_ACCESS_KEY=loki
- MINIO_SECRET_KEY=supersecret
- MINIO_PROMETHEUS_AUTH_TYPE=public
- MINIO_UPDATE=off
ports:
- 9000
volumes:
- ./.data/minio:/data

dapr-placement:
image: "daprio/dapr"
Expand Down
36 changes: 36 additions & 0 deletions src/loki-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
auth_enabled: false
server:
http_listen_port: 3100
memberlist:
join_members:
- loki:7946
schema_config:
configs:
- from: 2021-08-01
store: boltdb-shipper
object_store: s3
schema: v11
index:
prefix: index_
period: 24h
#limits_config:
# max_streams_per_user: 100000
common:
path_prefix: /loki
replication_factor: 1
storage:
s3:
endpoint: minio:9000
insecure: true
bucketnames: loki-data
access_key_id: loki
secret_access_key: supersecret
s3forcepathstyle: true
ring:
kvstore:
store: memberlist
ruler:
storage:
s3:
bucketnames: loki-ruler

0 comments on commit 7cd9966

Please sign in to comment.