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

(WIP) Separate property bag of TelemetryContext and ISupportProperties #814

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void SerializeWritesTimestampAsExpectedByEndpoint()
var expected = new TTelemetry { Timestamp = DateTimeOffset.UtcNow };
expected.Sanitize();

TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TTelemetry, TEndpointData>(expected);
TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TEndpointData>(expected);


Assert.AreEqual<DateTimeOffset>(expected.Timestamp, DateTimeOffset.Parse(actual.time, null, System.Globalization.DateTimeStyles.AssumeUniversal));
Expand All @@ -166,7 +166,7 @@ private void SerializeWritesSequenceAsExpectedByEndpoint()
var expected = new TTelemetry { Sequence = "4:2" };
expected.Sanitize();

TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TTelemetry, TEndpointData>(expected);
TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TEndpointData>(expected);

Assert.AreEqual(expected.Sequence, actual.seq);
}
Expand All @@ -177,7 +177,7 @@ private void SerializeWritesInstrumentationKeyAsExpectedByEndpoint()
expected.Context.InstrumentationKey = Guid.NewGuid().ToString();
expected.Sanitize();

TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TTelemetry, TEndpointData>(expected);
TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TEndpointData>(expected);

Assert.AreEqual(expected.Context.InstrumentationKey, actual.iKey);
}
Expand All @@ -187,7 +187,7 @@ private void SerializeWritesDataBaseTypeAsExpectedByEndpoint()
var telemetry = new TTelemetry();
telemetry.Sanitize();

TelemetryItem<TEndpointData> envelope = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TTelemetry, TEndpointData>(telemetry);
TelemetryItem<TEndpointData> envelope = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<TEndpointData>(telemetry);

string expectedBaseType = ExtractTelemetryNameFromType(typeof(TTelemetry)) + "Data";
Assert.AreEqual(expectedBaseType, envelope.data.baseType);
Expand All @@ -200,7 +200,7 @@ private void SerializeWritesTelemetryNameAsExpectedByEndpoint()
expected.Sanitize();

TelemetryItem<TEndpointData> actual = TelemetryItemTestHelper
.SerializeDeserializeTelemetryItem<TTelemetry, TEndpointData>(expected);
.SerializeDeserializeTelemetryItem<TEndpointData>(expected);

Assert.AreEqual(
Constants.TelemetryNamePrefix + "312cbd799dbb4c48a7da3cc2a931cb71." + this.ExtractTelemetryNameFromType(typeof(TTelemetry)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,32 @@
[TestClass]
public class AvailabilityTelemetryTest
{
[TestMethod]
public void AvailabilityTelemetryPropertiesFromContextAndItemSerializesToPropertiesInJson()
{
var expected = CreateAvailabilityTelemetry();

((ITelemetry)expected).Sanitize();

Assert.AreEqual(1, expected.Properties.Count);
Assert.AreEqual(1, expected.Context.Properties.Count);

Assert.IsTrue(expected.Properties.ContainsKey("TestProperty"));
Assert.IsTrue(expected.Context.Properties.ContainsKey("contextpropkey"));

var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.AvailabilityData>(expected);

// Items added to both availability.Properties, and availability.Context.Properties are serialized to properties.
Assert.AreEqual(2, item.data.baseData.properties.Count);
Assert.IsTrue(item.data.baseData.properties.ContainsKey("contextpropkey"));
Assert.IsTrue(item.data.baseData.properties.ContainsKey("TestProperty"));
}

[TestMethod]
public void AvailabilityTelemetrySerializesToJson()
{
AvailabilityTelemetry expected = this.CreateAvailabilityTelemetry();
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AvailabilityTelemetry, AI.AvailabilityData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.AvailabilityData>(expected);

Assert.AreEqual<DateTimeOffset>(expected.Timestamp, DateTimeOffset.Parse(item.time, null, System.Globalization.DateTimeStyles.AssumeUniversal));
Assert.AreEqual(expected.Sequence, item.seq);
Expand Down Expand Up @@ -137,6 +158,7 @@ private AvailabilityTelemetry CreateAvailabilityTelemetry()
Success = true
};
item.Context.InstrumentationKey = Guid.NewGuid().ToString();
item.Context.Properties.Add("contextpropkey", "contextpropvalue");
item.Properties.Add("TestProperty", "TestValue");
item.Sequence = "12";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,32 @@ public void VerifyExpectedDefaultValue()
Assert.AreEqual(true, defaultDependencyTelemetry.Success, "Success is expected to be true");
}

[TestMethod]
public void DependencyTelemetryPropertiesFromContextAndItemSerializesToPropertiesInJson()
{
var expected = CreateRemoteDependencyTelemetry();

((ITelemetry)expected).Sanitize();

Assert.AreEqual(1, expected.Properties.Count);
Assert.AreEqual(1, expected.Context.Properties.Count);

Assert.IsTrue(expected.Properties.ContainsKey("TestProperty"));
Assert.IsTrue(expected.Context.Properties.ContainsKey("contextpropkey"));

var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.AvailabilityData>(expected);

// Items added to both dependency.Properties, and dependency.Context.Properties are serialized to properties.
Assert.AreEqual(2, item.data.baseData.properties.Count);
Assert.IsTrue(item.data.baseData.properties.ContainsKey("contextpropkey"));
Assert.IsTrue(item.data.baseData.properties.ContainsKey("TestProperty"));
}

[TestMethod]
public void RemoteDependencyTelemetrySerializesToJson()
{
DependencyTelemetry expected = this.CreateRemoteDependencyTelemetry();
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(expected);

Assert.AreEqual<DateTimeOffset>(expected.Timestamp, DateTimeOffset.Parse(item.time, null, System.Globalization.DateTimeStyles.AssumeUniversal));
Assert.AreEqual(expected.Sequence, item.seq);
Expand All @@ -54,7 +75,7 @@ public void SerializeWritesNullValuesAsExpectedByEndpoint()
original.Type = null;
original.Success = null;
((ITelemetry)original).Sanitize();
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(original);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(original);

Assert.AreEqual(2, item.data.baseData.ver);
}
Expand All @@ -64,7 +85,7 @@ public void RemoteDependencyTelemetrySerializesStructuredIKeyToJsonCorrectlyPres
{
DependencyTelemetry expected = this.CreateRemoteDependencyTelemetry();
expected.Context.InstrumentationKey = "AIC-" + expected.Context.InstrumentationKey;
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(expected);

Assert.AreEqual(expected.Context.InstrumentationKey, item.iKey);
}
Expand All @@ -73,7 +94,7 @@ public void RemoteDependencyTelemetrySerializesStructuredIKeyToJsonCorrectlyPres
public void RemoteDependencyTelemetrySerializeCommandNameToJson()
{
DependencyTelemetry expected = this.CreateRemoteDependencyTelemetry("Select * from Customers where CustomerID=@1");
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(expected);
AI.RemoteDependencyData dp = item.data.baseData;
Assert.AreEqual(expected.Data, dp.data);
}
Expand All @@ -82,7 +103,7 @@ public void RemoteDependencyTelemetrySerializeCommandNameToJson()
public void RemoteDependencyTelemetrySerializeNullCommandNameToJson()
{
DependencyTelemetry expected = this.CreateRemoteDependencyTelemetry(null);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(expected);
AI.RemoteDependencyData dp = item.data.baseData;
Assert.IsTrue(string.IsNullOrEmpty(dp.data));
}
Expand All @@ -91,7 +112,7 @@ public void RemoteDependencyTelemetrySerializeNullCommandNameToJson()
public void RemoteDependencyTelemetrySerializeEmptyCommandNameToJson()
{
DependencyTelemetry expected = this.CreateRemoteDependencyTelemetry(string.Empty);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(expected);
AI.RemoteDependencyData dp = item.data.baseData;
Assert.IsTrue(string.IsNullOrEmpty(dp.data));
}
Expand Down Expand Up @@ -162,7 +183,7 @@ public void DependencyTelemetryHasCorrectValueOfSamplingPercentageAfterSerializa
var telemetry = this.CreateRemoteDependencyTelemetry("mycommand");
((ISupportSampling)telemetry).SamplingPercentage = 10;

var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<DependencyTelemetry, AI.RemoteDependencyData>(telemetry);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.RemoteDependencyData>(telemetry);

Assert.AreEqual(10, item.sampleRate);
}
Expand Down Expand Up @@ -226,6 +247,7 @@ private DependencyTelemetry CreateRemoteDependencyTelemetry()
Type = "external call"
};
item.Context.InstrumentationKey = Guid.NewGuid().ToString();
item.Context.Properties.Add("contextpropkey", "contextpropvalue");
item.Properties.Add("TestProperty", "TestValue");

