Skip to content
This repository was archived by the owner on Dec 18, 2023. It is now read-only.

Commit 5192867

Browse files
support double attribute (#44)
* support double attribute values * added double value
1 parent 5696825 commit 5192867

File tree

9 files changed

+49
-19
lines changed

9 files changed

+49
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- Support double attributes according to the [spec change](https://github.com/census-instrumentation/opencensus-specs/issues/172).
34
- Initial implementation of Prometheus exporter.
45
- Initial version of Application Insights exporter implemented.
56
- Zipkin exporter implemented.

src/OpenCensus.Abstractions/Trace/IAttributeValue.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ T Match<T>(
2424
Func<string, T> stringFunction,
2525
Func<bool, T> booleanFunction,
2626
Func<long, T> longFunction,
27+
Func<double, T> doubleFunction,
2728
Func<object, T> defaultFunction);
2829
}
2930

src/OpenCensus.Exporter.ApplicationInsights/Implementation/TraceExporterHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void Export(IList<ISpanData> spanDataList)
6565
(s) => { return s; },
6666
(b) => { return b.ToString(); },
6767
(l) => { return l.ToString(); },
68+
(d) => { return d.ToString(); },
6869
(obj) => { return obj.ToString(); });
6970

7071
result.Properties.Add(attr.Key, value);
@@ -99,6 +100,10 @@ private bool IsClientSpanKind(IAttributeValue value)
99100
return false;
100101
},
101102
(arg) =>
103+
{
104+
return false;
105+
},
106+
(arg) =>
102107
{
103108
return false;
104109
});
@@ -120,6 +125,10 @@ private bool IsServerSpanKind(IAttributeValue value)
120125
return false;
121126
},
122127
(arg) =>
128+
{
129+
return false;
130+
},
131+
(arg) =>
123132
{
124133
return false;
125134
});

src/OpenCensus.Exporter.Zipkin/Implementation/TraceExporterHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private string AttributeValueToString(IAttributeValue attributeValue)
124124
(arg) => { return arg; },
125125
(arg) => { return arg.ToString(); },
126126
(arg) => { return arg.ToString(); },
127+
(arg) => { return arg.ToString(); },
127128
(arg) => { return null; });
128129
}
129130

@@ -186,6 +187,10 @@ private bool IsClientSpanKind(IAttributeValue value)
186187
return false;
187188
},
188189
(arg) =>
190+
{
191+
return false;
192+
},
193+
(arg) =>
189194
{
190195
return false;
191196
});
@@ -207,6 +212,10 @@ private bool IsServerSpanKind(IAttributeValue value)
207212
return false;
208213
},
209214
(arg) =>
215+
{
216+
return false;
217+
},
218+
(arg) =>
210219
{
211220
return false;
212221
});

src/OpenCensus/Trace/AttributeValue.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ public static IAttributeValue<bool> BooleanAttributeValue(bool booleanValue)
4444
return new AttributeValue<bool>(booleanValue);
4545
}
4646

47+
public static IAttributeValue<double> DoubleAttributeValue(double doubleValue)
48+
{
49+
return new AttributeValue<double>(doubleValue);
50+
}
51+
4752
public abstract T Match<T>(
4853
Func<string, T> stringFunction,
4954
Func<bool, T> booleanFunction,
5055
Func<long, T> longFunction,
56+
Func<double, T> doubleFunction,
5157
Func<object, T> defaultFunction);
5258
}
5359
}

src/OpenCensus/Trace/AttributeValue{T}.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public static IAttributeValue<bool> Create(bool booleanValue)
4747
return new AttributeValue<bool>(booleanValue);
4848
}
4949

