Skip to content
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

Webhook exporter allowing Pelorus to work with push data model #836

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions exporters/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Needed by exporters/tests
pytest
pytest-cov
pytest-asyncio

# Used by bats in the conftests
yq
Expand All @@ -25,3 +26,6 @@ yamale
yamllint

pre-commit

# Required by TestClient
httpx
5 changes: 5 additions & 0 deletions exporters/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ jira # module jira
pytz # module pytz
requests # module requests
Pygithub # module github

# Webhook exporter
fastapi
uvicorn
asyncio
7 changes: 7 additions & 0 deletions exporters/tests/data/webhook_pelorus_committime.json
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
}
6 changes: 6 additions & 0 deletions exporters/tests/data/webhook_pelorus_deploytime.json
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
}
6 changes: 6 additions & 0 deletions exporters/tests/data/webhook_pelorus_failure_created.json
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
}
6 changes: 6 additions & 0 deletions exporters/tests/data/webhook_pelorus_failure_resolved.json
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
}
170 changes: 170 additions & 0 deletions exporters/tests/test_in_memory_metric.py
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):
mateusoliveira43 marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading