Skip to content

Commit

Permalink
Add Jaeger client for tracing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
averevki committed Feb 13, 2024
1 parent fbe3d4e commit 55d9b74
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/settings.local.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
# url: "AUTH0_URL"
# mockserver:
# url: "MOCKSERVER_URL"
# jaeger:
# collector_url: "rpc://jaeger-collector.com:4317" # Jaeger collector URL (may be internal)
# query_url: "http://jaeger-query.com" # Jaeger query URL
# cfssl: "cfssl" # Path to the CFSSL library for TLS tests
# hyperfoil:
# url: "HYPERFOIL_URL"
Expand Down
2 changes: 2 additions & 0 deletions testsuite/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, name, default, **kwargs) -> None:
DefaultValueValidator("rhsso.url", default=fetch_route("no-ssl-sso")),
DefaultValueValidator("rhsso.password", default=fetch_secret("credential-sso", "ADMIN_PASSWORD")),
DefaultValueValidator("mockserver.url", default=fetch_route("mockserver", force_http=True)),
DefaultValueValidator("jaeger.collector_url", default="rpc://jaeger-collector.tools.svc.cluster.local:4317"),
DefaultValueValidator("jaeger.query_url", default=fetch_route("jaeger-query", force_http=True)),
],
validate_only=["authorino", "kuadrant"],
loaders=["dynaconf.loaders.env_loader", "testsuite.config.openshift_loader"],
Expand Down
42 changes: 42 additions & 0 deletions testsuite/jaeger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Module with Jaeger client for traces management"""

from typing import Optional, Iterator

import httpx
import backoff
from apyproxy import ApyProxy


class Jaeger:
"""Jaeger client for traces management"""

def __init__(self, collector_url: str, query_url: str):
self.collector_url = collector_url
self.query = ApyProxy(query_url, session=httpx.Client(verify=False))

def _get_traces(self, operation: str) -> Iterator[dict]:
"""Get traces from jaeger by operation name"""
params = {"service": "authorino", "operation": operation}
response = self.query.api.traces.get(params=params)
return reversed(response.json()["data"])

@backoff.on_predicate(backoff.fibo, lambda x: x is None, max_tries=5, jitter=None)
def find_trace(self, operation: str, request_id: str) -> Optional[dict]:
"""Find trace in jaeger by operation and authorino request id"""
for trace in self._get_traces(operation): # pylint: disable=too-many-nested-blocks
for span in trace["spans"]:
if span["operationName"] == operation:
for tag in span["tags"]:
if tag["key"] == "authorino.request_id":
if tag["value"] == request_id:
return trace
return None

def find_tagged_trace(self, operation: str, request_id: str, tag_key: str, tag_value: str) -> Optional[dict]:
"""Find trace in jaeger by operation, authorino request id and tag key-value pair"""
if trace := self.find_trace(operation, request_id):
for process in trace["processes"]:
for proc_tag in trace["processes"][process]["tags"]:
if proc_tag["key"] == tag_key and proc_tag["value"] == tag_value:
return trace
return None
11 changes: 11 additions & 0 deletions testsuite/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from testsuite.certificates import CFSSLClient
from testsuite.config import settings
from testsuite.mockserver import Mockserver
from testsuite.jaeger import Jaeger
from testsuite.gateway import Gateway, GatewayRoute, Hostname, Exposer
from testsuite.oidc import OIDCProvider
from testsuite.oidc.auth0 import Auth0Provider
Expand Down Expand Up @@ -203,6 +204,16 @@ def mockserver(testconfig, skip_or_fail):
return skip_or_fail(f"Mockserver configuration item is missing: {exc}")


@pytest.fixture(scope="module")
def jaeger(testconfig):
"""Returns Jaeger client for tracing tests"""
try:
testconfig.validators.validate(only=["jaeger"])
return Jaeger(testconfig["jaeger"]["collector_url"], testconfig["jaeger"]["query_url"])
except (KeyError, ValidationError) as exc:
return pytest.skip(f"Jaeger configuration item is missing: {exc}")


@pytest.fixture(scope="session")
def oidc_provider(rhsso) -> OIDCProvider:
"""Fixture which enables switching out OIDC providers for individual modules"""
Expand Down

0 comments on commit 55d9b74

Please sign in to comment.