Skip to content

Commit

Permalink
Add scope info and version to Prometheus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart committed Nov 27, 2023
1 parent eec09c9 commit 665537c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ public int ScrapeResponseCacheDurationMilliseconds
set => this.ExporterOptions.ScrapeResponseCacheDurationMilliseconds = value;
}

/// <summary>
/// Gets or sets the ability to export metric scope info. Default value: true.
/// </summary>
public bool ScopeInfoEnabled
{
get => this.ExporterOptions.ScopeInfoEnabled;
set => this.ExporterOptions.ScopeInfoEnabled = value;
}

internal PrometheusExporterOptions ExporterOptions { get; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,23 @@ private ExportResult OnCollect(Batch<Metric> metrics)

try
{
foreach (var metric in metrics)
if (this.exporter.ScopeInfoEnabled)
{
if (!PrometheusSerializer.CanWriteMetric(metric))
var scopes = new HashSet<string>();

foreach (var metric in metrics)
{
continue;
if (PrometheusSerializer.CanWriteMetric(metric))
{
scopes.Add(metric.MeterName);
}
}

while (true)
foreach (var scope in scopes)
{
try
{
cursor = PrometheusSerializer.WriteMetric(this.buffer, cursor, metric, this.GetPrometheusMetric(metric));
break;
cursor = PrometheusSerializer.WriteScopeInfo(this.buffer, cursor, scope);
}
catch (IndexOutOfRangeException)
{
Expand All @@ -212,6 +216,30 @@ private ExportResult OnCollect(Batch<Metric> metrics)
}
}

foreach (var metric in metrics)
{
if (!PrometheusSerializer.CanWriteMetric(metric))
{
continue;
}

while (true)
{
try
{
cursor = PrometheusSerializer.WriteMetric(this.buffer, cursor, metric, this.GetPrometheusMetric(metric), this.exporter.ScopeInfoEnabled);
break;
}
catch (IndexOutOfRangeException)
{
if (!this.IncreaseBufferSize())
{
throw;
}
}
}
}

while (true)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public PrometheusExporter(PrometheusExporterOptions options)
Guard.ThrowIfNull(options);

this.ScrapeResponseCacheDurationMilliseconds = options.ScrapeResponseCacheDurationMilliseconds;
this.ScopeInfoEnabled = options.ScopeInfoEnabled;

this.CollectionManager = new PrometheusCollectionManager(this);
}
Expand All @@ -63,6 +64,8 @@ public PrometheusExporter(PrometheusExporterOptions options)

internal int ScrapeResponseCacheDurationMilliseconds { get; }

internal bool ScopeInfoEnabled { get; }

/// <inheritdoc/>
public override ExportResult Export(in Batch<Metric> metrics)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public int ScrapeResponseCacheDurationMilliseconds
this.scrapeResponseCacheDurationMilliseconds = value;
}
}

/// <summary>
/// Gets or sets the ability to export metric scope info. Default value: true.
/// </summary>
public bool ScopeInfoEnabled { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,31 @@ public static int WriteUnitMetadata(byte[] buffer, int cursor, PrometheusMetric
return cursor;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteScopeInfo(byte[] buffer, int cursor, string scopeName)
{
if (string.IsNullOrEmpty(scopeName))
{
return cursor;
}

cursor = WriteAsciiStringNoEscape(buffer, cursor, "# TYPE otel_scope_info info");
buffer[cursor++] = ASCII_LINEFEED;

cursor = WriteAsciiStringNoEscape(buffer, cursor, "# HELP otel_scope_info Scope metadata");
buffer[cursor++] = ASCII_LINEFEED;

cursor = WriteAsciiStringNoEscape(buffer, cursor, "otel_scope_info");
buffer[cursor++] = unchecked((byte)'{');
cursor = WriteLabel(buffer, cursor, "otel_scope_name", scopeName);
buffer[cursor++] = unchecked((byte)'}');
buffer[cursor++] = unchecked((byte)' ');
buffer[cursor++] = unchecked((byte)'1');
buffer[cursor++] = ASCII_LINEFEED;

return cursor;
}

private static string MapPrometheusType(PrometheusType type)
{
return type switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static bool CanWriteMetric(Metric metric)
return true;
}

public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric)
public static int WriteMetric(byte[] buffer, int cursor, Metric metric, PrometheusMetric prometheusMetric, bool scopeInfoEnabled)
{
cursor = WriteTypeMetadata(buffer, cursor, prometheusMetric);
cursor = WriteUnitMetadata(buffer, cursor, prometheusMetric);
Expand All @@ -51,10 +51,22 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
// Counter and Gauge
cursor = WriteMetricName(buffer, cursor, prometheusMetric);

if (tags.Count > 0)
if (tags.Count > 0 || scopeInfoEnabled)
{
buffer[cursor++] = unchecked((byte)'{');

if (scopeInfoEnabled)
{
cursor = WriteLabel(buffer, cursor, "otel_scope_name", metric.MeterName);
buffer[cursor++] = unchecked((byte)',');

if (!string.IsNullOrEmpty(metric.MeterVersion))
{
cursor = WriteLabel(buffer, cursor, "otel_scope_version", metric.MeterVersion);
buffer[cursor++] = unchecked((byte)',');
}
}

foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
Expand Down Expand Up @@ -145,10 +157,22 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
cursor = WriteMetricName(buffer, cursor, prometheusMetric);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_sum");

if (tags.Count > 0)
if (tags.Count > 0 || scopeInfoEnabled)
{
buffer[cursor++] = unchecked((byte)'{');

if (scopeInfoEnabled)
{
cursor = WriteLabel(buffer, cursor, "otel_scope_name", metric.MeterName);
buffer[cursor++] = unchecked((byte)',');

if (!string.IsNullOrEmpty(metric.MeterVersion))
{
cursor = WriteLabel(buffer, cursor, "otel_scope_version", metric.MeterVersion);
buffer[cursor++] = unchecked((byte)',');
}
}

foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
Expand All @@ -171,10 +195,22 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric, Promethe
cursor = WriteMetricName(buffer, cursor, prometheusMetric);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_count");

if (tags.Count > 0)
if (tags.Count > 0 || scopeInfoEnabled)
{
buffer[cursor++] = unchecked((byte)'{');

if (scopeInfoEnabled)
{
cursor = WriteLabel(buffer, cursor, "otel_scope_name", metric.MeterName);
buffer[cursor++] = unchecked((byte)',');

if (!string.IsNullOrEmpty(metric.MeterVersion))
{
cursor = WriteLabel(buffer, cursor, "otel_scope_version", metric.MeterVersion);
buffer[cursor++] = unchecked((byte)',');
}
}

foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
Expand Down

0 comments on commit 665537c

Please sign in to comment.