Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -256,6 +256,7 @@ public void TrackAvailabilitySendsAvailabilityTelemetryWithAllParameters()
Assert.Contains(logRecord.Attributes, a => a.Key == "microsoft.availability.success" && a.Value?.ToString() == success.ToString());
Assert.Contains(logRecord.Attributes, a => a.Key == "microsoft.availability.runLocation" && a.Value?.ToString() == runLocation);
Assert.Contains(logRecord.Attributes, a => a.Key == "microsoft.availability.message" && a.Value?.ToString() == message);
Assert.Contains(logRecord.Attributes, a => a.Key == "microsoft.availability.testTimestamp" && a.Value?.ToString() == timeStamp.ToString("o"));
Assert.Contains(logRecord.Attributes, a => a.Key == "Environment" && a.Value?.ToString() == "Production");
}

Expand Down Expand Up @@ -286,7 +287,8 @@ public void TrackAvailabilitySendsAvailabilityTelemetryWithMinimalParameters()
[Fact]
public void TrackAvailabilityWithAvailabilityTelemetryObject()
{
var availabilityTelemetry = new AvailabilityTelemetry("TestWithObject", DateTimeOffset.UtcNow, TimeSpan.FromSeconds(3), "North Europe", true, "Success");
var timeStamp = DateTimeOffset.UtcNow;
var availabilityTelemetry = new AvailabilityTelemetry("TestWithObject", timeStamp, TimeSpan.FromSeconds(3), "North Europe", true, "Success");
availabilityTelemetry.Properties["CustomProp"] = "CustomValue";

this.telemetryClient.TrackAvailability(availabilityTelemetry);
Expand All @@ -300,6 +302,9 @@ public void TrackAvailabilityWithAvailabilityTelemetryObject()

// Verify custom property
Assert.Contains(logRecord.Attributes, a => a.Key == "CustomProp" && a.Value?.ToString() == "CustomValue");

// Verify timestamp is preserved
Assert.Contains(logRecord.Attributes, a => a.Key == "microsoft.availability.testTimestamp" && a.Value?.ToString() == timeStamp.ToString("o"));
}

[Fact]
Expand Down Expand Up @@ -357,6 +362,48 @@ public void TrackAvailabilityGeneratesIdIfNotProvided()
Assert.True(Guid.TryParse(idAttr.Value?.ToString(), out _), "ID should be a valid GUID");
}

[Fact]
public void TrackAvailabilityPreservesUserSpecifiedTimestamp()
{
// Use a specific past timestamp to verify it's not overwritten by the current time
var specificTimestamp = new DateTimeOffset(2025, 4, 19, 12, 10, 59, 993, TimeSpan.Zero);
var availabilityTelemetry = new AvailabilityTelemetry("TimestampTest", specificTimestamp, TimeSpan.FromMilliseconds(19.3), "Sweden Central", true);

this.telemetryClient.TrackAvailability(availabilityTelemetry);
this.telemetryClient.Flush();

Assert.True(this.logItems.Count > 0, "At least one log should be collected");
var logRecord = this.logItems.FirstOrDefault(l =>
l.Attributes != null && l.Attributes.Any(a =>
a.Key == "microsoft.availability.name" && a.Value?.ToString() == "TimestampTest"));
Assert.NotNull(logRecord);

// Verify the user-specified timestamp is included as a property
var timestampAttr = logRecord.Attributes.FirstOrDefault(a => a.Key == "microsoft.availability.testTimestamp");
Assert.NotNull(timestampAttr.Value);
Assert.Equal(specificTimestamp.ToString("o"), timestampAttr.Value?.ToString());
}

[Fact]
public void TrackAvailabilityOmitsTimestampWhenDefault()
{
var availabilityTelemetry = new AvailabilityTelemetry();
availabilityTelemetry.Name = "DefaultTimestampTest";
// Don't set Timestamp, it should remain default

this.telemetryClient.TrackAvailability(availabilityTelemetry);
this.telemetryClient.Flush();

Assert.True(this.logItems.Count > 0, "At least one log should be collected");
var logRecord = this.logItems.FirstOrDefault(l =>
l.Attributes != null && l.Attributes.Any(a =>
a.Key == "microsoft.availability.name" && a.Value?.ToString() == "DefaultTimestampTest"));
Assert.NotNull(logRecord);

// Verify that no testTimestamp attribute is present when timestamp is default
Assert.DoesNotContain(logRecord.Attributes, a => a.Key == "microsoft.availability.testTimestamp");
}

#endregion

#region TrackTrace
Expand Down
5 changes: 5 additions & 0 deletions BASE/src/Microsoft.ApplicationInsights/TelemetryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ public void TrackAvailability(AvailabilityTelemetry telemetry)
properties.Add("microsoft.availability.message", telemetry.Message);
}

if (telemetry.Timestamp != default)
{
properties.Add("microsoft.availability.testTimestamp", telemetry.Timestamp.ToString("o"));
Comment thread
rajkumar-rangaraj marked this conversation as resolved.
Outdated
}

// Add custom properties from telemetry
if (telemetry.Properties != null && telemetry.Properties.Count > 0)
{
Expand Down
Loading