Skip to content

Commit

Permalink
[Exporter.InfluxDB] Add support for Resources attributes (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret authored Jun 20, 2023
1 parent 78d2016 commit 37b1e45
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.InfluxDB/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Support for Resource attributes in OpenTelemetry.Exporter.InfluxDB, allowing
resource attributes to be passed as InfluxDB tags.
([#1241](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1241))
* Updates to 1.5.0 of OpenTelemetry SDK.
([#1220](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1220))

Expand Down
3 changes: 2 additions & 1 deletion src/OpenTelemetry.Exporter.InfluxDB/IMetricsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

using InfluxDB.Client;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Exporter.InfluxDB;

internal interface IMetricsWriter
{
void Write(Metric metric, WriteApi writeApi);
void Write(Metric metric, Resource resource, WriteApi writeApi);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override ExportResult Export(in Batch<Metric> batch)
{
foreach (var metric in batch)
{
this.writer.Write(metric, this.writeApi);
this.writer.Write(metric, this.ParentProvider?.GetResource(), this.writeApi);
}

return ExportResult.Success;
Expand Down
16 changes: 16 additions & 0 deletions src/OpenTelemetry.Exporter.InfluxDB/PointDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System.Collections.Generic;
using InfluxDB.Client.Writes;

namespace OpenTelemetry.Exporter.InfluxDB;
Expand All @@ -29,4 +30,19 @@ public static PointData Tags(this PointData pointData, ReadOnlyTagCollection tag

return pointData;
}

public static PointData Tags(this PointData pointData, IEnumerable<KeyValuePair<string, object>> tags)
{
if (tags == null)
{
return pointData;
}

foreach (var tag in tags)
{
pointData = pointData.Tag(tag.Key, tag.Value.ToString());
}

return pointData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Exporter.InfluxDB;

internal sealed class TelegrafPrometheusWriterV1 : IMetricsWriter
{
public void Write(Metric metric, WriteApi writeApi)
public void Write(Metric metric, Resource resource, WriteApi writeApi)
{
var measurement = metric.Name;

Expand All @@ -38,6 +39,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field("gauge", dataPoint.GetGaugeLastValueLong())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -53,6 +55,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field("gauge", dataPoint.GetGaugeLastValueDouble())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -68,6 +71,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field("counter", dataPoint.GetSumLong())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -83,6 +87,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field("counter", dataPoint.GetSumDouble())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -98,6 +103,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field("gauge", dataPoint.GetSumLong())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -113,6 +119,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field("gauge", dataPoint.GetSumDouble())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -128,6 +135,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Field("count", dataPoint.GetHistogramCount())
.Field("sum", dataPoint.GetHistogramSum())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);

if (dataPoint.TryGetHistogramMinMaxValues(out double min, out double max))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Writes;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Exporter.InfluxDB;

internal sealed class TelegrafPrometheusWriterV2 : IMetricsWriter
{
public void Write(Metric metric, WriteApi writeApi)
public void Write(Metric metric, Resource resource, WriteApi writeApi)
{
var measurement = "prometheus";
var metricName = metric.Name;
Expand All @@ -39,6 +40,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field(metricName, metricPoint.GetGaugeLastValueLong())
.Tags(metricPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(metricPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -54,6 +56,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field(metricName, metricPoint.GetGaugeLastValueDouble())
.Tags(metricPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(metricPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -69,6 +72,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field(metricName, metricPoint.GetSumLong())
.Tags(metricPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(metricPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -84,6 +88,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field(metricName, metricPoint.GetSumDouble())
.Tags(metricPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(metricPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -99,6 +104,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field(metricName, dataPoint.GetSumLong())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -114,6 +120,7 @@ public void Write(Metric metric, WriteApi writeApi)
.Measurement(measurement)
.Field(metricName, dataPoint.GetSumDouble())
.Tags(dataPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(dataPoint.EndTime.UtcDateTime, WritePrecision.Ns);
writeApi.WritePoint(pointData);
}
Expand All @@ -127,6 +134,7 @@ public void Write(Metric metric, WriteApi writeApi)
var basePoint = PointData
.Measurement(measurement)
.Tags(metricPoint.Tags)
.Tags(resource?.Attributes)
.Timestamp(metricPoint.EndTime.UtcDateTime, WritePrecision.Ns);

var headPoint = basePoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Diagnostics.Metrics;
using OpenTelemetry.Exporter.InfluxDB.Tests.Utils;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using Xunit;

namespace OpenTelemetry.Exporter.InfluxDB.Tests;
Expand All @@ -36,6 +37,7 @@ public void ExportIntGaugeMetric(MetricsSchema metricsSchema, string measurement

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand All @@ -61,9 +63,7 @@ public void ExportIntGaugeMetric(MetricsSchema metricsSchema, string measurement
Assert.Equal(1, dataPoint.Fields.Count);
AssertUtils.HasField(valueKey, 42L, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand All @@ -81,6 +81,7 @@ public void ExportDoubleGaugeMetric(MetricsSchema metricsSchema, string measurem

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand All @@ -106,9 +107,7 @@ public void ExportDoubleGaugeMetric(MetricsSchema metricsSchema, string measurem
Assert.Equal(1, dataPoint.Fields.Count);
AssertUtils.HasField(valueKey, 55.42D, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand All @@ -126,6 +125,7 @@ public void ExportIntSumMetric(MetricsSchema metricsSchema, string measurement,

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand All @@ -149,9 +149,7 @@ public void ExportIntSumMetric(MetricsSchema metricsSchema, string measurement,
Assert.Equal(1, dataPoint.Fields.Count);
AssertUtils.HasField(valueKey, 100L, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand All @@ -169,6 +167,7 @@ public void ExportDoubleSumMetric(MetricsSchema metricsSchema, string measuremen

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand All @@ -192,9 +191,7 @@ public void ExportDoubleSumMetric(MetricsSchema metricsSchema, string measuremen
Assert.Equal(1, dataPoint.Fields.Count);
AssertUtils.HasField(valueKey, 12.59D, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand All @@ -212,6 +209,7 @@ public void ExportNonMonotonicIntSumMetric(MetricsSchema metricsSchema, string m

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand All @@ -236,9 +234,7 @@ public void ExportNonMonotonicIntSumMetric(MetricsSchema metricsSchema, string m
Assert.Equal(1, dataPoint.Fields.Count);
AssertUtils.HasField(valueKey, -50L, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand All @@ -256,6 +252,7 @@ public void ExportNonMonotonicDoubleSumMetric(MetricsSchema metricsSchema, strin

using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand All @@ -280,9 +277,7 @@ public void ExportNonMonotonicDoubleSumMetric(MetricsSchema metricsSchema, strin
Assert.Equal(1, dataPoint.Fields.Count);
AssertUtils.HasField(valueKey, -50.11D, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand All @@ -303,6 +298,7 @@ public void ExportHistogramMetricWhenTelegrafPrometheusV1MetricsSchemaUsed()
Boundaries = new[] { 10D, 20D, 100D, 200D },
RecordMinMax = true,
})
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.WithDefaultTestConfiguration();
Expand Down Expand Up @@ -337,9 +333,7 @@ public void ExportHistogramMetricWhenTelegrafPrometheusV1MetricsSchemaUsed()
AssertUtils.HasField("200.00", 1L, dataPoint.Fields);
AssertUtils.HasField("+Inf", 1L, dataPoint.Fields);

Assert.Equal(2, dataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, dataPoint.Tags);
AssertTags(dataPoint);

Assert.InRange(dataPoint.Timestamp, before, after);
}
Expand Down Expand Up @@ -395,6 +389,7 @@ public void ExportHistogramMetricWhenTelegrafPrometheusV2MetricsSchemaUsed()
Boundaries = new[] { 10D, 20D, 100D, 200D },
RecordMinMax = true,
})
.ConfigureDefaultTestResource()
.AddInfluxDBMetricsExporter(options =>
{
options.Endpoint = influxServerEndpoint;
Expand Down Expand Up @@ -422,9 +417,8 @@ public void ExportHistogramMetricWhenTelegrafPrometheusV2MetricsSchemaUsed()
AssertUtils.HasField("histogram_metric_sum", 550D, headDataPoint.Fields);
AssertUtils.HasField("histogram_metric_min", 50D, headDataPoint.Fields);
AssertUtils.HasField("histogram_metric_max", 250D, headDataPoint.Fields);
Assert.Equal(2, headDataPoint.Tags.Count);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 0, headDataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 1, headDataPoint.Tags);

AssertTags(headDataPoint);
Assert.InRange(headDataPoint.Timestamp, before, after);

AssertBucketDataPoint(influxServer.ReadPoint(), "10.00", 0);
Expand All @@ -438,8 +432,15 @@ static void AssertBucketDataPoint(PointData dataPoint, string bound, long count)
Assert.Equal("prometheus", dataPoint.Measurement);
AssertUtils.HasField("histogram_metric_bucket", count, dataPoint.Fields);
AssertUtils.HasTag("le", bound, 0, dataPoint.Tags);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 1, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 2, dataPoint.Tags);
AssertUtils.HasTag("service.instance.id", "my-service-id", 1, dataPoint.Tags);
AssertUtils.HasTag("service.name", "my-service", 2, dataPoint.Tags);
AssertUtils.HasTag("service.namespace", "my-service-namespace", 3, dataPoint.Tags);
AssertUtils.HasTag("service.version", "1.0", 4, dataPoint.Tags);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 5, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 6, dataPoint.Tags);
AssertUtils.HasTag("telemetry.sdk.language", "dotnet", 7, dataPoint.Tags);
AssertUtils.HasTag("telemetry.sdk.name", "opentelemetry", 8, dataPoint.Tags);
AssertUtils.HasTag("telemetry.sdk.version", "1.5.0", 9, dataPoint.Tags);
}
}

Expand Down Expand Up @@ -477,4 +478,18 @@ public void ExportHistogramMetricWithoutMinMaxFieldsWhenTelegrafPrometheusV2Metr
Assert.DoesNotContain("histogram_metric_min", pointData.Fields);
Assert.DoesNotContain("histogram_metric_max", pointData.Fields);
}

private static void AssertTags(PointData dataPoint)
{
Assert.Equal(9, dataPoint.Tags.Count);
AssertUtils.HasTag("service.instance.id", "my-service-id", 0, dataPoint.Tags);
AssertUtils.HasTag("service.name", "my-service", 1, dataPoint.Tags);
AssertUtils.HasTag("service.namespace", "my-service-namespace", 2, dataPoint.Tags);
AssertUtils.HasTag("service.version", "1.0", 3, dataPoint.Tags);
AssertUtils.HasTag("tag_key_1", "tag_value_1", 4, dataPoint.Tags);
AssertUtils.HasTag("tag_key_2", "tag_value_2", 5, dataPoint.Tags);
AssertUtils.HasTag("telemetry.sdk.language", "dotnet", 6, dataPoint.Tags);
AssertUtils.HasTag("telemetry.sdk.name", "opentelemetry", 7, dataPoint.Tags);
AssertUtils.HasTag("telemetry.sdk.version", "1.5.0", 8, dataPoint.Tags);
}
}
Loading

0 comments on commit 37b1e45

Please sign in to comment.