50+
public static IAttributeValue<double> Create(double doubleValue)
51+
{
52+
return new AttributeValue<double>(doubleValue);
53+
}
54+
5055
public TArg Apply<TArg>(Func<T, TArg> function)
5156
{
5257
return function(this.Value);
@@ -89,25 +94,29 @@ public override TReturn Match<TReturn>(
8994
Func<string, TReturn> stringFunction,
9095
Func<bool, TReturn> booleanFunction,
9196
Func<long, TReturn> longFunction,
97+
Func<double, TReturn> doubleFunction,
9298
Func<object, TReturn> defaultFunction)
9399
{
94100
if (typeof(T) == typeof(string))
95101
{
96102
string value = this.Value as string;
97103
return stringFunction(value);
98-
}
99-
100-
if (typeof(T) == typeof(long))
104+
}
105+
else if (typeof(T) == typeof(long))
101106
{
102107
long val = (long)(object)this.Value;
103108
return longFunction(val);
104109
}
105-
106-
if (typeof(T) == typeof(bool))
110+
else if (typeof(T) == typeof(bool))
107111
{
108112
bool val = (bool)(object)this.Value;
109113
return booleanFunction(val);
110114
}
115+
else if (typeof(T) == typeof(double))
116+
{
117+
double val = (double)(object)this.Value;
118+
return doubleFunction(val);
119+
}
111120

112121
return defaultFunction(this.Value);
113122
}

test/OpenCensus.Tests/Impl/Trace/AttributeValueTest.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,15 @@ public void LongAttributeValue()
5454
});
5555
}
5656

57-
5857
[Fact]
59-
public void AttributeValue_EqualsAndHashCode()
58+
public void DoubleAttributeValue()
6059
{
61-
// EqualsTester tester = new EqualsTester();
62-
// tester.addEqualityGroup(
63-
// AttributeValue.stringAttributeValue("MyStringAttributeValue"),
64-
// AttributeValue.stringAttributeValue("MyStringAttributeValue"));
65-
// tester.addEqualityGroup(AttributeValue.stringAttributeValue("MyStringAttributeDiffValue"));
66-
// tester.addEqualityGroup(
67-
// AttributeValue.booleanAttributeValue(true), AttributeValue.booleanAttributeValue(true));
68-
// tester.addEqualityGroup(AttributeValue.booleanAttributeValue(false));
69-
// tester.addEqualityGroup(
70-
// AttributeValue.longAttributeValue(123456L), AttributeValue.longAttributeValue(123456L));
71-
// tester.addEqualityGroup(AttributeValue.longAttributeValue(1234567L));
72-
// tester.testEquals();
60+
var attribute = AttributeValue<double>.Create(0.005);
61+
attribute.Apply<object>((val) =>
62+
{
63+
Assert.Equal(0.005, val);
64+
return null;
65+
});
7366
}
7467

7568
[Fact]

test/OpenCensus.Tests/Impl/Trace/BlankSpanTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void DoNotCrash()
3939
"MyStringAttributeKey", AttributeValue<string>.Create("MyStringAttributeValue"));
4040
multipleAttributes.Add("MyBooleanAttributeKey", AttributeValue<bool>.Create(true));
4141
multipleAttributes.Add("MyLongAttributeKey", AttributeValue<long>.Create(123));
42+
multipleAttributes.Add("MyDoubleAttributeKey", AttributeValue<double>.Create(0.005));
4243
// Tests only that all the methods are not crashing/throwing errors.
4344
BlankSpan.Instance.PutAttribute(
4445
"MyStringAttributeKey2", AttributeValue<string>.Create("MyStringAttributeValue2"));

test/OpenCensus.Tests/Impl/Trace/LinkTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public LinkTest()
3434
attributesMap.Add("MyAttributeKey0", AttributeValue<string>.Create("MyStringAttribute"));
3535
attributesMap.Add("MyAttributeKey1", AttributeValue<long>.Create(10));
3636
attributesMap.Add("MyAttributeKey2", AttributeValue<bool>.Create(true));
37+
attributesMap.Add("MyAttributeKey3", AttributeValue<double>.Create(0.005));
3738
}
3839

3940
[Fact]

0 commit comments

Comments
 (0)