|
1 | 1 | # --- |
2 | 2 | import logging |
3 | | -import random |
4 | 3 | import sys |
5 | 4 | import time |
6 | 5 |
|
7 | 6 | from UnleashClient import UnleashClient |
8 | | -from UnleashClient.impact_metrics import MetricFlagContext |
9 | 7 |
|
10 | 8 | root = logging.getLogger() |
11 | 9 | root.setLevel(logging.DEBUG) |
|
17 | 15 | root.addHandler(handler) |
18 | 16 | # --- |
19 | 17 |
|
20 | | -my_client = UnleashClient( |
21 | | - url="https://sandbox.getunleash.io/enterprise/api/", |
22 | | - app_name="pyIvan", |
23 | | - environment="development", |
24 | | - custom_headers={ |
25 | | - "Authorization": "impact-metrics:development.6d70e55dd70dd79f5be3ce97835996000db94ed997c492421effe935" |
26 | | - }, |
27 | | -) |
| 18 | +my_client = UnleashClient(url="http://localhost:4242/api", app_name="pyIvan") |
28 | 19 |
|
29 | 20 | my_client.initialize_client() |
30 | 21 |
|
31 | | -# Define impact metrics |
32 | | -my_client.impact_metrics.define_counter("purchases", "Number of purchases made") |
33 | | -my_client.impact_metrics.define_gauge("active_users", "Current number of active users") |
34 | | -my_client.impact_metrics.define_histogram( |
35 | | - "request_latency", "Request latency in seconds", [0.01, 0.05, 0.1, 0.5, 1.0, 5.0] |
36 | | -) |
37 | | - |
38 | | -# Track active users count |
39 | | -active_users = 100 |
40 | | - |
41 | 22 | while True: |
42 | 23 | time.sleep(10) |
43 | | - print(f"Demo enabled: {my_client.is_enabled('Demo')}") |
44 | | - |
45 | | - # Create flag context for metrics (ties metrics to feature flag state) |
46 | | - flag_context = MetricFlagContext( |
47 | | - flag_names=["Demo"], |
48 | | - context={"userId": "integration-test-user"}, |
49 | | - ) |
50 | | - |
51 | | - # Increment counter (simulate a purchase) |
52 | | - my_client.impact_metrics.increment_counter("purchases", 1, flag_context) |
53 | | - print("Incremented purchases counter") |
54 | | - |
55 | | - # Update gauge (simulate active users changing) |
56 | | - active_users += random.randint(-10, 15) |
57 | | - active_users = max(0, active_users) # Don't go negative |
58 | | - my_client.impact_metrics.update_gauge("active_users", active_users, flag_context) |
59 | | - print(f"Updated active_users gauge to {active_users}") |
60 | | - |
61 | | - # Observe histogram (simulate request latency) |
62 | | - latency = random.uniform(0.01, 2.0) |
63 | | - my_client.impact_metrics.observe_histogram("request_latency", latency, flag_context) |
64 | | - print(f"Observed request_latency: {latency:.3f}s") |
| 24 | + print(my_client.is_enabled("Demo")) |
0 commit comments