-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Metrics pull export mode (#2389)
- Loading branch information
Showing
8 changed files
with
235 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// <copyright file="IPullMetricExporter.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; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
/// <summary> | ||
/// Describes a type of <see cref="BaseExporter{Metric}"/> which supports <see cref="ExportModes.Pull"/>. | ||
/// </summary> | ||
public interface IPullMetricExporter | ||
{ | ||
Func<int, bool> Collect { get; set; } | ||
} | ||
} |
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,52 @@ | ||
// <copyright file="PullMetricScope.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; | ||
using OpenTelemetry.Context; | ||
|
||
namespace OpenTelemetry.Metrics | ||
{ | ||
internal sealed class PullMetricScope : IDisposable | ||
{ | ||
private static readonly RuntimeContextSlot<bool> Slot = RuntimeContext.RegisterSlot<bool>("otel.pull_metric"); | ||
|
||
private readonly bool previousValue; | ||
private bool disposed; | ||
|
||
internal PullMetricScope(bool value = true) | ||
{ | ||
this.previousValue = Slot.Get(); | ||
Slot.Set(value); | ||
} | ||
|
||
internal static bool IsPullAllowed => Slot.Get(); | ||
|
||
public static IDisposable Begin(bool value = true) | ||
{ | ||
return new PullMetricScope(value); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
if (!this.disposed) | ||
{ | ||
Slot.Set(this.previousValue); | ||
this.disposed = true; | ||
} | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
test/OpenTelemetry.Tests/Metrics/MetricExporterTests.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,105 @@ | ||
// <copyright file="MetricExporterTests.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; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Metrics; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Metrics.Tests | ||
{ | ||
public class MetricExporterTests | ||
{ | ||
[Theory] | ||
[InlineData(ExportModes.Push)] | ||
[InlineData(ExportModes.Pull)] | ||
[InlineData(ExportModes.Pull | ExportModes.Push)] | ||
public void FlushMetricExporterTest(ExportModes mode) | ||
{ | ||
BaseExporter<Metric> exporter = null; | ||
|
||
switch (mode) | ||
{ | ||
case ExportModes.Push: | ||
exporter = new PushOnlyMetricExporter(); | ||
break; | ||
case ExportModes.Pull: | ||
exporter = new PullOnlyMetricExporter(); | ||
break; | ||
case ExportModes.Pull | ExportModes.Push: | ||
exporter = new PushPullMetricExporter(); | ||
break; | ||
} | ||
|
||
var reader = new BaseExportingMetricReader(exporter); | ||
using var meterProvider = Sdk.CreateMeterProviderBuilder() | ||
.AddMetricReader(reader) | ||
.Build(); | ||
|
||
switch (mode) | ||
{ | ||
case ExportModes.Push: | ||
Assert.True(reader.Collect()); | ||
Assert.True(meterProvider.ForceFlush()); | ||
break; | ||
case ExportModes.Pull: | ||
Assert.False(reader.Collect()); | ||
Assert.False(meterProvider.ForceFlush()); | ||
Assert.True((exporter as IPullMetricExporter).Collect(-1)); | ||
break; | ||
case ExportModes.Pull | ExportModes.Push: | ||
Assert.True(reader.Collect()); | ||
Assert.True(meterProvider.ForceFlush()); | ||
break; | ||
} | ||
} | ||
|
||
[ExportModes(ExportModes.Push)] | ||
private class PushOnlyMetricExporter : BaseExporter<Metric> | ||
{ | ||
public override ExportResult Export(in Batch<Metric> batch) | ||
{ | ||
return ExportResult.Success; | ||
} | ||
} | ||
|
||
[ExportModes(ExportModes.Pull)] | ||
private class PullOnlyMetricExporter : BaseExporter<Metric>, IPullMetricExporter | ||
{ | ||
private Func<int, bool> funcCollect; | ||
|
||
public Func<int, bool> Collect | ||
{ | ||
get => this.funcCollect; | ||
set { this.funcCollect = value; } | ||
} | ||
|
||
public override ExportResult Export(in Batch<Metric> batch) | ||
{ | ||
return ExportResult.Success; | ||
} | ||
} | ||
|
||
[ExportModes(ExportModes.Pull | ExportModes.Push)] | ||
private class PushPullMetricExporter : BaseExporter<Metric> | ||
{ | ||
public override ExportResult Export(in Batch<Metric> batch) | ||
{ | ||
return ExportResult.Success; | ||
} | ||
} | ||
} | ||
} |