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

Refactor for DataValue #2093

Merged
merged 2 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 15 additions & 51 deletions src/OpenTelemetry/Metrics/DataPoint/DataPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@ namespace OpenTelemetry.Metrics
{
private static readonly KeyValuePair<string, object>[] EmptyTag = new KeyValuePair<string, object>[0];

private readonly Type valueType;
private readonly long longValue;
private readonly double doubleValue;
private readonly IDataValue value;

internal DataPoint(DateTimeOffset timestamp, long value, KeyValuePair<string, object>[] tags)
{
this.Timestamp = timestamp;
this.Tags = tags;
this.valueType = typeof(long);
this.longValue = value;
this.doubleValue = 0;
this.value = new DataValue<long>(value);
}

internal DataPoint(DateTimeOffset timestamp, double value, KeyValuePair<string, object>[] tags)
{
this.Timestamp = timestamp;
this.Tags = tags;
this.valueType = typeof(double);
this.longValue = 0;
this.doubleValue = value;
this.value = new DataValue<double>(value);
}

internal DataPoint(DateTimeOffset timestamp, IDataValue value, KeyValuePair<string, object>[] tags)
{
this.Timestamp = timestamp;
this.Tags = tags;
this.value = value;
}

internal DataPoint(DateTimeOffset timestamp, long value)
Expand All @@ -55,52 +56,15 @@ internal DataPoint(DateTimeOffset timestamp, double value)
{
}

public DateTimeOffset Timestamp { get; }

public readonly KeyValuePair<string, object>[] Tags { get; }

public object Value
internal DataPoint(DateTimeOffset timestamp, IDataValue value)
: this(timestamp, value, DataPoint.EmptyTag)
{
get
{
if (this.valueType == typeof(long))
{
return this.longValue;
}
else if (this.valueType == typeof(double))
{
return this.doubleValue;
}
else
{
throw new Exception("Unsupported Type");
}
}
}

internal static DataPoint CreateDataPoint<T>(DateTimeOffset timestamp, T value, KeyValuePair<string, object>[] tags)
{
DataPoint dp;
public DateTimeOffset Timestamp { get; }

if (typeof(T) == typeof(int))
{
// Promoted to Long
dp = new DataPoint(timestamp, (int)(object)value, tags);
}
else if (typeof(T) == typeof(long))
{
dp = new DataPoint(timestamp, (long)(object)value, tags);
}
else if (typeof(T) == typeof(double))
{
dp = new DataPoint(timestamp, (double)(object)value, tags);
}
else
{
throw new Exception("Unsupported Type");
}
public readonly KeyValuePair<string, object>[] Tags { get; }

return dp;
}
public object Value => this.value.Value;
}
}
16 changes: 14 additions & 2 deletions src/OpenTelemetry/Metrics/DataPoint/DataPoint{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ namespace OpenTelemetry.Metrics
{
private static readonly KeyValuePair<string, object>[] EmptyTag = new KeyValuePair<string, object>[0];

private readonly T value;
private readonly IDataValue value;

internal DataPoint(DateTimeOffset timestamp, T value, KeyValuePair<string, object>[] tags)
{
this.Timestamp = timestamp;
this.Tags = tags;
this.value = new DataValue<T>(value);
}

internal DataPoint(DateTimeOffset timestamp, IDataValue value, KeyValuePair<string, object>[] tags)
{
this.Timestamp = timestamp;
this.Tags = tags;
Expand All @@ -38,10 +45,15 @@ internal DataPoint(DateTimeOffset timestamp, T value)
{
}

internal DataPoint(DateTimeOffset timestamp, IDataValue value)
: this(timestamp, value, DataPoint<T>.EmptyTag)
{
}

public DateTimeOffset Timestamp { get; }

public readonly KeyValuePair<string, object>[] Tags { get; }

public object Value => (object)this.value;
public object Value => this.value.Value;
}
}
48 changes: 48 additions & 0 deletions src/OpenTelemetry/Metrics/DataPoint/DataValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// <copyright file="DataValue.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;

namespace OpenTelemetry.Metrics
{
public readonly struct DataValue : IDataValue
{
private readonly IDataValue value;

internal DataValue(int value)
{
// Promote to long
this.value = new DataValue<long>(value);
}

internal DataValue(long value)
{
this.value = new DataValue<long>(value);
}

internal DataValue(double value)
{
this.value = new DataValue<double>(value);
}

internal DataValue(IDataValue value)
{
this.value = value;
}

public object Value => this.value.Value;
}
}
31 changes: 31 additions & 0 deletions src/OpenTelemetry/Metrics/DataPoint/DataValue{T}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="DataValue{T}.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>

namespace OpenTelemetry.Metrics
{
internal readonly struct DataValue<T> : IDataValue
where T : struct
{
private readonly T value;

internal DataValue(T value)
{
this.value = value;
}

public object Value => (object)this.value;
}
}
70 changes: 18 additions & 52 deletions src/OpenTelemetry/Metrics/DataPoint/Exemplar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ namespace OpenTelemetry.Metrics
private static readonly KeyValuePair<string, object>[] EmptyTag = new KeyValuePair<string, object>[0];
private static readonly byte[] EmptyId = new byte[0];

private readonly Type valueType;
private readonly long longValue;
private readonly double doubleValue;
private readonly IDataValue value;

internal Exemplar(DateTimeOffset timestamp, long value, byte[] spanId, byte[] traceId, KeyValuePair<string, object>[] filteredTags)
{
this.Timestamp = timestamp;
this.FilteredTags = filteredTags;
this.SpanId = spanId;
this.TraceId = traceId;
this.valueType = typeof(long);
this.longValue = value;
this.doubleValue = 0;
this.value = new DataValue<long>(value);
}

internal Exemplar(DateTimeOffset timestamp, double value, byte[] spanId, byte[] traceId, KeyValuePair<string, object>[] filteredTags)
Expand All @@ -45,9 +41,16 @@ internal Exemplar(DateTimeOffset timestamp, double value, byte[] spanId, byte[]
this.FilteredTags = filteredTags;
this.SpanId = spanId;
this.TraceId = traceId;
this.valueType = typeof(double);
this.longValue = 0;
this.doubleValue = value;
this.value = new DataValue<double>(value);
}

internal Exemplar(DateTimeOffset timestamp, IDataValue value, byte[] spanId, byte[] traceId, KeyValuePair<string, object>[] filteredTags)
{
this.Timestamp = timestamp;
this.FilteredTags = filteredTags;
this.SpanId = spanId;
this.TraceId = traceId;
this.value = value;
}

internal Exemplar(DateTimeOffset timestamp, long value)
Expand All @@ -60,6 +63,11 @@ internal Exemplar(DateTimeOffset timestamp, double value)
{
}

internal Exemplar(DateTimeOffset timestamp, IDataValue value)
: this(timestamp, value, Exemplar.EmptyId, Exemplar.EmptyId, Exemplar.EmptyTag)
{
}

public DateTimeOffset Timestamp { get; }

public readonly KeyValuePair<string, object>[] FilteredTags { get; }
Expand All @@ -68,48 +76,6 @@ internal Exemplar(DateTimeOffset timestamp, double value)

public readonly byte[] TraceId { get; }

public object Value
{
get
{
if (this.valueType == typeof(long))
{
return this.longValue;
}
else if (this.valueType == typeof(double))
{
return this.doubleValue;
}
else
{
throw new Exception("Unsupported Type");
}
}
}

internal static Exemplar CreateExemplar<T>(DateTimeOffset timestamp, T value, byte[] spanId, byte[] traceId, KeyValuePair<string, object>[] filteredTags)
{
Exemplar dp;

if (typeof(T) == typeof(int))
{
// Promoted to Long
dp = new Exemplar(timestamp, (int)(object)value, spanId, traceId, filteredTags);
}
else if (typeof(T) == typeof(long))
{
dp = new Exemplar(timestamp, (long)(object)value, spanId, traceId, filteredTags);
}
else if (typeof(T) == typeof(double))
{
dp = new Exemplar(timestamp, (double)(object)value, spanId, traceId, filteredTags);
}
else
{
throw new Exception("Unsupported Type");
}

return dp;
}
public object Value => this.value.Value;
}
}
18 changes: 16 additions & 2 deletions src/OpenTelemetry/Metrics/DataPoint/Exemplar{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ namespace OpenTelemetry.Metrics
private static readonly KeyValuePair<string, object>[] EmptyTag = new KeyValuePair<string, object>[0];
private static readonly byte[] EmptyId = new byte[0];

private readonly T value;
private readonly IDataValue value;

internal Exemplar(DateTimeOffset timestamp, T value, byte[] spanId, byte[] traceId, KeyValuePair<string, object>[] filteredTags)
{
this.Timestamp = timestamp;
this.FilteredTags = filteredTags;
this.SpanId = spanId;
this.TraceId = traceId;
this.value = new DataValue<T>(value);
}

internal Exemplar(DateTimeOffset timestamp, IDataValue value, byte[] spanId, byte[] traceId, KeyValuePair<string, object>[] filteredTags)
{
this.Timestamp = timestamp;
this.FilteredTags = filteredTags;
Expand All @@ -41,6 +50,11 @@ internal Exemplar(DateTimeOffset timestamp, T value)
{
}

internal Exemplar(DateTimeOffset timestamp, IDataValue value)
: this(timestamp, value, Exemplar<T>.EmptyId, Exemplar<T>.EmptyId, Exemplar<T>.EmptyTag)
{
}

public DateTimeOffset Timestamp { get; }

public readonly KeyValuePair<string, object>[] FilteredTags { get; }
Expand All @@ -49,6 +63,6 @@ internal Exemplar(DateTimeOffset timestamp, T value)

public readonly byte[] TraceId { get; }

public object Value => (object)this.value;
public object Value => this.value.Value;
}
}
4 changes: 1 addition & 3 deletions src/OpenTelemetry/Metrics/DataPoint/IDataPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

namespace OpenTelemetry.Metrics
{
internal interface IDataPoint
public interface IDataPoint : IDataValue
{
DateTimeOffset Timestamp { get; }

KeyValuePair<string, object>[] Tags { get; }

object Value { get; }
}
}
23 changes: 23 additions & 0 deletions src/OpenTelemetry/Metrics/DataPoint/IDataValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <copyright file="IDataValue.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>

namespace OpenTelemetry.Metrics
{
public interface IDataValue
{
object Value { get; }
}
}
Loading