Skip to content
Prev Previous commit
Next Next commit
Clean up
  • Loading branch information
JamesNK committed Aug 8, 2023
commit 1b041cc7a6b667e821d894f7cc806a07381f8160
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ public PrometheusMetric(string name, string unit, PrometheusType type)

this.Name = sanitizedName;
this.Unit = sanitizedUnit;
this.Type = type;
}

public string Name { get; }

public string Unit { get; }

public PrometheusType Type { get; }

internal static string SanitizeMetricName(string metricName)
{
StringBuilder sb = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public static int WriteLabel(byte[] buffer, int cursor, string labelKey, object
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteMetricName(byte[] buffer, int cursor, PrometheusMetric metric)
{
// Metric name has already been escaped.
for (int i = 0; i < metric.Name.Length; i++)
{
var ordinal = (ushort)metric.Name[i];
Expand Down Expand Up @@ -272,8 +273,10 @@ public static int WriteHelpMetadata(byte[] buffer, int cursor, PrometheusMetric
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteTypeMetadata(byte[] buffer, int cursor, PrometheusMetric metric, string metricType)
public static int WriteTypeMetadata(byte[] buffer, int cursor, PrometheusMetric metric)
{
var metricType = MapPrometheusType(metric.Type);

Debug.Assert(!string.IsNullOrEmpty(metricType), $"{nameof(metricType)} should not be null or empty.");

cursor = WriteAsciiStringNoEscape(buffer, cursor, "# TYPE ");
Expand All @@ -299,6 +302,7 @@ public static int WriteUnitMetadata(byte[] buffer, int cursor, PrometheusMetric

buffer[cursor++] = unchecked((byte)' ');

// Unit name has already been escaped.
for (int i = 0; i < metric.Unit.Length; i++)
{
var ordinal = (ushort)metric.Unit[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ UpDownCounter becomes gauge
PrometheusType.Untyped, PrometheusType.Counter, PrometheusType.Gauge, PrometheusType.Summary, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Histogram, PrometheusType.Gauge,
};

private static string MapPrometheusType(PrometheusType type)
{
return type switch
{
PrometheusType.Gauge => "gauge",
PrometheusType.Counter => "counter",
PrometheusType.Summary => "summary",
PrometheusType.Histogram => "histogram",
_ => "untyped",
};
}

private static PrometheusType GetPrometheusType(Metric metric)
{
int metricType = (int)metric.MetricType >> 4;
return MetricTypes[metricType];
}

private static readonly ConcurrentDictionary<Metric, PrometheusMetric> MetricsCache = new ConcurrentDictionary<Metric, PrometheusMetric>();
private static int metricsCacheCount;

Expand All @@ -67,10 +49,9 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
return cursor;
}

PrometheusType prometheusType = GetPrometheusType(metric);
var prometheusMetric = GetPrometheusMetric(metric);

cursor = WriteTypeMetadata(buffer, cursor, prometheusMetric, MapPrometheusType(prometheusType));
cursor = WriteTypeMetadata(buffer, cursor, prometheusMetric);
cursor = WriteUnitMetadata(buffer, cursor, prometheusMetric);
cursor = WriteHelpMetadata(buffer, cursor, prometheusMetric, metric.Description);

Expand Down Expand Up @@ -253,4 +234,22 @@ private static PrometheusMetric GetPrometheusMetric(Metric metric)
});
}
}

private static string MapPrometheusType(PrometheusType type)
{
return type switch
{
PrometheusType.Gauge => "gauge",
PrometheusType.Counter => "counter",
PrometheusType.Summary => "summary",
PrometheusType.Histogram => "histogram",
_ => "untyped",
};
}

private static PrometheusType GetPrometheusType(Metric metric)
{
int metricType = (int)metric.MetricType >> 4;
return MetricTypes[metricType];
}
}