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

Add PrometheusTagTransformer #3312

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Text.Json;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.Jaeger.Implementation;
Expand Down Expand Up @@ -48,5 +49,5 @@ protected override JaegerTag TransformStringTag(string key, string value)
}

protected override JaegerTag TransformArrayTag(string key, Array array)
=> this.TransformStringTag(key, System.Text.Json.JsonSerializer.Serialize(array));
=> this.TransformStringTag(key, JsonSerializer.Serialize(array));
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ public static int WriteLabelValue(byte[] buffer, int cursor, string value)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int WriteLabel(byte[] buffer, int cursor, string labelKey, object labelValue)
public static int WriteLabel(byte[] buffer, int cursor, string labelKey, string labelValue)
{
cursor = WriteLabelKey(buffer, cursor, labelKey);
buffer[cursor++] = unchecked((byte)'=');
buffer[cursor++] = unchecked((byte)'"');

// In Prometheus, a label with an empty label value is considered equivalent to a label that does not exist.
cursor = WriteLabelValue(buffer, cursor, labelValue?.ToString() ?? string.Empty);
cursor = WriteLabelValue(buffer, cursor, labelValue ?? string.Empty);
buffer[cursor++] = unchecked((byte)'"');

return cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,25 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)

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

var tagsWritten = 0;
foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
buffer[cursor++] = unchecked((byte)',');
if (PrometheusTagTransformer.Instance.TryTransformTag(tag, out var result))
{
if (++tagsWritten == 1)
{
buffer[cursor++] = unchecked((byte)'{');
}

cursor = WriteLabel(buffer, cursor, tag.Key, result);
buffer[cursor++] = unchecked((byte)',');
}
}

buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra.
if (tagsWritten > 0)
{
buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra.
}
}

buffer[cursor++] = unchecked((byte)' ');
Expand Down Expand Up @@ -113,8 +123,11 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)

foreach (var tag in tags)
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
buffer[cursor++] = unchecked((byte)',');
if (PrometheusTagTransformer.Instance.TryTransformTag(tag, out var result))
{
cursor = WriteLabel(buffer, cursor, tag.Key, result);
buffer[cursor++] = unchecked((byte)',');
}
}

cursor = WriteAsciiStringNoEscape(buffer, cursor, "le=\"");
Expand Down Expand Up @@ -142,16 +155,23 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
cursor = WriteMetricName(buffer, cursor, metric.Name, metric.Unit);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_sum");

if (tags.Count > 0)
var tagsWritten = 0;
foreach (var tag in tags)
{
buffer[cursor++] = unchecked((byte)'{');

foreach (var tag in tags)
if (PrometheusTagTransformer.Instance.TryTransformTag(tag, out var result))
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
if (++tagsWritten == 1)
{
buffer[cursor++] = unchecked((byte)'{');
}

cursor = WriteLabel(buffer, cursor, tag.Key, result);
buffer[cursor++] = unchecked((byte)',');
}
}

if (tagsWritten > 0)
{
buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra.
}

Expand All @@ -168,16 +188,23 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
cursor = WriteMetricName(buffer, cursor, metric.Name, metric.Unit);
cursor = WriteAsciiStringNoEscape(buffer, cursor, "_count");

if (tags.Count > 0)
tagsWritten = 0;
foreach (var tag in tags)
{
buffer[cursor++] = unchecked((byte)'{');

foreach (var tag in tags)
if (PrometheusTagTransformer.Instance.TryTransformTag(tag, out var result))
{
cursor = WriteLabel(buffer, cursor, tag.Key, tag.Value);
if (++tagsWritten == 1)
{
buffer[cursor++] = unchecked((byte)'{');
}

cursor = WriteLabel(buffer, cursor, tag.Key, result);
buffer[cursor++] = unchecked((byte)',');
}
}

if (tagsWritten > 0)
{
buffer[cursor - 1] = unchecked((byte)'}'); // Note: We write the '}' over the last written comma, which is extra.
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <copyright file="PrometheusTagTransformer.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;
using System.Text.Json;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.Prometheus;

internal sealed class PrometheusTagTransformer : TagTransformer<string>
{
private PrometheusTagTransformer()
{
}

public static PrometheusTagTransformer Instance { get; } = new();

protected override string TransformIntegralTag(string key, long value) => value.ToString();
reyang marked this conversation as resolved.
Show resolved Hide resolved

protected override string TransformFloatingPointTag(string key, double value) => value.ToString();

protected override string TransformBooleanTag(string key, bool value) => value ? "true" : "false";

protected override string TransformStringTag(string key, string value) => value;

protected override string TransformArrayTag(string key, Array array)
=> this.TransformStringTag(key, JsonSerializer.Serialize(array));
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<RunApiCompat>false</RunApiCompat>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="System.Text.Json" Version="$(SystemTextJsonPkgVer)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System;
using System.Text.Json;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.Zipkin.Implementation;
Expand All @@ -36,5 +37,5 @@ private ZipkinTagTransformer()
protected override string TransformStringTag(string key, string value) => value;

protected override string TransformArrayTag(string key, Array array)
=> this.TransformStringTag(key, System.Text.Json.JsonSerializer.Serialize(array));
=> this.TransformStringTag(key, JsonSerializer.Serialize(array));
}
Loading