Skip to content

Commit

Permalink
Merge pull request #36 from granstel/metrics
Browse files Browse the repository at this point in the history
Metrics
  • Loading branch information
granstel authored Feb 16, 2023
2 parents a0f919f + 39c7a3e commit 9c4ec73
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/FillInTheTextBot.Api/FillInTheTextBot.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<Version>1.21.0</Version>
<Version>1.22.0</Version>
<PackageReleaseNotes>Optimized interaction with Dialogflow</PackageReleaseNotes>
</PropertyGroup>

Expand Down
41 changes: 0 additions & 41 deletions src/FillInTheTextBot.Api/Middleware/MetricsMiddleware.cs

This file was deleted.

6 changes: 5 additions & 1 deletion src/FillInTheTextBot.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
"стоп",
"закончить",
"выйти",
"выход"
"выход",
"заткнись дура",
"заткнись, дура",
"алиса пока",
"алиса, пока"
]
}
}
Expand Down
1 change: 0 additions & 1 deletion src/FillInTheTextBot.Messengers.Yandex/YandexService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using FillInTheTextBot.Services;
using Microsoft.Extensions.Logging;
Expand Down
2 changes: 2 additions & 0 deletions src/FillInTheTextBot.Messengers/MessengerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public virtual async Task<TOutput> ProcessIncomingAsync(TInput input)
}
}
};

MetricsCollector.Increment("ErrorAnswer", string.Empty);
}

using (Tracing.Trace(operationName: "AfterAsync"))
Expand Down
10 changes: 6 additions & 4 deletions src/FillInTheTextBot.Services/ConversationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace FillInTheTextBot.Services
{
public class ConversationService : IConversationService
{
private static readonly Random Random = new Random();
private static readonly Random Random = new();

private readonly ConversationConfiguration _configuration;
private readonly IDialogflowService _dialogflowService;
Expand Down Expand Up @@ -129,7 +129,7 @@ private async Task<Response> GetText(Request request, string startText, string t
}

var eventName = $"event:{textKey}";

MetricsCollector.Increment("event", textKey);

var dialog = await _dialogflowService.GetResponseAsync(eventName, request.SessionId, request.ScopeKey);

Expand Down Expand Up @@ -303,8 +303,10 @@ private async Task<Response> TryGetAnswerForCancelsSlotFilling(bool? isCancelsSl
{
return response;
}

const string eventName = $"event:CancelsSlotFilling";

const string cancelsSlotFilling = "CancelsSlotFilling";
const string eventName = $"event:{cancelsSlotFilling}";
MetricsCollector.Increment("event", cancelsSlotFilling);

var cancelsSlotFillingDialog = await _dialogflowService.GetResponseAsync(eventName, sessionId, scopeKey);

Expand Down
12 changes: 4 additions & 8 deletions src/FillInTheTextBot.Services/DialogflowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using InternalModels = FillInTheTextBot.Models;
using Microsoft.Extensions.Logging;
using FillInTheTextBot.Services.Mapping;
using Prometheus;

namespace FillInTheTextBot.Services
{
Expand Down Expand Up @@ -38,8 +37,6 @@ public class DialogflowService : IDialogflowService

private readonly Dictionary<InternalModels.Source, Func<InternalModels.Request, string, EventInput>> _eventResolvers;

private readonly Gauge _statistics;

public DialogflowService(
ILogger<DialogflowService> log,
ScopesSelector<SessionsClient> sessionsClientSelector,
Expand All @@ -55,9 +52,6 @@ public DialogflowService(
{InternalModels.Source.Sber, DefaultWelcomeEventResolve},
{InternalModels.Source.Marusia, DefaultWelcomeEventResolve}
};

_statistics = Metrics
.CreateGauge("statistics", "Statistics", "statistic_name", "parameter");
}

public async Task<InternalModels.Dialog> GetResponseAsync(string text, string sessionId, string scopeKey)
Expand Down Expand Up @@ -97,7 +91,7 @@ public Task SetContextAsync(string sessionId, string scopeKey, string contextNam
{
using (Tracing.Trace(s => s.WithTag(nameof(context.ScopeId), context.ScopeId), "Get response from Dialogflow"))
{
_statistics.WithLabels("dialogflow_DetectIntent_scope", context.ScopeId).Inc();
MetricsCollector.Increment("dialogflow_DetectIntent_scope", context.ScopeId);

context.TryGetParameterValue(nameof(DialogflowConfiguration.ProjectId), out string projectId);
context.TryGetParameterValue(nameof(DialogflowConfiguration.LanguageCode), out string languageCode);
Expand All @@ -118,6 +112,8 @@ public Task SetContextAsync(string sessionId, string scopeKey, string contextNam

var queryResult = intentResponse.QueryResult;

MetricsCollector.Increment("intent", queryResult.Intent.DisplayName);

var response = queryResult.ToDialog();

response.ScopeKey = context.ScopeId;
Expand All @@ -137,7 +133,7 @@ private Task SetContextInternalAsync(ContextsClient client, string sessionId, Sc

var context = GetContext(projectId, region, session, contextName, lifeSpan, parameters);

_statistics.WithLabels("dialogflow_CreateContext_scope", scopeContext.ScopeId).Inc();
MetricsCollector.Increment("dialogflow_CreateContext_scope", scopeContext.ScopeId);

return client.CreateContextAsync(session, context);
}
Expand Down
19 changes: 19 additions & 0 deletions src/FillInTheTextBot.Services/MetricsCollector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Prometheus;

namespace FillInTheTextBot.Services;

public static class MetricsCollector
{
private static readonly Gauge Metrics;

static MetricsCollector()
{
Metrics = Prometheus.Metrics
.CreateGauge("metrics", "Custom metrics", "metric_name", "parameter");
}

public static void Increment(string key, string value)
{
Metrics.WithLabels(key, value).Inc();
}
}

0 comments on commit 9c4ec73

Please sign in to comment.