-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a hook to call OpenTelemetry.SuppressInstrumentationScope.Beg…
…in before exporting HTTP or gRPC payloads
- Loading branch information
1 parent
ec43a5d
commit aafb957
Showing
9 changed files
with
160 additions
and
7 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
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
70 changes: 70 additions & 0 deletions
70
...g.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Exporters/InstrumentationSuppressingExporter.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,70 @@ | ||
// Copyright © Serilog Contributors | ||
// | ||
// 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. | ||
|
||
using OpenTelemetry.Proto.Collector.Logs.V1; | ||
using OpenTelemetry.Proto.Collector.Trace.V1; | ||
|
||
namespace Serilog.Sinks.OpenTelemetry.Exporters; | ||
|
||
/// <summary> | ||
/// Uses a callback to suppress instrumentation while telemetry is being exported by the wrapped exporter. | ||
/// </summary> | ||
sealed class InstrumentationSuppressingExporter : IExporter, IDisposable | ||
{ | ||
readonly IExporter _exporter; | ||
readonly Func<IDisposable> _onBeginSuppressInstrumentation; | ||
|
||
public InstrumentationSuppressingExporter(IExporter exporter, Func<IDisposable> onBeginSuppressInstrumentation) | ||
{ | ||
_exporter = exporter; | ||
_onBeginSuppressInstrumentation = onBeginSuppressInstrumentation; | ||
} | ||
|
||
public void Export(ExportLogsServiceRequest request) | ||
{ | ||
using (_onBeginSuppressInstrumentation()) | ||
{ | ||
_exporter.Export(request); | ||
} | ||
} | ||
|
||
public async Task ExportAsync(ExportLogsServiceRequest request) | ||
{ | ||
using (_onBeginSuppressInstrumentation()) | ||
{ | ||
await _exporter.ExportAsync(request); | ||
} | ||
} | ||
|
||
public void Export(ExportTraceServiceRequest request) | ||
{ | ||
using (_onBeginSuppressInstrumentation()) | ||
{ | ||
_exporter.Export(request); | ||
} | ||
} | ||
|
||
public async Task ExportAsync(ExportTraceServiceRequest request) | ||
{ | ||
using (_onBeginSuppressInstrumentation()) | ||
{ | ||
await _exporter.ExportAsync(request); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
(_exporter as IDisposable)?.Dispose(); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
test/Serilog.Sinks.OpenTelemetry.Tests/InstrumentationSuppressingExporterTests.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,24 @@ | ||
using OpenTelemetry.Proto.Collector.Logs.V1; | ||
using Serilog.Sinks.OpenTelemetry.Exporters; | ||
using Serilog.Sinks.OpenTelemetry.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.OpenTelemetry.Tests; | ||
|
||
public class InstrumentationSuppressingExporterTests | ||
{ | ||
[Fact] | ||
public void RequestsAreNotInstrumentedWhenSuppressed() | ||
{ | ||
var exporter = new CollectingExporter(); | ||
|
||
exporter.Export(new ExportLogsServiceRequest()); | ||
Assert.Equal(1, exporter.InstrumentedRequestCount); | ||
Assert.Single(exporter.ExportLogsServiceRequests); | ||
|
||
var wrapper = new InstrumentationSuppressingExporter(exporter, TestSuppressInstrumentationScope.Begin); | ||
wrapper.Export(new ExportLogsServiceRequest()); | ||
Assert.Equal(1, exporter.InstrumentedRequestCount); | ||
Assert.Equal(2, exporter.ExportLogsServiceRequests.Count); | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
test/Serilog.Sinks.OpenTelemetry.Tests/Support/TestSuppressInstrumentationScope.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,22 @@ | ||
namespace Serilog.Sinks.OpenTelemetry.Tests.Support; | ||
|
||
class TestSuppressInstrumentationScope: IDisposable | ||
{ | ||
static readonly AsyncLocal<int> Depth = new(); | ||
bool _disposed; | ||
|
||
public static bool IsSuppressed => Depth.Value > 0; | ||
|
||
public static IDisposable Begin() | ||
{ | ||
Depth.Value++; | ||
return new TestSuppressInstrumentationScope(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) return; | ||
_disposed = true; | ||
Depth.Value--; | ||
} | ||
} |