-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: checks | docs: included tests and assets
- Loading branch information
1 parent
7e5e724
commit 6524032
Showing
7 changed files
with
158 additions
and
62 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,39 @@ | ||
from datadog_checks.base import OpenMetricsBaseCheck, ConfigurationError | ||
|
||
from .metrics import METRIC_MAP | ||
|
||
|
||
class Resilience4jCheck(OpenMetricsBaseCheck): | ||
DEFAULT_METRIC_LIMIT = 0 | ||
|
||
def __init__(self, name, init_config, instances): | ||
default_instances = { | ||
'resilience4j': { | ||
'metrics': [METRIC_MAP], | ||
'send_distribution_sums_as_monotonic': 'true', | ||
'send_distribution_counts_as_monotonic': 'true', | ||
} | ||
} | ||
|
||
super(Resilience4jCheck, self).__init__( | ||
name, init_config, instances, default_instances=default_instances, default_namespace='resilience4j' | ||
) | ||
|
||
def _http_check(self, url, check_name): | ||
try: | ||
response = self.http.get(url) | ||
response.raise_for_status() | ||
except Exception as e: | ||
self.service_check(check_name, self.CRITICAL, message=str(e)) | ||
else: | ||
if response.status_code == 200: | ||
self.service_check(check_name, self.OK) | ||
else: | ||
self.service_check(check_name, self.WARNING) | ||
|
||
def check(self, instance): | ||
prometheus_url = instance.get("prometheus_url") | ||
if prometheus_url is None: | ||
raise ConfigurationError("Each instance must have a url to the metrics endpoint") | ||
|
||
super(Resilience4jCheck, self).check(instance) |
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,59 @@ | ||
import os | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from datadog_checks.resilience4j.check import Resilience4jCheck | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def dd_environment(): | ||
yield | ||
|
||
|
||
@pytest.fixture | ||
def instance(): | ||
return { | ||
"prometheus_url": "http://localhost:3000/metrics", | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def check(instance): | ||
return Resilience4jCheck("resilience4j", {}, [instance]) | ||
|
||
|
||
# @pytest.fixture() | ||
# def mock_micrometer_metrics(): | ||
# fixture_file = os.path.join(os.path.dirname(__file__), "fixtures", "metrics-micrometer.txt") | ||
|
||
# with open(fixture_file, "r") as f: | ||
# content = f.read() | ||
|
||
# with mock.patch( | ||
# "requests.get", | ||
# return_value=mock.MagicMock( | ||
# status_code=200, | ||
# iter_lines=lambda **kwargs: content.split("\n"), | ||
# headers={"Content-Type": "text/plain"}, | ||
# ), | ||
# ): | ||
# yield | ||
|
||
|
||
@pytest.fixture() | ||
def mock_prometheus_metrics(): | ||
fixture_file = os.path.join(os.path.dirname(__file__), "fixtures", "metrics-prometheus.txt") | ||
|
||
with open(fixture_file, "r") as f: | ||
content = f.read() | ||
|
||
with mock.patch( | ||
"requests.get", | ||
return_value=mock.MagicMock( | ||
status_code=200, | ||
iter_lines=lambda **kwargs: content.split("\n"), | ||
headers={"Content-Type": "text/plain"}, | ||
), | ||
): | ||
yield |
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,39 @@ | ||
version: "3" | ||
|
||
# Simple service demonstrating the use of resilience4j | ||
services: | ||
resilience4j-demo: | ||
container_name: resilience4j | ||
image: ghcr.io/willianccs/sample-demo-resilience4j-1.0@sha256:f2c814df19ff41eaf2e0f92ea0b5806cb1e6b9869db62d791ab2f51a3d2a0a84 | ||
ports: | ||
- "9080:9080" | ||
environment: | ||
- SPRING_PROFILES_ACTIVE=docker | ||
healthcheck: | ||
test: curl --fail http://resilience4j-demo:9080/actuator/health || exit 1 | ||
interval: 40s | ||
timeout: 30s | ||
retries: 3 | ||
start_period: 60s | ||
|
||
tester: | ||
image: alpine | ||
depends_on: | ||
- resilience4j-demo | ||
command: > | ||
sh -c " | ||
apk add --no-cache curl && | ||
echo 'Requests Success' && | ||
curl -s http://resilience4j-demo:9080/backendA/success && | ||
echo 'Requests Fail' && | ||
for i in 1 2 3 4; do | ||
curl -s http://resilience4j-demo:9080/backendA/failure; | ||
done && | ||
echo 'Validate Fallback' && | ||
curl -s http://resilience4j-demo:9080/backendA/fallback && | ||
echo 'Circuitbreaker closed' && | ||
for i in 1 2 3; do | ||
curl -s http://resilience4j-demo:9080/backendA/success; | ||
done | ||
" | ||
restart: always |
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