Skip to content

Commit 83dca14

Browse files
committed
add high level latency function
Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent 106421c commit 83dca14

File tree

7 files changed

+478
-13
lines changed

7 files changed

+478
-13
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
3232
- name: Install dependencies
3333
run: |
34-
pdm sync -d
34+
pdm sync -d -G lueur
3535
3636
- name: Run Lint
3737
run: |

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
## [Unreleased][]
44

5-
[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-google-cloud-platform/compare/0.33.0...HEAD
5+
[Unreleased]: https://github.com/chaostoolkit-incubator/chaostoolkit-google-cloud-platform/compare/0.34.0...HEAD
6+
7+
## [0.34.0][] - 2024-07-01
8+
9+
[0.34.0]: https://github.com/chaostoolkit-incubator/chaostoolkit-google-cloud-platform/compare/0.33.0...0.34.0
10+
11+
### Added
12+
13+
* Bringing `lueur` as an optional dependency to start creating higher level
14+
actions/probes where data is automatically discovered from a live environment.
15+
The goal is to reduce boiler plate wherever we can.
616

717
## [0.33.0][] - 2024-06-20
818

chaosgcp/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"to_dict",
4040
"context_from_parent_path",
4141
"parse_interval",
42+
"is_lueur_installed",
4243
]
4344

4445
logger = logging.getLogger("chaostoolkit")
@@ -315,6 +316,24 @@ def parse_interval(
315316
return (start_time, end_time)
316317

317318

319+
def is_lueur_installed() -> bool:
320+
"""
321+
Check that the lueur dependency is installed.
322+
"""
323+
try:
324+
import lueur # noqa F401
325+
326+
return True
327+
except ImportError:
328+
pass
329+
330+
logger.debug(
331+
"Lueur package not installed. Please refer to the documentation"
332+
)
333+
334+
return False
335+
336+
318337
###############################################################################
319338
# Private functions
320339
###############################################################################

chaosgcp/lb/actions.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from chaoslib.types import Configuration, Secrets
77
from google.cloud import compute_v1
88

9-
from chaosgcp import get_context, load_credentials, wait_on_extended_operation
9+
from chaosgcp import (
10+
get_context,
11+
load_credentials,
12+
wait_on_extended_operation,
13+
is_lueur_installed,
14+
)
1015
from chaosgcp.lb import (
1116
get_fault_injection_policy,
1217
remove_fault_injection_policy,
@@ -316,3 +321,63 @@ def remove_fault_injection_traffic_policy(
316321
wait_on_extended_operation(operation=operation)
317322

318323
return urlmap.__class__.to_dict(urlmap)
324+
325+
326+
if is_lueur_installed():
327+
import asyncio
328+
329+
from lueur.api.gcp.lb import lb_config_from_url
330+
from lueur.platform.gcp.lb import explore_lb
331+
332+
__all__.append("add_latency_to_endpoint")
333+
334+
def add_latency_to_endpoint(
335+
url: str,
336+
latency: float = 0.3,
337+
percentage: float = 90.0,
338+
project_id: str = None,
339+
region: str = None,
340+
configuration: Configuration = None,
341+
secrets: Secrets = None,
342+
) -> Dict[str, Any]:
343+
"""
344+
Add latency to a particular URL.
345+
346+
This is a high level shortcut to the `inject_traffic_delay` which
347+
infers all the appropriate parameters from the URL itself. It does this
348+
by querying the GCP project for all LB information and matches the
349+
correct target from there.
350+
351+
This might no work on all combinaison of Load Balancer and backend
352+
services that GCP support but should work well with LB + Cloud Run.
353+
354+
The `latency` is expressed in seconds with a default set to 0.3 seconds.
355+
"""
356+
credentials = load_credentials(secrets)
357+
context = get_context(
358+
configuration, project_id=project_id, region=region
359+
)
360+
361+
region = context.region
362+
project = context.project_id
363+
364+
snapshot = asyncio.run(
365+
explore_lb(project, region, credentials=credentials)
366+
)
367+
368+
config = lb_config_from_url(snapshot=snapshot, url=url)
369+
370+
return inject_traffic_delay(
371+
url_map=config.urlmap,
372+
target_name=config.path,
373+
target_path=config.path_matcher,
374+
delay_in_seconds=0,
375+
delay_in_nanos=int(latency * 1e9),
376+
impacted_percentage=percentage,
377+
latency=config.project,
378+
regional=config.is_regional,
379+
region=region,
380+
project_id=project,
381+
configuration=configuration,
382+
secrets=secrets,
383+
)

0 commit comments

Comments
 (0)