-
Notifications
You must be signed in to change notification settings - Fork 785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Metrics pull export mode #2389
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c005741
Improve metrics pulling logic
reyang 95204e8
add test case
reyang b4a3e1a
format code
reyang 6c7fc48
fix format format
reyang ba04ddf
fix nit
reyang f5912a4
update PrometheusExporter to leverage the new logic
reyang 1e5b868
improve test
reyang 86a162b
clean up
reyang 380ea1c
refactor
reyang d64dec7
Merge branch 'main' into reyang/pull-mode
reyang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this as a way for exporter to tell |
||
{ | ||
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; | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alanwest I noticed this while changing the code.
I think we might run into some race condition - if a scraper happens to hit the HTTP server before we could add the reader, what would happen (I guess we will hit exception, which turns into HTTP 500)? I haven't looked into the HTTP server logic.
I think it might be OKAY. A better version could be - we only start the HTTP server once the exporter/reader are fully ready and both are hooked up to the provider.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the reader is not ready, we could modify the http server to still return 200, but with empty metric.
try
{
this.exporter.Collect(Timeout.Infinite)
}
catch(Exporter/Reader not readyexception)
{
exporter.BatchMetrics = default.
}
Or we can wait for some signal before starting HTTP Server.
Will track this as a separate follow up once this PR is merged.