Skip to content

Commit

Permalink
Allow disabling of resource monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcschier committed Jun 25, 2024
1 parent d2074f7 commit 926a31b
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,9 @@ public CommandLine(string[] args, CommandLineLogger? logger = null)
{ $"pd|diagnosticstarget=|{PublisherConfig.DiagnosticsTargetKey}=",
$"Configures how to emit diagnostics information at the `--di` configured interval.\nUse this to for example emit diagnostics as events to the event topic template instead of the console.\nAllowed values:\n `{string.Join("`\n `", Enum.GetNames(typeof(PublisherDiagnosticTargetType)))}`\nDefault: `{PublisherDiagnosticTargetType.Logger}`.\n",
(PublisherDiagnosticTargetType d) => this[PublisherConfig.DiagnosticsTargetKey] = d.ToString() },
{ $"sl|opcstacklogging:|{OpcUaClientConfig.EnableOpcUaStackLoggingKey}:",
"Enable opc ua stack logging beyond logging at error level.\nDefault: `disabled`.\n",
(bool? b) => this[OpcUaClientConfig.EnableOpcUaStackLoggingKey] = b?.ToString() ?? "True" },
{ $"dr|disableresourcemonitoring:|{PublisherConfig.DisableResourceMonitoringKey}:",
$"Disable resource monitoring as part of the diagnostics output and metrics.\nDefault: `false` (enabled).\n",
(bool? b) => this[PublisherConfig.DisableResourceMonitoringKey] = b?.ToString() ?? "True" },
{ "ln|lognotifications:",
"Log ingress subscription notifications at Informational level to aid debugging.\nDefault: `disabled`.\n",
(bool? b) => this[PublisherConfig.DebugLogNotificationsKey] = b?.ToString() ?? "True" },
Expand All @@ -547,6 +547,9 @@ public CommandLine(string[] args, CommandLineLogger? logger = null)
{ "len|logencodednotifications:",
"Log encoded subscription and monitored item notifications at Informational level to aid debugging.\nDefault: `disabled`.\n",
(bool? b) => this[PublisherConfig.DebugLogEncodedNotificationsKey] = b?.ToString() ?? "True" },
{ $"sl|opcstacklogging:|{OpcUaClientConfig.EnableOpcUaStackLoggingKey}:",
"Enable opc ua stack logging beyond logging at error level.\nDefault: `disabled`.\n",
(bool? b) => this[OpcUaClientConfig.EnableOpcUaStackLoggingKey] = b?.ToString() ?? "True" },
{ $"ecw|enableconsolewriter:|{Configuration.ConsoleWriter.EnableKey}:",
"Enable writing encoded messages to standard error log through the filesystem transport (must enable via `-t FileSystem` and `-o` must be set to either `stderr` or `stdout`).\nDefault: `false`.\n",
(bool? b) => this[Configuration.ConsoleWriter.EnableKey] = b?.ToString() ?? "True" },
Expand Down
15 changes: 15 additions & 0 deletions src/Azure.IIoT.OpcUa.Publisher.Module/src/Runtime/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ public static void AddPublisherServices(this ContainerBuilder builder)
.AsImplementedInterfaces();
}

/// <summary>
/// Add resource monitoring
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
public static void AddResourceMonitoring(this IServiceCollection services,
IConfiguration configuration)
{
var publisherOptions = new PublisherConfig(configuration).ToOptions();
if (publisherOptions.Value.DisableResourceMonitoring != true)
{
services.AddResourceMonitoring();
}
}

/// <summary>
/// Add mqtt client
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Azure.IIoT.OpcUa.Publisher.Module/src/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void ConfigureServices(IServiceCollection services)
;

services.AddHttpClient();
services.AddResourceMonitoring();
services.AddResourceMonitoring(Configuration);

services.AddRouting();
services.AddHealthChecks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.ResourceMonitoring" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.ResourceMonitoring" Version="8.6.0" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="Furly.Azure.IoT.Edge" Version="1.0.52" />
<PackageReference Include="Irony" Version="1.5.1" />
Expand Down
2 changes: 2 additions & 0 deletions src/Azure.IIoT.OpcUa.Publisher/src/Runtime/PublisherConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public sealed class PublisherConfig : PostConfigureOptionBase<PublisherOptions>
public const string PublishMessageSchemaKey = "PublishMessageSchema";
public const string PreferAvroOverJsonSchemaKey = "PreferAvroOverJsonSchema";
public const string SchemaNamespaceKey = "SchemaNamespace";
public const string DisableResourceMonitoringKey = "DisableResourceMonitoring";
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

/// <summary>
Expand Down Expand Up @@ -129,6 +130,7 @@ public sealed class PublisherConfig : PostConfigureOptionBase<PublisherOptions>
/// <inheritdoc/>
public override void PostConfigure(string? name, PublisherOptions options)
{
options.DisableResourceMonitoring ??= GetBoolOrNull(DisableResourceMonitoringKey);
options.PublisherId ??= GetStringOrDefault(PublisherIdKey,
_identity?.Identity ?? Dns.GetHostName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,10 @@ public sealed class PublisherOptions
/// enabled.
/// </summary>
public SchemaOptions? SchemaOptions { get; set; }

/// <summary>
/// Disable resource monitoring
/// </summary>
public bool? DisableResourceMonitoring { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public sealed class PublisherDiagnosticCollector : IDiagnosticCollector,
/// <param name="resources"></param>
/// <param name="logger"></param>
/// <param name="timeProvider"></param>
public PublisherDiagnosticCollector(IResourceMonitor resources,
ILogger<PublisherDiagnosticCollector> logger, TimeProvider? timeProvider = null)
public PublisherDiagnosticCollector(ILogger<PublisherDiagnosticCollector> logger,
IResourceMonitor? resources = null, TimeProvider? timeProvider = null)
{
_resources = resources;
_logger = logger;
Expand Down Expand Up @@ -76,26 +76,37 @@ public bool TryGetDiagnosticsForWriterGroup(string writerGroupId,
//
_meterListener.RecordObservableInstruments();
var duration = _timeProvider.GetUtcNow() - value.AggregateModel.IngestionStart;
var resources = _resources.GetUtilization(TimeSpan.FromSeconds(5));

diagnostic = value.AggregateModel with
if (_resources != null)
{
IngestionDuration = duration,
MemoryUsedPercentage =
resources.MemoryUsedPercentage,
MemoryUsedInBytes =
resources.MemoryUsedInBytes,
CpuUsedPercentage =
resources.CpuUsedPercentage,
GuaranteedCpuUnits =
resources.SystemResources.GuaranteedCpuUnits,
MaximumCpuUnits =
resources.SystemResources.MaximumCpuUnits,
GuaranteedMemoryInBytes =
resources.SystemResources.GuaranteedMemoryInBytes,
MaximumMemoryInBytes =
resources.SystemResources.MaximumMemoryInBytes
};
var resources = _resources.GetUtilization(TimeSpan.FromSeconds(5));

diagnostic = value.AggregateModel with
{
IngestionDuration = duration,
MemoryUsedPercentage =
resources.MemoryUsedPercentage,
MemoryUsedInBytes =
resources.MemoryUsedInBytes,
CpuUsedPercentage =
resources.CpuUsedPercentage,
GuaranteedCpuUnits =
resources.SystemResources.GuaranteedCpuUnits,
MaximumCpuUnits =
resources.SystemResources.MaximumCpuUnits,
GuaranteedMemoryInBytes =
resources.SystemResources.GuaranteedMemoryInBytes,
MaximumMemoryInBytes =
resources.SystemResources.MaximumMemoryInBytes
};
}
else
{
diagnostic = value.AggregateModel with
{
IngestionDuration = duration
};
}
return true;
}
diagnostic = default;
Expand All @@ -112,28 +123,39 @@ public bool TryGetDiagnosticsForWriterGroup(string writerGroupId,
.Select(kv => (kv.Key, kv.Value.AggregateModel)))
{
var duration = now - info.IngestionStart;
var resources = _resources.GetUtilization(TimeSpan.FromSeconds(5));

yield return (writerGroupId, info with
if (_resources == null)
{
yield return (writerGroupId, info with
{
Timestamp = now,
IngestionDuration = duration
});
}
else
{
Timestamp = now,
IngestionDuration = duration,
var resources = _resources.GetUtilization(TimeSpan.FromSeconds(5));
yield return (writerGroupId, info with
{
Timestamp = now,
IngestionDuration = duration,

MemoryUsedPercentage =
resources.MemoryUsedPercentage,
MemoryUsedInBytes =
resources.MemoryUsedInBytes,
CpuUsedPercentage =
resources.CpuUsedPercentage,
GuaranteedCpuUnits =
resources.SystemResources.GuaranteedCpuUnits,
MaximumCpuUnits =
resources.SystemResources.MaximumCpuUnits,
GuaranteedMemoryInBytes =
resources.SystemResources.GuaranteedMemoryInBytes,
MaximumMemoryInBytes =
resources.SystemResources.MaximumMemoryInBytes
});
MemoryUsedPercentage =
resources.MemoryUsedPercentage,
MemoryUsedInBytes =
resources.MemoryUsedInBytes,
CpuUsedPercentage =
resources.CpuUsedPercentage,
GuaranteedCpuUnits =
resources.SystemResources.GuaranteedCpuUnits,
MaximumCpuUnits =
resources.SystemResources.MaximumCpuUnits,
GuaranteedMemoryInBytes =
resources.SystemResources.GuaranteedMemoryInBytes,
MaximumMemoryInBytes =
resources.SystemResources.MaximumMemoryInBytes
});
}
}
}

Expand Down Expand Up @@ -284,7 +306,7 @@ public WriterGroupDiagnosticModel Get(string dataSetWriterId, TimeProvider timeP
}

private readonly MeterListener _meterListener;
private readonly IResourceMonitor _resources;
private readonly IResourceMonitor? _resources;
private readonly ILogger _logger;
private readonly TimeProvider _timeProvider;
private readonly ConcurrentDictionary<string, AggregateDiagnosticModel> _diagnostics = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private async Task DiagnosticsOutputTimerAsync(CancellationToken ct)
// TODO: case PublisherDiagnosticTargetType.PubSub:
// TODO: break;
default:
WriteDiagnosticsToConsole(diagnostics);
WriteDiagnosticsToConsole(diagnostics, _options.Value.DisableResourceMonitoring != true);
break;
}
}
Expand Down Expand Up @@ -475,12 +475,14 @@ await events.SendEventAsync(diagnosticsTopic,
/// Format diagnostics to console
/// </summary>
/// <param name="diagnostics"></param>
private static void WriteDiagnosticsToConsole(IEnumerable<(string, WriterGroupDiagnosticModel)> diagnostics)
/// <param name="includeResourceInfo"></param>
private static void WriteDiagnosticsToConsole(
IEnumerable<(string, WriterGroupDiagnosticModel)> diagnostics, bool includeResourceInfo)
{
var builder = new StringBuilder();
foreach (var (writerGroupId, info) in diagnostics)
{
builder = Append(builder, writerGroupId, info);
builder = Append(builder, writerGroupId, info, includeResourceInfo);
}

if (builder.Length > 0)
Expand All @@ -489,7 +491,7 @@ private static void WriteDiagnosticsToConsole(IEnumerable<(string, WriterGroupDi
}

static StringBuilder Append(StringBuilder builder, string writerGroupId,
WriterGroupDiagnosticModel info)
WriterGroupDiagnosticModel info, bool includeResourceInfo)
{
var s = info.IngestionDuration.TotalSeconds == 0 ? 1 : info.IngestionDuration.TotalSeconds;
var min = info.IngestionDuration.TotalMinutes == 0 ? 1 : info.IngestionDuration.TotalMinutes;
Expand Down Expand Up @@ -536,14 +538,18 @@ static string Format(long changes, long lastMinute, double s)
var connectivityState = info.NumberOfConnectedEndpoints > 0 ? (info.NumberOfDisconnectedEndpoints > 0 ?
"(Partially Connected)" : "(Connected)") : "(Disconnected)";

return builder.AppendLine()
var sb = builder.AppendLine()
.Append(" DIAGNOSTICS INFORMATION for : ")
.Append(info.WriterGroupName ?? Constants.DefaultWriterGroupName)
.Append(" (")
.AppendFormat(CultureInfo.CurrentCulture, "{0:0}", writerGroupId)
.AppendLine(")")
.Append(" # OPC Publisher Version (Runtime) : ")
.AppendLine(info.PublisherVersion)
;
if (includeResourceInfo)
{
sb = sb
.Append(" # Cpu/Memory max : ")
.AppendFormat(CultureInfo.CurrentCulture, "{0,14:n2}", info.MaximumCpuUnits)
.Append(" | ")
Expand All @@ -561,6 +567,9 @@ static string Format(long changes, long lastMinute, double s)
.Append(" (")
.AppendFormat(CultureInfo.CurrentCulture, "{0:n0}", info.MemoryUsedInBytes / 1000d)
.AppendLine(" kb)")
;
}
return sb
.Append(" # Ingest duration (dd:hh:mm:ss)/Time : ")
.AppendFormat(CultureInfo.CurrentCulture, "{0,14:dd\\:hh\\:mm\\:ss}", info.IngestionDuration)
.Append(" | ")
Expand Down

0 comments on commit 926a31b

Please sign in to comment.