Skip to content

Azure Monitor Exporter: consume microsoft.availability.testTimestamp to override envelope time#57550

Merged
rajkumar-rangaraj merged 3 commits intomainfrom
copilot/update-azure-monitor-exporter
Mar 27, 2026
Merged

Azure Monitor Exporter: consume microsoft.availability.testTimestamp to override envelope time#57550
rajkumar-rangaraj merged 3 commits intomainfrom
copilot/update-azure-monitor-exporter

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

Implements support for the new microsoft.availability.testTimestamp attribute introduced in microsoft/ApplicationInsights-dotnet#3153. When this ISO 8601 attribute is present on an availability log record, the exporter now uses it as the TelemetryItem envelope time instead of the wall-clock logRecord.Timestamp, preserving the user-specified test start time end-to-end.

Changes

  • LogsHelper.cs

    • Added AvailabilityTestTimestampAttributeName constant and TestTimestamp property to AvailabilityInfo struct
    • Added explicit case for the attribute in ExtractAvailabilityInfo — prevents it leaking into AvailabilityData.Properties via the default: branch
    • OtelToAzureMonitorLogs: availability branch parses TestTimestamp using CultureInfo.InvariantCulture for deterministic culture-independent parsing and normalizes the result to UTC via .ToUniversalTime(); falls back to logRecord.Timestamp if absent or unparseable
  • TelemetryItem.cs

    • Added constructor overload TelemetryItem(string name, DateTimeOffset envelopeTime, LogRecord logRecord, ...) accepting an explicit envelope time
    • Existing LogRecord constructor delegates to the new overload (no duplication)
  • AvailabilityTests.cs

    • VerifyAvailabilityTelemetryWithTestTimestamp: asserts TelemetryItem.Time equals the parsed (UTC-normalized, culture-invariant) timestamp and attribute is absent from AvailabilityData.Properties
    • VerifyAvailabilityTelemetryWithoutTestTimestamp: asserts existing behavior (envelope time from logRecord.Timestamp) is unchanged when attribute is absent

This checklist is used to make sure that common guidelines for a pull request are followed.

General Guidelines

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

SDK Generation Guidelines

  • If an SDK is being regenerated based on a new swagger spec, a link to the pull request containing these swagger spec changes has been included above.
  • The generate.cmd file for the SDK has been updated with the version of AutoRest, as well as the commitid of your swagger spec or link to the swagger spec, used to generate the code.
  • The *.csproj and AssemblyInfo.cs files have been updated with the new version of the SDK.
Original prompt

Background

microsoft/ApplicationInsights-dotnet#3153 adds a new attribute microsoft.availability.testTimestamp (ISO 8601 string) to the log record properties when AvailabilityTelemetry.Timestamp != default. This allows the downstream exporter to use the user-specified test start time as the envelope timestamp instead of the wall-clock time of the log record.

The Azure Monitor Exporter must be updated to consume this new attribute.


Files to change

1. sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs

a) Add new constant alongside the other availability attribute constants (~line 41):

private const string AvailabilityTestTimestampAttributeName = "microsoft.availability.testTimestamp";

b) Add a field to AvailabilityInfo struct (at the bottom of the file, ~line 436):

public string? TestTimestamp { get; set; }

c) In ExtractAvailabilityInfo, add a local variable string? availabilityTestTimestamp = null; alongside the others, and handle it in the switch:

case AvailabilityTestTimestampAttributeName:
    availabilityTestTimestamp = item.Value?.ToString();
    break;

Also populate it when constructing the return value:

TestTimestamp = availabilityTestTimestamp,

This ensures the attribute is explicitly consumed and not leaked into AvailabilityData.Properties via the default: branch.


2. sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/TelemetryItem.cs

The TelemetryItem constructor used for log records (line ~139) always derives the envelope Time from logRecord.Timestamp. Since Time is a get-only property set in the base constructor, overriding it requires a different approach.

Add a new constructor overload that accepts an explicit DateTimeOffset for the time to be used when testTimestamp is available:

public TelemetryItem(string name, DateTimeOffset envelopeTime, LogRecord logRecord, AzureMonitorResource? resource, string instrumentationKey, LogContextInfo logContext) :
    this(name, envelopeTime)
{
    // ... same body as the existing LogRecord constructor ...
}

Or alternatively, modify the existing OtelToAzureMonitorLogs method in LogsHelper.cs to override Time after construction — but since Time is readonly, the constructor overload approach is needed.


3. sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.csOtelToAzureMonitorLogs

In the else if (availabilityInfo is not null) branch (~line 111), use the testTimestamp to set the envelope time:

else if (availabilityInfo is not null)
{
    DateTimeOffset envelopeTime = availabilityInfo.Value.TestTimestamp != null
        && DateTimeOffset.TryParse(
            availabilityInfo.Value.TestTimestamp,
            null,
            System.Globalization.DateTimeStyles.RoundtripKind,
            out var parsedTs)
        ? parsedTs
        : TelemetryItem.FormatUtcTimestamp(logRecord.Timestamp);

    telemetryItem = new TelemetryItem("Availability", envelopeTime, logRecord, resource, instrumentationKey, logContext)
    {
        Data = new MonitorBase
        {
            BaseType = "AvailabilityData",
            BaseData = new AvailabilityData(Version, availabilityInfo.Value, properties, logRecord),
        }
    };
    telemetrySchemaTypeCounter._availabilityCount++;
}

Note: TelemetryItem.FormatUtcTimestamp is currently internal static — make sure it remains accessible from LogsHelper.cs (it's already in the same assembly so this is fine).


4. Tests — sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/E2ETelemetryItemValidation/AvailabilityTests.cs

Add a new test VerifyAvailabilityTelemetryWithTestTimestamp that:

  1. Logs an availability record that includes microsoft.availability.testTimestamp with a specific past timestamp (e.g., "2025-04-19T12:10:59.9930000+00:00").
  2. Asserts that the resulting TelemetryItem.Time equals that parsed timestamp.
  3. Asserts that microsoft.availability.testTimestamp does not appear in AvailabilityData.Properties.

Also add a test VerifyAvailabilityTelemetryWithoutTestTimestamp that verifies the existing behavior is unchanged when the attribute is absent (envelope time comes from logRecord.Timestamp).


Key constraint

The microsoft.availability.testTimestamp attribute must be consumed (not passed through to AvailabilityData.Properties). The default: branch in ExtractAvailabilityInfo adds any unrecognized attribute to properties. Adding an explicit case for the new attribute name prevents this leakage.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: @microsoft/ApplicationInsights-dotnet/pull/3153 - A new attribute is added to ava...

This pull request was created from Copilot chat.


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

…re Monitor Exporter

Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-net/sessions/6086c808-de3e-457a-a15e-96232608c738

Co-authored-by: rajkumar-rangaraj <9479006+rajkumar-rangaraj@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Azure Monitor Exporter to consume test timestamp attribute Azure Monitor Exporter: consume microsoft.availability.testTimestamp to override envelope time Mar 27, 2026
Copilot AI requested a review from rajkumar-rangaraj March 27, 2026 17:56
@rajkumar-rangaraj rajkumar-rangaraj marked this pull request as ready for review March 27, 2026 18:03
Copilot AI review requested due to automatic review settings March 27, 2026 18:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Azure Monitor OpenTelemetry Exporter to honor a new microsoft.availability.testTimestamp log attribute by using it as the Availability telemetry envelope timestamp, preserving the original test start time end-to-end.

Changes:

  • Consume microsoft.availability.testTimestamp during availability attribute extraction and prevent it from leaking into AvailabilityData.Properties.
  • Add a TelemetryItem constructor overload to allow explicitly setting the envelope Time for log-based telemetry.
  • Add E2E validation tests covering both presence and absence of testTimestamp.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/LogsHelper.cs Parses and applies testTimestamp for Availability envelope time; consumes the attribute during extraction.
sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/TelemetryItem.cs Adds constructor overload to set envelope time explicitly for log-based telemetry items.
sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/tests/Azure.Monitor.OpenTelemetry.Exporter.Tests/E2ETelemetryItemValidation/AvailabilityTests.cs Adds coverage verifying envelope time override and no property leakage.

@rajkumar-rangaraj
Copy link
Copy Markdown
Member

@copilot apply changes based on the comments in this thread

…estTimestamp

Agent-Logs-Url: https://github.com/Azure/azure-sdk-for-net/sessions/971c0e48-fb38-4772-a6fe-9636dfc1a589

Co-authored-by: rajkumar-rangaraj <9479006+rajkumar-rangaraj@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 27, 2026

@copilot apply changes based on the comments in this thread

Applied both review suggestions in commit 45d3e2c:

  • LogsHelper.cs: changed nullCultureInfo.InvariantCulture for DateTimeOffset.TryParse, and added .ToUniversalTime() to normalize the result to UTC.
  • AvailabilityTests.cs: changed nullCultureInfo.InvariantCulture for DateTimeOffset.Parse (and aligned expectedTime with the same UTC normalization).

@rajkumar-rangaraj rajkumar-rangaraj enabled auto-merge (squash) March 27, 2026 19:31
@rajkumar-rangaraj
Copy link
Copy Markdown
Member

/check-enforcer override

@rajkumar-rangaraj rajkumar-rangaraj merged commit 4c57a5b into main Mar 27, 2026
23 checks passed
@rajkumar-rangaraj rajkumar-rangaraj deleted the copilot/update-azure-monitor-exporter branch March 27, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants