feat(telemetry): 0.3.0 ApplyConfiguration reads an app's telemetry section (#55)
Exporter, OtlpEndpoint, ServiceVersion, SiteId, NodeRole, ExportLogs and ResourceAttributes:<key> from <App>:Telemetry, absent keys keep code defaults, so the four apps stop re-implementing the exporter parse and gain the identity keys. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.Telemetry;
|
||||
|
||||
/// <summary>
|
||||
/// Reads an app's own telemetry configuration section onto <see cref="ZbTelemetryOptions"/>, so
|
||||
/// every app exposes the same operator keys under its own prefix (e.g. <c>OtOpcUa:Telemetry</c>,
|
||||
/// <c>MxGateway:Telemetry</c>) without re-implementing the parsing.
|
||||
/// </summary>
|
||||
public static class ZbTelemetryConfigurationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies the keys present in <paramref name="section"/>; an absent key leaves the option as the
|
||||
/// caller set it, so call this after setting code defaults:
|
||||
/// <list type="table">
|
||||
/// <item><term><c>Exporter</c></term><description><c>Prometheus</c> | <c>Otlp</c>, case-insensitive; an unparseable value is ignored.</description></item>
|
||||
/// <item><term><c>OtlpEndpoint</c></term><description>e.g. <c>http://collector:4317</c> (non-blank only).</description></item>
|
||||
/// <item><term><c>ServiceVersion</c></term><description>overrides the entry-assembly default; an empty value omits <c>service.version</c>.</description></item>
|
||||
/// <item><term><c>SiteId</c> / <c>NodeRole</c></term><description>Resource <c>site.id</c> / <c>node.role</c> (non-blank only).</description></item>
|
||||
/// <item><term><c>ExportLogs</c></term><description><c>true</c> | <c>false</c>; whether OTLP also carries logs.</description></item>
|
||||
/// <item><term><c>ResourceAttributes:<key></c></term><description>extra string Resource attributes (reserved keys are rejected at startup).</description></item>
|
||||
/// </list>
|
||||
/// </summary>
|
||||
/// <param name="options">The options to update.</param>
|
||||
/// <param name="section">The app's telemetry section (it may be empty or missing).</param>
|
||||
/// <returns><paramref name="options"/>, for chaining.</returns>
|
||||
public static ZbTelemetryOptions ApplyConfiguration(this ZbTelemetryOptions options, IConfiguration section)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
ArgumentNullException.ThrowIfNull(section);
|
||||
|
||||
if (Enum.TryParse<ZbExporter>(section["Exporter"], ignoreCase: true, out var exporter))
|
||||
{
|
||||
options.Exporter = exporter;
|
||||
}
|
||||
|
||||
if (section["OtlpEndpoint"] is { } endpoint && !string.IsNullOrWhiteSpace(endpoint))
|
||||
{
|
||||
options.OtlpEndpoint = endpoint;
|
||||
}
|
||||
|
||||
if (section["ServiceVersion"] is { } version)
|
||||
{
|
||||
options.ServiceVersion = version.Trim();
|
||||
}
|
||||
|
||||
if (section["SiteId"] is { } siteId && !string.IsNullOrWhiteSpace(siteId))
|
||||
{
|
||||
options.SiteId = siteId.Trim();
|
||||
}
|
||||
|
||||
if (section["NodeRole"] is { } nodeRole && !string.IsNullOrWhiteSpace(nodeRole))
|
||||
{
|
||||
options.NodeRole = nodeRole.Trim();
|
||||
}
|
||||
|
||||
if (bool.TryParse(section["ExportLogs"], out var exportLogs))
|
||||
{
|
||||
options.ExportLogs = exportLogs;
|
||||
}
|
||||
|
||||
foreach (var child in section.GetSection("ResourceAttributes").GetChildren())
|
||||
{
|
||||
if (child.Value is { } value)
|
||||
{
|
||||
options.ResourceAttributes[child.Key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user