Skip to content
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
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.UtcDateTime.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.UtcDateTime.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.UtcDateTime.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.UtcDateTime.ToString("o"));
}

// Add custom properties from telemetry
if (telemetry.Properties != null && telemetry.Properties.Count > 0)
{
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- [Fix `TrackAvailability` ignoring user-specified `Timestamp` on `AvailabilityTelemetry`. The timestamp is now emitted as `microsoft.availability.testTimestamp` so downstream exporters can use the correct event time.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3153)
- [Added `netstandard2.0` target framework to `Microsoft.ApplicationInsights` package to support customers with shared libraries targeting netstandard2.0.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3142)
- [Fix `TelemetryClient.Context.Cloud.RoleName` set after construction now applies to all telemetry.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3129)
- [Fix bug where Debug/Trace level logs from TrackTrace API were not emitted to Application Insights](https://github.com/microsoft/ApplicationInsights-dotnet/pull/3121)
Expand Down
Loading