-
Notifications
You must be signed in to change notification settings - Fork 85
Webhook exporter allowing Pelorus to work with push data model #836
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
Merged
mateusoliveira43
merged 2 commits into
dora-metrics:master
from
mpryc:webhook_playground
Mar 3, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
{ | ||
"app": "mongo-todolist", | ||
"commit_hash": "5379bad65a3f83853a75aabec9e0e43c75fd18fc", | ||
"image_sha": "sha256:af4092ccbfa99a3ec1ea93058fe39b8ddfd8db1c7a18081db397c50a0b8ec77d", | ||
"namespace": "mongo-persistent", | ||
"timestamp": 1557933657 | ||
} |
This file contains hidden or 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,6 @@ | ||
{ | ||
"app": "mongo-todolist", | ||
"image_sha": "sha256:af4092ccbfa99a3ec1ea93058fe39b8ddfd8db1c7a18081db397c50a0b8ec77d", | ||
"namespace": "mongo-persistent", | ||
"timestamp": 1557933657 | ||
} |
This file contains hidden or 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,6 @@ | ||
{ | ||
"app": "mongo-todolist", | ||
"failure_id": "MONGO-1", | ||
"failure_event": "created", | ||
"timestamp": 1557933657 | ||
} |
This file contains hidden or 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,6 @@ | ||
{ | ||
"app": "mongo-todolist", | ||
"failure_id": "MONGO-1", | ||
"failure_event": "resolved", | ||
"timestamp": 1557933657 | ||
} |
This file contains hidden or 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,170 @@ | ||
# | ||
# Copyright Red Hat | ||
# | ||
# 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. | ||
# | ||
|
||
|
||
from unittest import mock | ||
|
||
import pytest | ||
from prometheus_client import REGISTRY | ||
from prometheus_client.registry import Collector | ||
|
||
from webhook.models.pelorus_webhook import CommitTimePelorusPayload, PelorusPayload | ||
from webhook.store.in_memory_metric import ( | ||
PelorusGaugeMetricFamily, | ||
_pelorus_metric_to_dict, | ||
pelorus_metric_to_prometheus, | ||
) | ||
|
||
metric_labels = list(_pelorus_metric_to_dict(CommitTimePelorusPayload).values()) | ||
|
||
in_memory_test_committime_metrics = PelorusGaugeMetricFamily( | ||
"test_committime_metrics", | ||
"Test timestamp", | ||
labels=metric_labels, | ||
) | ||
|
||
|
||
class CustomCommitCollector(Collector): | ||
def __init_subclass__(cls) -> None: | ||
super().__init_subclass__() | ||
|
||
# make sure __hash__ is something prometheus' registry can handle properly. | ||
cls.__hash__ = lambda self: id(self) # type: ignore | ||
|
||
def collect(self) -> PelorusGaugeMetricFamily: | ||
yield in_memory_test_committime_metrics | ||
|
||
|
||
class TestInMemoryMetric: | ||
def setup_method(self): | ||
self.custom_collector = CustomCommitCollector() | ||
REGISTRY.register(self.custom_collector) | ||
|
||
def teardown_method(self): | ||
REGISTRY.unregister(self.custom_collector) | ||
|
||
@pytest.mark.parametrize( | ||
"name,timestamp,image_hash,namespace,commit_hash", | ||
[ | ||
( | ||
"todolist", | ||
"timestamp_str", | ||
"sha256:af4092ccbfa99a3ec1ea93058fe39b8ddfd8db1c7a18081db397c50a0b8ec77d", | ||
"mynamespace", | ||
"5379bad65a3f83853a75aabec9e0e43c75fd18fc", | ||
), | ||
], | ||
) | ||
def test_pelorus_gauge_metric_family( | ||
self, name, timestamp, image_hash, namespace, commit_hash | ||
): | ||
""" | ||
Verifies if the metric passed to the pelorus_metric_to_prometheus method | ||
and then registered in our CustomCommitCollector is properly collected | ||
by Prometheus. It does it by getting sample value and comparing the | ||
timestamp of that metric to the timestamp of the data received from | ||
Prometheus. | ||
""" | ||
commit_payload = CommitTimePelorusPayload( | ||
app=name, | ||
timestamp=timestamp, | ||
image_sha=image_hash, | ||
namespace=namespace, | ||
commit_hash=commit_hash, | ||
) | ||
|
||
prometheus_commit_metric = pelorus_metric_to_prometheus(commit_payload) | ||
in_memory_test_committime_metrics.add_metric( | ||
commit_payload.commit_hash, | ||
prometheus_commit_metric, | ||
commit_payload.timestamp, | ||
) | ||
|
||
metric_labels = { | ||
"app": name, | ||
"image_sha": image_hash, | ||
"commit_hash": commit_hash, | ||
"namespace": namespace, | ||
} | ||
|
||
query_result = REGISTRY.get_sample_value( | ||
"test_committime_metrics", | ||
labels=metric_labels, | ||
) | ||
|
||
assert query_result == timestamp | ||
|
||
|
||
def test_all_models_have_prometheus_mappings(): | ||
""" | ||
Safeguard test to ensure new PelorusPayload models also have | ||
corresponding Prometheus model. | ||
|
||
It does this by verifying if all models which are inheriting | ||
from PelorusPayload class returns non empty dictionary from the | ||
_pelorus_metric_to_dict method, where the mappings are defined. | ||
|
||
We need to test this scenario to ensure we know how to store | ||
each of the Pelorus data models into Prometheus. | ||
""" | ||
import webhook.models.pelorus_webhook as pelorus_webhook | ||
mateusoliveira43 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
test_models = [] | ||
|
||
for cls in pelorus_webhook.__dict__.values(): | ||
if isinstance(cls, type) and issubclass(cls, PelorusPayload): | ||
test_models.append(cls) | ||
|
||
for test_model in test_models: | ||
metric = _pelorus_metric_to_dict(test_model) | ||
assert bool(metric) # dict should not be empty | ||
|
||
|
||
class NewPelorusPayloadModel(PelorusPayload): | ||
pass | ||
|
||
|
||
def test_model_do_not_have_prometheus_mapping(): | ||
""" | ||
Negative safeguard test to ensure that proper Error is raised | ||
when the new models which are inheriting from PelorusPayload | ||
model do not have corresponding Prometheus model defined in | ||
the _pelorus_metric_to_dict method. | ||
""" | ||
|
||
with pytest.raises(TypeError) as type_error: | ||
_pelorus_metric_to_dict(NewPelorusPayloadModel) | ||
assert "Improper prometheus data model" in str(type_error.value) | ||
|
||
|
||
@mock.patch( | ||
"webhook.store.in_memory_metric._pelorus_metric_to_dict", | ||
return_value={"app": "nonexisting"}, | ||
) | ||
def test_model_missing_value_in_model(*args): | ||
""" | ||
Negative safeguard test to ensure that the model which is | ||
inheriting from the PelorusPayload model does match | ||
expected Prometheus model defined in the _pelorus_metric_to_dict | ||
method. | ||
|
||
It does this by verifying if all the expected variables are defined | ||
in the PelorusPayload model, otherwise raising TypeError. | ||
""" | ||
|
||
with pytest.raises(TypeError) as type_error: | ||
pelorus_metric_to_prometheus(NewPelorusPayloadModel) | ||
assert "Attribute nonexisting was not found in" in str(type_error.value) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.