-
Notifications
You must be signed in to change notification settings - Fork 705
testing: add in-memory metrics exporter #653
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 7 commits
b914995
c120465
b5d051e
7580c24
8b83594
22940e8
72ace01
9ee675e
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,58 @@ | ||
# 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. | ||
|
||
import threading | ||
from typing import Sequence | ||
|
||
from . import MetricRecord, MetricsExporter, MetricsExportResult | ||
|
||
|
||
class InMemoryMetricsExporter(MetricsExporter): | ||
"""Implementation of `MetricsExporter` that stores metrics in memory. | ||
|
||
This class can be used for testing purposes. It stores exported metrics | ||
in a list in memory that can be retrieved using the | ||
:func:`.get_exported_metrics` method. | ||
""" | ||
|
||
def __init__(self): | ||
self._exported_metrics = [] | ||
self._stopped = False | ||
self._lock = threading.Lock() | ||
|
||
def clear(self): | ||
"""Clear list of collected metrics.""" | ||
with self._lock: | ||
self._exported_metrics.clear() | ||
|
||
def export( | ||
self, metric_records: Sequence[MetricRecord] | ||
) -> MetricsExportResult: | ||
if self._stopped: | ||
return MetricsExportResult.FAILURE | ||
with self._lock: | ||
self._exported_metrics.extend(metric_records) | ||
return MetricsExportResult.SUCCESS | ||
|
||
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. What do you think about implementing the shutdown method as well? 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 thought about it, but it wasn't clear to me what shutdown would be used for yet. 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 think it's just a matter of completeness, it could help to find bugs if an implementation is calling 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. done |
||
def get_exported_metrics(self): | ||
"""Get list of collected metrics.""" | ||
with self._lock: | ||
return tuple(self._exported_metrics) | ||
|
||
def shutdown(self) -> None: | ||
"""Shuts down the exporter. | ||
|
||
Called when the SDK is shut down. | ||
""" | ||
self._stopped = True |
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.
Seems like
self._stopped
isn't being used for anything? I believe in the span exporter it is used as a check upon callingexport
.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.
added the check to export, as is done in the
InMemorySpanExporter