Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1 +1,3 @@
#nullable enable
static Microsoft.Testing.Extensions.OpenTelemetry.TestingPlatformResourceDetector.GetCiResourceAttributes() -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object!>>!
static Microsoft.Testing.Extensions.OpenTelemetry.TestingPlatformResourceDetector.GetTestResourceAttributes() -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string!, object!>>!
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,14 @@ public static MeterProviderBuilder AddTestingPlatformInstrumentation(this MeterP
/// Adds the Microsoft Testing Platform resource attributes (test assembly, host, OS, runtime and the detected
/// CI provider, pipeline and commit) to the resource of a tracer, meter or logger provider.
/// </summary>
/// <remarks>Resource attributes are attached once to every span and metric point exported by the provider, which
/// is what lets you slice a dashboard by branch, pipeline or machine without adding those values to every span.
/// <remarks>This is the standalone aggregate convenience path. It configures service identity and adds host, OS,
/// process, test assembly, CI and source-control attributes.
/// <para>Applications that already configure their own service, host, OS or process identity through Aspire
/// ServiceDefaults, <c>HostApplicationBuilder</c> or another application-level OpenTelemetry composition root
/// should use <see cref="AddTestingPlatformTestResource(ResourceBuilder)"/> and
/// <see cref="AddTestingPlatformCiResource(ResourceBuilder)"/> instead.</para>
/// <para>Resource attributes are attached once to every span and metric point exported by the provider, which
/// is what lets you slice a dashboard by branch, pipeline or machine without adding those values to every span.</para>
/// <para>The CI attributes follow the OpenTelemetry <c>cicd.*</c> and <c>vcs.*</c> conventions and are detected
/// from GitHub Actions, Azure Pipelines, GitLab CI and Jenkins environment variables.</para></remarks>
/// <param name="builder">The resource builder to enrich.</param>
Expand All @@ -126,6 +132,39 @@ public static ResourceBuilder AddTestingPlatformResource(this ResourceBuilder bu
.AddAttributes(TestingPlatformResourceDetector.GetResourceAttributes());
}

/// <summary>
/// Adds Microsoft Testing Platform test-specific resource attributes to the resource of a tracer, meter or logger
/// provider.
/// </summary>
/// <remarks>This focused helper currently adds the test assembly name. It does not configure or overwrite
/// <c>service.*</c>, <c>host.*</c>, <c>os.*</c>, <c>process.*</c>, <c>cicd.*</c> or <c>vcs.*</c> attributes,
/// so it can be composed with application-owned OpenTelemetry resource configuration.</remarks>
/// <param name="builder">The resource builder to enrich.</param>
/// <returns>The same <see cref="ResourceBuilder"/> instance.</returns>
public static ResourceBuilder AddTestingPlatformTestResource(this ResourceBuilder builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

return builder.AddAttributes(TestingPlatformResourceDetector.GetTestResourceAttributes());
}

/// <summary>
/// Adds CI and source-control provenance for the current test run to the resource of a tracer, meter or logger
/// provider.
/// </summary>
/// <remarks>The attributes follow the OpenTelemetry <c>cicd.*</c> and <c>vcs.*</c> conventions and are detected
/// from GitHub Actions, Azure Pipelines, GitLab CI and Jenkins environment variables. User-info credentials are
/// removed from repository URLs. This focused helper does not configure or overwrite <c>service.*</c>,
/// <c>host.*</c>, <c>os.*</c>, <c>process.*</c> or <c>test.*</c> attributes.</remarks>
/// <param name="builder">The resource builder to enrich.</param>
/// <returns>The same <see cref="ResourceBuilder"/> instance.</returns>
public static ResourceBuilder AddTestingPlatformCiResource(this ResourceBuilder builder)
{
_ = builder ?? throw new ArgumentNullException(nameof(builder));

return builder.AddAttributes(TestingPlatformResourceDetector.GetCiResourceAttributes());
}

/// <summary>
/// Registers OpenTelemetry tracing and metrics providers configured entirely from the standard <c>OTEL_*</c>
/// environment variables, so a test run can be exported to an observability backend without any code change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This package extends Microsoft.Testing.Platform with:
- **Application-owned diagnostics**: `AddTestingPlatformDiagnostics()` activates the source and meter without constructing or owning an OpenTelemetry SDK provider, so test applications can use the provider configured by Aspire ServiceDefaults or any other host-level observability setup.
- **Semantic conventions**: where an OpenTelemetry convention exists it is used verbatim — `test.case.name`, `test.case.result.status` (upstream `pass`/`fail`), `test.suite.name`, `code.function.name`, `code.file.path`, `code.line.number`, `code.stacktrace`, `error.type`, plus an `exception` span event and an `Error` span status on failures. The pre-existing attribute and instrument names are still emitted by default so existing dashboards keep working; set `TESTINGPLATFORM_OTEL_EMIT_LEGACY_ATTRIBUTES=0` to drop them.
- **Platform extensions**: OpenTelemetry does not define any `test.*` **metrics** or test-case **span** conventions (as of semantic conventions 1.43.0), and `test.case.result.status` upstream only defines `pass` and `fail`. The instruments listed below, the additional result statuses (`skipped`, `error`, `timeout`, `cancelled`, `unknown`), `cicd.provider.name`, and the `test.case.*` attributes not listed above are therefore Microsoft.Testing.Platform extensions, deliberately placed in the namespace where an upstream definition would land.
- **Resource attributes**: `AddTestingPlatformResource()` describes *where* the run happened — test assembly, host, OS, runtime — and detects the CI provider, pipeline run, branch and commit (`cicd.*` / `vcs.*`) from GitHub Actions, Azure Pipelines, GitLab CI and Jenkins.
- **Composable resource attributes**: `AddTestingPlatformTestResource()` adds test-specific identity and `AddTestingPlatformCiResource()` adds CI/source-control provenance without changing application-owned service, host, OS or process identity. `AddTestingPlatformResource()` remains the standalone aggregate convenience path for test assembly, service, host, OS, runtime, CI provider, pipeline run, branch and commit.
- **Turnkey configuration**: `AddOpenTelemetryProviderFromEnvironment()` wires instrumentation, resource and an OTLP exporter purely from the standard `OTEL_*` environment variables, so a run can be exported without writing configuration code.
- **Trace context propagation**: when the process that started the test run publishes a `TRACEPARENT` environment variable (CI runners, `dotnet test`, IDEs), the whole run nests under that trace instead of starting an orphan one.
- **Lifecycle management**: ties the lifetime of a `TracerProvider` and `MeterProvider` to the test application, so they are disposed alongside the test host.
Expand Down Expand Up @@ -47,12 +47,17 @@ Then subscribe the application-owned providers to the MTP source and meter along

```csharp
services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddTestingPlatformTestResource()
.AddTestingPlatformCiResource())
.WithTracing(tracing => tracing.AddTestingPlatformInstrumentation())
.WithMetrics(metrics => metrics.AddTestingPlatformInstrumentation());
```

`AddTestingPlatformDiagnostics()` does not build, configure, flush, or dispose a `TracerProvider` or `MeterProvider`. The application keeps full ownership of those providers and their exporters. The older `AddOpenTelemetryProvider()` and `AddOpenTelemetryProviderFromEnvironment()` helpers remain available as standalone convenience paths when the test application wants MTP to own the provider lifetime.

The focused resource helpers are recommended when Aspire ServiceDefaults, `HostApplicationBuilder`, or another application-level composition root already owns `service.*`, `host.*`, `os.*`, and `process.*`. Use `AddTestingPlatformResource()` only when the test application wants the extension to configure that complete standalone resource identity.

## Emitted metrics

| Instrument | Type | Unit | Description |
Expand Down Expand Up @@ -94,7 +99,7 @@ Telemetry is only ever sent to the exporters and endpoints **you** configure —
| `test.output.stdout` / `test.output.stderr` (`test.stdout` / `test.stderr`) | Captured standard output and error of the test, which routinely contains secrets or environment data. | Off when `TESTINGPLATFORM_OTEL_CAPTURE_TEST_OUTPUT=0`; truncated to `TESTINGPLATFORM_OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT`. |
| `code.stacktrace`, `test.case.result.explanation` (`test.result.explanation`), the `exception` span event (`exception.type` / `exception.message` / `exception.stacktrace`), the span status description, and the legacy `test.result.exception.type` / `test.result.exception.message` / `test.result.exception.stacktrace` | Exception message and stack-trace text. | Truncated to `TESTINGPLATFORM_OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT`. Disable the legacy twins with `TESTINGPLATFORM_OTEL_EMIT_LEGACY_ATTRIBUTES=0`. |
| `test.metadata.*` (`test.metadataProperty.*`) | Framework-supplied trait/metadata values, exported verbatim (not truncated). | Emitted whenever a test carries metadata. Disable the legacy twin with `TESTINGPLATFORM_OTEL_EMIT_LEGACY_ATTRIBUTES=0`. |
| Resource `vcs.repository.url.full`, plus the other resource attributes (`host.name`, `os.description`, and in CI the `cicd.*` / `vcs.*` pipeline, branch and commit — see *Resource attributes* above) | The machine, OS and CI provenance attached to every span and metric point. | User-info credentials in the repository URL (`https://user:token@host/...`) are stripped before export. |
| Resource `vcs.repository.url.full`, plus the other resource attributes (`host.name`, `os.description`, and in CI the `cicd.*` / `vcs.*` pipeline, branch and commit — see *Composable resource attributes* above) | The machine, OS and CI provenance attached to every span and metric point. | User-info credentials in the repository URL (`https://user:token@host/...`) are stripped before export. |

Only the captured output, the result explanation and the exception message and stack trace are truncated to `TESTINGPLATFORM_OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT` (8192 characters by default); all other listed values are exported verbatim unless the Control column notes otherwise, so truncation is a size guard rather than redaction. Set `TESTINGPLATFORM_OTEL_CAPTURE_TEST_OUTPUT=0` on any job whose test output can contain secrets, and prefer sending telemetry to a backend you control.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#nullable enable
static Microsoft.Testing.Extensions.OpenTelemetryProviderExtensions.AddTestingPlatformCiResource(this OpenTelemetry.Resources.ResourceBuilder! builder) -> OpenTelemetry.Resources.ResourceBuilder!
static Microsoft.Testing.Extensions.OpenTelemetryProviderExtensions.AddTestingPlatformDiagnostics(this Microsoft.Testing.Platform.Builder.ITestApplicationBuilder! builder) -> void
static Microsoft.Testing.Extensions.OpenTelemetryProviderExtensions.AddTestingPlatformTestResource(this OpenTelemetry.Resources.ResourceBuilder! builder) -> OpenTelemetry.Resources.ResourceBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Microsoft.Testing.Extensions.OpenTelemetry;

/// <summary>
/// Builds the OpenTelemetry <c>Resource</c> attributes that describe *where* a test run happened.
/// Builds the OpenTelemetry <c>Resource</c> attributes that describe a test run and where it happened.
/// </summary>
/// <remarks>
/// Traces and metrics coming out of a test run are only actionable when you can tell one run apart from another:
Expand All @@ -17,12 +17,17 @@ internal static class TestingPlatformResourceDetector

public static IEnumerable<KeyValuePair<string, object>> GetResourceAttributes()
{
foreach (KeyValuePair<string, object> attribute in GetProcessAttributes())
foreach (KeyValuePair<string, object> attribute in GetHostOsAndProcessAttributes())
{
yield return attribute;
}

foreach (KeyValuePair<string, object> attribute in GetCiAttributes())
foreach (KeyValuePair<string, object> attribute in GetTestResourceAttributes())
{
yield return attribute;
}

foreach (KeyValuePair<string, object> attribute in GetCiResourceAttributes())
{
yield return attribute;
}
Expand All @@ -40,7 +45,7 @@ public static string GetServiceName()
=> Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? Assembly.GetEntryAssembly()?.GetName().Version?.ToString();

private static IEnumerable<KeyValuePair<string, object>> GetProcessAttributes()
private static IEnumerable<KeyValuePair<string, object>> GetHostOsAndProcessAttributes()
{
yield return new("host.name", Environment.MachineName);
yield return new("host.arch", GetHostArchitecture());
Expand All @@ -56,7 +61,10 @@ private static IEnumerable<KeyValuePair<string, object>> GetProcessAttributes()
yield return new("process.pid", GetCurrentProcessId());
yield return new("process.runtime.name", ".NET");
yield return new("process.runtime.description", RuntimeInformation.FrameworkDescription);
}

public static IEnumerable<KeyValuePair<string, object>> GetTestResourceAttributes()
{
if (Assembly.GetEntryAssembly()?.GetName().Name is { } entryAssemblyName)
{
yield return new("test.assembly.name", entryAssemblyName);
Expand Down Expand Up @@ -102,7 +110,7 @@ private static string GetHostArchitecture()
/// is <b>not</b>: as of semantic conventions 1.43.0 there is no attribute identifying the CI system, so this is
/// a platform extension sitting in the namespace where an upstream definition would land.
/// </remarks>
private static IEnumerable<KeyValuePair<string, object>> GetCiAttributes()
public static IEnumerable<KeyValuePair<string, object>> GetCiResourceAttributes()
{
if (IsTrue(Environment.GetEnvironmentVariable("GITHUB_ACTIONS")))
{
Expand Down
Loading
Loading