Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Emulated PLC metrics #376

Merged
merged 11 commits into from
Jul 22, 2024
97 changes: 54 additions & 43 deletions src/Helpers/Metrics.cs → src/Helpers/MetricsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OpcPlc;
using System.Diagnostics.Metrics;
using System.Linq;

public static class MetricsConfig
public static class MetricsHelper
{
/// <summary>
/// The name of the service.
Expand All @@ -17,11 +17,14 @@ public static class MetricsConfig
/// </summary>
public static readonly Meter Meter = new(ServiceName);

private const string OPC_PLC_POD_COUNT_METRIC = "opc_plc_pod_count";
/// <summary>
/// Gets or sets whether the meter is enabled.
/// </summary>
public static bool IsEnabled { get; set; }

private const string OPC_PLC_SESSION_COUNT_METRIC = "opc_plc_session_count";
private const string OPC_PLC_SUBSCRIPTION_COUNT_METRIC = "opc_plc_subscription_count";
private const string OPC_PLC_MONITORED_ITEM_COUNT_METRIC = "opc_plc_monitored_item_count";
private const string OPC_PLC_PUBLISHED_COUNT_METRIC = "opc_plc_published_count";
private const string OPC_PLC_PUBLISHED_COUNT_WITH_TYPE_METRIC = "opc_plc_published_count_with_type";
private const string OPC_PLC_TOTAL_ERRORS_METRIC = "opc_plc_total_errors";

Expand Down Expand Up @@ -54,13 +57,11 @@ private static string SIMULATION_ID
{
try
{
var simulationId = Environment.GetEnvironmentVariable("SIMULATION_ID");
if (string.IsNullOrEmpty(simulationId))
{
return null;
}
string simulationId = Environment.GetEnvironmentVariable("SIMULATION_ID");

return simulationId;
return string.IsNullOrEmpty(simulationId)
? null
: simulationId;
}
catch (Exception)
{
Expand All @@ -75,14 +76,11 @@ private static string CLUSTER_NAME
{
try
{
var clusterName = Environment.GetEnvironmentVariable("DEPLOYMENT_NAME");
string clusterName = Environment.GetEnvironmentVariable("DEPLOYMENT_NAME");

if (string.IsNullOrEmpty(clusterName))
{
return null;
}

return clusterName;
return string.IsNullOrEmpty(clusterName)
? null
: clusterName;
}
catch (Exception)
{
Expand All @@ -91,7 +89,7 @@ private static string CLUSTER_NAME
}
}

private static readonly IDictionary<string, object> BaseDimensions = new Dictionary<string, object>
private static readonly IDictionary<string, object> _baseDimensions = new Dictionary<string, object>
{
{ "kubernetes_node", KUBERNETES_NODE ?? "node" },
{ "role_instance", ROLE_INSTANCE ?? "host" },
Expand All @@ -100,81 +98,94 @@ private static string CLUSTER_NAME
{ "cluster", CLUSTER_NAME ?? "cluster" },
};

private static readonly UpDownCounter<int> SessionCount = Meter.CreateUpDownCounter<int>(OPC_PLC_SESSION_COUNT_METRIC);
private static readonly UpDownCounter<int> SubscriptionCount = Meter.CreateUpDownCounter<int>(OPC_PLC_SUBSCRIPTION_COUNT_METRIC);
private static readonly UpDownCounter<int> MonitoredItemCount = Meter.CreateUpDownCounter<int>(OPC_PLC_MONITORED_ITEM_COUNT_METRIC);
private static readonly Counter<int> PublishedCount = Meter.CreateCounter<int>(OPC_PLC_PUBLISHED_COUNT_METRIC);
private static readonly Counter<int> PublishedCountWithType = Meter.CreateCounter<int>(OPC_PLC_PUBLISHED_COUNT_WITH_TYPE_METRIC);
private static readonly Counter<int> TotalErrors = Meter.CreateCounter<int>(OPC_PLC_TOTAL_ERRORS_METRIC);

private static readonly ObservableGauge<int> PodCountGauge = Meter.CreateObservableGauge<int>(OPC_PLC_POD_COUNT_METRIC, () => {
return new Measurement<int>(1, ConvertDictionaryToKeyVaultPairArray(BaseDimensions));
});
private static readonly UpDownCounter<int> _sessionCount = Meter.CreateUpDownCounter<int>(OPC_PLC_SESSION_COUNT_METRIC);
private static readonly UpDownCounter<int> _subscriptionCount = Meter.CreateUpDownCounter<int>(OPC_PLC_SUBSCRIPTION_COUNT_METRIC);
private static readonly UpDownCounter<int> _monitoredItemCount = Meter.CreateUpDownCounter<int>(OPC_PLC_MONITORED_ITEM_COUNT_METRIC);
private static readonly Counter<int> _publishedCountWithType = Meter.CreateCounter<int>(OPC_PLC_PUBLISHED_COUNT_WITH_TYPE_METRIC);
private static readonly Counter<int> _totalErrors = Meter.CreateCounter<int>(OPC_PLC_TOTAL_ERRORS_METRIC);

/// <summary>
/// Add a session count.
/// </summary>
public static void AddSessionCount(string sessionId, int delta = 1)
{
if (!IsEnabled)
{
return;
}

var dimensions = MergeWithBaseDimensions(new KeyValuePair<string, object>("session", sessionId));
SessionCount.Add(delta, dimensions);
_sessionCount.Add(delta, dimensions);
}

/// <summary>
/// Add a subscription count.
/// </summary>
public static void AddSubscriptionCount(string sessionId, string subscriptionId, int delta = 1)
{
if (!IsEnabled)
{
return;
}

var dimensions = MergeWithBaseDimensions(
new KeyValuePair<string, object>("session", sessionId),
new KeyValuePair<string, object>("subscription", subscriptionId));

SubscriptionCount.Add(delta, dimensions);
_subscriptionCount.Add(delta, dimensions);
}

/// <summary>
/// Add a monitored item count.
/// </summary>
public static void AddMonitoredItemCount(string sessionId, string subscriptionId, int delta = 1)
public static void AddMonitoredItemCount(int delta = 1)
{
var dimensions = MergeWithBaseDimensions(
new KeyValuePair<string, object>("session", sessionId),
new KeyValuePair<string, object>("subscription", subscriptionId));
MonitoredItemCount.Add(delta, dimensions);
if (!IsEnabled)
{
return;
}

_monitoredItemCount.Add(delta, ConvertDictionaryToKeyVaultPairArray(_baseDimensions));
}

/// <summary>
/// Add a published count.
/// </summary>
public static void AddPublishedCount(string sessionId, string subscriptionId, int dataPoints, int events)
{
var dimensions = ConvertDictionaryToKeyVaultPairArray(BaseDimensions);
PublishedCount.Add(1, dimensions);
if (!IsEnabled)
{
return;
}

if (dataPoints > 0)
{
var dataPointsDimensions = MergeWithBaseDimensions(
new KeyValuePair<string, object>("type", "data_point"));
PublishedCountWithType.Add(dataPoints, dataPointsDimensions);;
_publishedCountWithType.Add(dataPoints, dataPointsDimensions);
}

if (events > 0)
{
var eventsDimensions = MergeWithBaseDimensions(
new KeyValuePair<string, object>("type", "event"));
PublishedCountWithType.Add(events, eventsDimensions);
_publishedCountWithType.Add(events, eventsDimensions);
}
}

/// <summary>
/// Record total errors.
/// </summary>
public static void RecordTotalErrors(string operation, string errorType, int delta = 1)
public static void RecordTotalErrors(string operation, int delta = 1)
{
if (!IsEnabled)
{
return;
}

var dimensions = MergeWithBaseDimensions(
new KeyValuePair<string, object>("operation", operation),
new KeyValuePair<string, object>("error_type", errorType));
TotalErrors.Add(delta, dimensions);
new KeyValuePair<string, object>("operation", operation));
_totalErrors.Add(delta, dimensions);
}

/// <summary>
Expand All @@ -190,7 +201,7 @@ private static KeyValuePair<string, object>[] ConvertDictionaryToKeyVaultPairArr
/// </summary>
private static KeyValuePair<string, object>[] MergeWithBaseDimensions(params KeyValuePair<string, object>[] items)
{
var newDimensions = new Dictionary<string, object>(BaseDimensions);
var newDimensions = new Dictionary<string, object>(_baseDimensions);
foreach (var item in items)
{
newDimensions[item.Key] = item.Value;
Expand Down
5 changes: 2 additions & 3 deletions src/Helpers/OtelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace OpcPlc.Helpers;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Extensions.Hosting;

using System;

Expand All @@ -30,8 +29,8 @@ public static void ConfigureOpenTelemetry(string serviceName, string exportEndpo

_ = Sdk.CreateMeterProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService(MetricsConfig.ServiceName).AddTelemetrySdk())
.AddMeter(MetricsConfig.Meter.Name)
.AddService(MetricsHelper.ServiceName).AddTelemetrySdk())
.AddMeter(MetricsHelper.Meter.Name)
.AddRuntimeInstrumentation()
.AddOtlpExporter((exporterOptions, metricsReaderOptions) => {
exporterOptions.Endpoint = new Uri(exportEndpointUri);
Expand Down
Loading
Loading