-
Notifications
You must be signed in to change notification settings - Fork 857
Improve Metrics pull export mode #2389
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
Changes from 6 commits
c005741
95204e8
b4a3e1a
6c7fc48
ba04ddf
f5912a4
1e5b868
86a162b
380ea1c
d64dec7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // <copyright file="MetricExporterExtensions.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.Threading; | ||
|
|
||
| namespace OpenTelemetry.Metrics | ||
| { | ||
| /// <summary> | ||
| /// Contains extension methods for the <see cref="BaseExporter{Metric}"/> class. | ||
| /// </summary> | ||
| public static class MetricExporterExtensions | ||
| { | ||
| /// <summary> | ||
| /// Attempts to collect the metrics, blocks the current thread until | ||
| /// metrics collection completed or timed out. | ||
| /// </summary> | ||
| /// <param name="exporter">BaseExporter{Metric} instance on which Collect will be called.</param> | ||
| /// <param name="reader">BaseExportingMetricReader instance on which Collect will be called.</param> | ||
| /// <param name="timeoutMilliseconds"> | ||
| /// The number (non-negative) of milliseconds to wait, or | ||
| /// <c>Timeout.Infinite</c> to wait indefinitely. | ||
| /// </param> | ||
| /// <returns> | ||
| /// Returns <c>true</c> when metrics collection succeeded; otherwise, <c>false</c>. | ||
| /// </returns> | ||
| /// <exception cref="System.ArgumentOutOfRangeException"> | ||
| /// Thrown when the <c>timeoutMilliseconds</c> is smaller than -1. | ||
| /// </exception> | ||
| /// <remarks> | ||
| /// This function guarantees thread-safety. | ||
| /// </remarks> | ||
| public static bool Collect(this BaseExporter<Metric> exporter, BaseExportingMetricReader reader, int timeoutMilliseconds = Timeout.Infinite) | ||
|
||
| { | ||
| if (exporter == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(exporter)); | ||
| } | ||
|
|
||
| if (reader == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(reader)); | ||
| } | ||
|
|
||
| if (timeoutMilliseconds < 0 && timeoutMilliseconds != Timeout.Infinite) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(timeoutMilliseconds), timeoutMilliseconds, "timeoutMilliseconds should be non-negative or Timeout.Infinite."); | ||
| } | ||
|
|
||
| using (PullMetricScope.Begin()) | ||
reyang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return reader.Collect(timeoutMilliseconds); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // <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 System.Runtime.CompilerServices; | ||
| using OpenTelemetry.Context; | ||
|
|
||
| namespace OpenTelemetry | ||
| { | ||
| internal sealed class PullMetricScope : IDisposable | ||
|
Member
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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // <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; | ||
| } | ||
|
|
||
| using var meterProvider = Sdk.CreateMeterProviderBuilder() | ||
| .AddMetricReader(new BaseExportingMetricReader(exporter)) | ||
| .Build(); | ||
|
|
||
| var result = meterProvider.ForceFlush(); | ||
|
|
||
| switch (mode) | ||
| { | ||
| case ExportModes.Push: | ||
| Assert.True(result); | ||
| break; | ||
| case ExportModes.Pull: | ||
| Assert.False(result); | ||
| break; | ||
| case ExportModes.Pull | ExportModes.Push: | ||
| Assert.True(result); | ||
| 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> | ||
| { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.