Skip to content

Commit

Permalink
Merge pull request #20 from elastic/initial-tests
Browse files Browse the repository at this point in the history
Add initial tests project
  • Loading branch information
stevejgordon authored Jan 4, 2024
2 parents b71dcd4 + 0dc011f commit 073bb3b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 10 deletions.
13 changes: 11 additions & 2 deletions Elastic.OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E622CFF2-C6C
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "build", "build\build.fsproj", "{872D0721-A4F2-405C-B0AB-036B340F1907}"
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "build", "build\build.fsproj", "{872D0721-A4F2-405C-B0AB-036B340F1907}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs", "docs\docs.csproj", "{37AF68F4-59EF-4B10-B1A0-87DEEB37CDCA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "docs", "docs\docs.csproj", "{37AF68F4-59EF-4B10-B1A0-87DEEB37CDCA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AAD39891-0B70-47FA-A212-43E1AAE5DF56}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Elastic.OpenTelemetry.Tests", "tests\Elastic.OpenTelemetry.Tests\Elastic.OpenTelemetry.Tests.csproj", "{22BF9223-3A6D-4197-8527-3E4E43A98A81}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -40,13 +44,18 @@ Global
{37AF68F4-59EF-4B10-B1A0-87DEEB37CDCA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37AF68F4-59EF-4B10-B1A0-87DEEB37CDCA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37AF68F4-59EF-4B10-B1A0-87DEEB37CDCA}.Release|Any CPU.Build.0 = Release|Any CPU
{22BF9223-3A6D-4197-8527-3E4E43A98A81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{22BF9223-3A6D-4197-8527-3E4E43A98A81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{22BF9223-3A6D-4197-8527-3E4E43A98A81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{22BF9223-3A6D-4197-8527-3E4E43A98A81}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{79C08F0E-7220-486C-AC0C-E3B287EB0B18} = {E622CFF2-C6C4-40FB-BE42-7C4F2B38B75A}
{32DF4F56-1773-49E9-A8AE-0573F0AA57DE} = {4E95C87B-655B-4BC3-8F2A-DF06B7AAB7E9}
{22BF9223-3A6D-4197-8527-3E4E43A98A81} = {AAD39891-0B70-47FA-A212-43E1AAE5DF56}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {573B2B5F-8CBB-4D52-A55A-4E65E282AAFB}
Expand Down
3 changes: 2 additions & 1 deletion build/exclusion.dic
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cloudfront
stacktrace
otel
otel
otlp
10 changes: 5 additions & 5 deletions src/Elastic.OpenTelemetry/AgentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class AgentBuilder
private readonly string[]? _activitySourceNames;
private Action<TracerProviderBuilder>? _tracerProviderBuilderAction;
private Action<ResourceBuilder>? _resourceBuilderAction;
private Action<OtlpExporterOptions>? _otlpExporerConfiguration;
private string? _otlpExporerName;
private Action<OtlpExporterOptions>? _otlpExporterConfiguration;
private string? _otlpExporterName;

/// <summary>
/// TODO
Expand Down Expand Up @@ -205,7 +205,7 @@ public IAgent Build()

// Add the OTLP exporter configured to ship data to an Elastic backend.
// TODO - What about cases where users want to register processors/exporters after any exporters we add by default (OTLP)?
_tracerProviderBuilder.AddElasticOtlpExporter(_otlpExporerConfiguration, _otlpExporerName);
_tracerProviderBuilder.AddElasticOtlpExporter(_otlpExporterConfiguration, _otlpExporterName);

var tracerProvider = _tracerProviderBuilder.Build();

Expand All @@ -218,8 +218,8 @@ public IAgent Build()
public void ConfigureOtlpExporter(Action<OtlpExporterOptions> configure, string? name = null)
{
ArgumentNullException.ThrowIfNull(configure);
_otlpExporerConfiguration = configure;
_otlpExporerName = name;
_otlpExporterConfiguration = configure;
_otlpExporterName = name;
}

private sealed class Agent(TracerProvider? tracerProvider, MeterProvider? meterProvider) : IAgent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OpenTelemetry;

using System.Diagnostics;

namespace Elastic.OpenTelemetry.Processors;
Expand All @@ -9,6 +8,11 @@ namespace Elastic.OpenTelemetry.Processors;
/// </summary>
public class TransactionIdProcessor : BaseProcessor<Activity>
{
/// <summary>
///
/// </summary>
public const string TransactionIdTagName = "transaction.id";

private readonly AsyncLocal<ActivitySpanId?> _currentTransactionId = new();

/// <inheritdoc cref="OnStart"/>
Expand All @@ -17,6 +21,7 @@ public override void OnStart(Activity activity)
if (activity.Parent == null)
_currentTransactionId.Value = activity.SpanId;

activity.SetTag("transaction.id", _currentTransactionId.Value);
if (_currentTransactionId.Value.HasValue)
activity.SetTag(TransactionIdTagName, _currentTransactionId.Value.Value.ToString());
}
}
34 changes: 34 additions & 0 deletions tests/Elastic.OpenTelemetry.Tests/BasicAgentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Elastic.OpenTelemetry.Tests;

public class TransactionIdProcessorTests
{
[Fact]
public void TransactionId_IsAddedToTags()
{
const string activitySourceName = "TestSource";

var activitySource = new ActivitySource(activitySourceName, "1.0.0");

var exportedItems = new List<Activity>();

using var agent = new AgentBuilder()
.ConfigureTracer(tpb => tpb
.ConfigureResource(rb => rb.AddService("Test", "1.0.0"))
.AddSource(activitySourceName)
.AddInMemoryExporter(exportedItems))
.Build();

using (var activity = activitySource.StartActivity("DoingStuff", ActivityKind.Internal))
{
activity?.SetStatus(ActivityStatusCode.Ok);
}

exportedItems.Should().HaveCount(1);

var exportedActivity = exportedItems[0];

var transactionId = exportedActivity.GetTagItem(TransactionIdProcessor.TransactionIdTagName);

transactionId.Should().NotBeNull().And.BeAssignableTo<string>().Which.Should().NotBeEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RunSettingsFilePath>$(MSBuildProjectDirectory)\test.runsettings</RunSettingsFilePath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="1.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Elastic.OpenTelemetry\Elastic.OpenTelemetry.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions tests/Elastic.OpenTelemetry.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global using Xunit;
global using System.Diagnostics;
global using Elastic.OpenTelemetry.Processors;
global using FluentAssertions;
global using OpenTelemetry.Resources;
global using OpenTelemetry.Trace;
9 changes: 9 additions & 0 deletions tests/Elastic.OpenTelemetry.Tests/test.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<OTEL_EXPORTER_OTLP_ENDPOINT>http://localhost:4318</OTEL_EXPORTER_OTLP_ENDPOINT>
<OTEL_EXPORTER_OTLP_TRACES_PROTOCOL>http/protobuf</OTEL_EXPORTER_OTLP_TRACES_PROTOCOL>
<OTEL_BSP_SCHEDULE_DELAY>10</OTEL_BSP_SCHEDULE_DELAY>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>

0 comments on commit 073bb3b

Please sign in to comment.