return item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void EventTelemetrySerializesToJsonCorrectly()
expected.Properties["Test Property"] = "Test Value";
expected.Metrics["Test Property"] = 4.2;

var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<EventTelemetry, AI.EventData>(expected);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.EventData>(expected);

// NOTE: It's correct that we use the v1 name here, and therefore we test against it.
Assert.AreEqual(AI.ItemType.Event, item.name);
Expand All @@ -73,11 +73,33 @@ public void SerializeWritesNullValuesAsExpectedByEndpoint()
EventTelemetry original = new EventTelemetry();
original.Name = null;
((ITelemetry)original).Sanitize();
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<EventTelemetry, AI.EventData>(original);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.EventData>(original);

Assert.AreEqual(2, item.data.baseData.ver);
}

[TestMethod]
public void EventTelemetryPropertiesFromContextAndItemSerializesToPropertiesInJson()
{
var expected = new EventTelemetry();
expected.Context.Properties.Add("contextpropkey", "contextpropvalue");
expected.Properties.Add("TestProperty", "TestPropertyValue");
((ITelemetry)expected).Sanitize();

Assert.AreEqual(1, expected.Properties.Count);
Assert.AreEqual(1, expected.Context.Properties.Count);

Assert.IsTrue(expected.Properties.ContainsKey("TestProperty"));
Assert.IsTrue(expected.Context.Properties.ContainsKey("contextpropkey"));

var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.EventData>(expected);

// Items added to both event.Properties, and event.Context.Properties are serialized to properties.
Assert.AreEqual(2, item.data.baseData.properties.Count);
Assert.IsTrue(item.data.baseData.properties.ContainsKey("contextpropkey"));
Assert.IsTrue(item.data.baseData.properties.ContainsKey("TestProperty"));
}

[TestMethod]
public void SanitizeWillTrimAppropriateFields()
{
Expand Down Expand Up @@ -145,7 +167,7 @@ public void EventTelemetryHasCorrectValueOfSamplingPercentageAfterSerialization(
var telemetry = new EventTelemetry("my event");
((ISupportSampling)telemetry).SamplingPercentage = 10;

var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<EventTelemetry, AI.EventData>(telemetry);
var item = TelemetryItemTestHelper.SerializeDeserializeTelemetryItem<AI.EventData>(telemetry);

Assert.AreEqual(10, item.sampleRate);
}
Expand Down
Loading