-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SuppressInstrumentation from ActivityListener and DiagnosticSourceLis…
…tener (#1079) * Add SuppressInstrumentation check in the ActivityListener * Add SuppressInstrumentation check in the DiagnosticSourceListner * Fix things post merge * Put AlwaysOn/AlwaysOff sampler check back in the constructor
- Loading branch information
Showing
5 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
test/OpenTelemetry.Tests/Instrumentation/DiagnosticSourceListenerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// <copyright file="DiagnosticSourceListenerTest.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Diagnostics; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Instrumentation.Tests | ||
{ | ||
public class DiagnosticSourceListenerTest | ||
{ | ||
private const string TestSourceName = "TestSourceName"; | ||
private DiagnosticSource diagnosticSource; | ||
private TestListenerHandler testListenerHandler; | ||
private DiagnosticSourceSubscriber testDiagnosticSourceSubscriber; | ||
|
||
public DiagnosticSourceListenerTest() | ||
{ | ||
this.diagnosticSource = new DiagnosticListener(TestSourceName); | ||
this.testListenerHandler = new TestListenerHandler(TestSourceName); | ||
this.testDiagnosticSourceSubscriber = new DiagnosticSourceSubscriber(this.testListenerHandler, null); | ||
this.testDiagnosticSourceSubscriber.Subscribe(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ListenerHandlerIsNotInvokedWhenSuppressInstrumentationTrue(bool suppressInstrumentation) | ||
{ | ||
using var scope = SuppressInstrumentationScope.Begin(suppressInstrumentation); | ||
|
||
var activity = new Activity("Main"); | ||
this.diagnosticSource.StartActivity(activity, null); | ||
this.diagnosticSource.StopActivity(activity, null); | ||
|
||
if (suppressInstrumentation) | ||
{ | ||
Assert.Equal(0, this.testListenerHandler.OnStartInvokedCount); | ||
Assert.Equal(0, this.testListenerHandler.OnStopInvokedCount); | ||
} | ||
else | ||
{ | ||
Assert.Equal(1, this.testListenerHandler.OnStartInvokedCount); | ||
Assert.Equal(1, this.testListenerHandler.OnStopInvokedCount); | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
test/OpenTelemetry.Tests/Instrumentation/TestListenerHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// <copyright file="TestListenerHandler.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System.Diagnostics; | ||
|
||
namespace OpenTelemetry.Instrumentation.Tests | ||
{ | ||
public class TestListenerHandler : ListenerHandler | ||
{ | ||
public int OnStartInvokedCount = 0; | ||
public int OnStopInvokedCount = 0; | ||
public int OnExceptionInvokedCount = 0; | ||
public int OnCustomInvokedCount = 0; | ||
|
||
public TestListenerHandler(string sourceName) | ||
: base(sourceName) | ||
{ | ||
} | ||
|
||
public override void OnStartActivity(Activity activity, object payload) | ||
{ | ||
this.OnStartInvokedCount++; | ||
} | ||
|
||
public override void OnStopActivity(Activity activity, object payload) | ||
{ | ||
this.OnStopInvokedCount++; | ||
} | ||
|
||
public override void OnException(Activity activity, object payload) | ||
{ | ||
this.OnExceptionInvokedCount++; | ||
} | ||
|
||
public override void OnCustom(string name, Activity activity, object payload) | ||
{ | ||
this.OnCustomInvokedCount++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters