Skip to content

Commit

Permalink
Add support for detecting experimental features in tests, benchmarks,…
Browse files Browse the repository at this point in the history
… examples, and docs.
  • Loading branch information
CodeBlanch committed May 24, 2024
1 parent 0022ab5 commit d7286d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions build/Common.nonprod.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,24 @@

<Message Importance="high" Text="Skipping test execution for '$(TargetPath)' because it does not contain the '$(TargetFramework)' target." Condition="'$(_SkipTests)' == 'true'" />
</Target>

<Target Name="ResolveExposedExperimentalFeatures" BeforeTargets="CoreCompile">
<!-- Note: What this does is look for $(ProjectReferenceName).BuildFlags.txt
files for all the dependencies of the project being compiled. If
ExposeExperimentalFeatures=true is found in those files we set compiler
constants for each project where experimental features were exposed.
Example:
OPENTELEMETRY_EXPORTER_INMEMORY_EXPERIMENTAL_FEATURES_EXPOSED;OPENTELEMETRY_EXPERIMENTAL_FEATURES_EXPOSED;OPENTELEMETRY_API_EXPERIMENTAL_FEATURES_EXPOSED;OPENTELEMETRY_API_PROVIDERBUILDEREXTENSIONS_EXPERIMENTAL_FEATURES_EXPOSED
-->
<ItemGroup>
<DependenciesWithExposedExperimentalFeatures
Include="@(_ResolvedProjectReferencePaths->'%(Filename)')"
Condition="$([System.IO.File]::Exists('%(RootDir)%(Directory)%(Filename).BuildFlags.txt')) AND $([System.IO.File]::ReadAllText('%(RootDir)%(Directory)%(Filename).BuildFlags.txt').Contains('ExposeExperimentalFeatures=true'))" />
<CompilerConstantsForDependenciesWithExposedExperimentalFeatures Include="@(DependenciesWithExposedExperimentalFeatures->'$([System.String]::Copy('%(DependenciesWithExposedExperimentalFeatures.Identity)').ToUpper().Replace('.', '_'))_EXPERIMENTAL_FEATURES_EXPOSED')" />
</ItemGroup>

<PropertyGroup Condition="'@(CompilerConstantsForDependenciesWithExposedExperimentalFeatures)' != ''">
<DefineConstants>$(DefineConstants);@(CompilerConstantsForDependenciesWithExposedExperimentalFeatures)</DefineConstants>
</PropertyGroup>
</Target>
</Project>
13 changes: 13 additions & 0 deletions build/Common.prod.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)opentelemetry-icon-color.png" Pack="true" PackagePath="\" />
<SourceRoot Condition="'$(Deterministic)'=='true'" Include="$(MSBuildThisFileDirectory)/" />
<None Include="$(IntermediateOutputPath)$(ProjectName).BuildFlags.txt"
Link="$(ProjectName).BuildFlags.txt"
CopyToOutputDirectory="PreserveNewest"
Visible="false" />
<!-- Note: This includes all the PublicApiAnalyzers files in projects to make editing easier in the IDE -->
<None Include=".publicApi\**\PublicAPI.*.txt" />
</ItemGroup>
Expand Down Expand Up @@ -82,6 +86,15 @@
<DefineConstants>$(DefineConstants);EXPOSE_EXPERIMENTAL_FEATURES</DefineConstants>
</PropertyGroup>

<!-- Note: We write a file into the output dir which captures the state of
ExposeExperimentalFeatures for the current compilation. This is used by
consuming projects (tests, benchmarks, examples, etc) to add compiler
constants which can be used to detect experimental APIs -->
<WriteLinesToFile
File="$(OutputPath)$(ProjectName).BuildFlags.txt"
Lines="ExposeExperimentalFeatures=$(ExposeExperimentalFeatures)"
Overwrite="true"/>

<!-- Note: This selects the correct PublicApiAnalyzers files based on $(ExposeExperimentalFeatures) -->
<ItemGroup>
<AdditionalFiles Include=".publicApi\Stable\PublicAPI.*.txt" />
Expand Down
4 changes: 4 additions & 0 deletions test/OpenTelemetry.Api.Tests/Logs/LoggerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ private sealed class NoopLoggerProvider : LoggerProvider

private sealed class TestLoggerProvider : LoggerProvider
{
#if OPENTELEMETRY_API_EXPERIMENTAL_FEATURES_EXPOSED
protected override bool TryCreateLogger(
#else
internal override bool TryCreateLogger(
#endif
string? name,
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
[NotNullWhen(true)]
Expand Down

0 comments on commit d7286d4

Please sign in to comment.