Skip to content

Commit 55c39d1

Browse files
committed
Add example scripts for running traces and testing OpenTelemetry integration for all functions in the project
1 parent 77fd1a3 commit 55c39d1

File tree

3 files changed

+485
-0
lines changed

3 files changed

+485
-0
lines changed

examples/run_all_traces.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
"""
2+
Run all instrumented functions and display their trace outputs.
3+
4+
This script demonstrates all functions decorated with @trace_function
5+
and shows their OpenTelemetry trace spans in the console.
6+
"""
7+
import sys
8+
from pathlib import Path
9+
10+
# Add project root to path
11+
project_root = Path(__file__).parent.parent
12+
sys.path.insert(0, str(project_root))
13+
14+
import numpy as np
15+
import pandas as pd
16+
17+
from src.telemetry import setup_telemetry
18+
from src.numerical.optimization import gradient_descent
19+
from src.algorithms.graph import graph_traversal, find_node_clusters, PathFinder, calculate_node_betweenness
20+
from src.algorithms.dynamic_programming import fibonacci, matrix_sum, matrix_chain_order, coin_change, knapsack
21+
from src.data_processing.dataframe import dataframe_filter, groupby_mean, dataframe_merge
22+
from src.statistics.descriptive import describe, correlation
23+
24+
# Initialize OpenTelemetry with console exporter
25+
print("=" * 80)
26+
print("Initializing OpenTelemetry...")
27+
print("=" * 80)
28+
setup_telemetry(
29+
service_name="optimize-me",
30+
service_version="0.1.0",
31+
exporter_type="console",
32+
)
33+
34+
print("\n" + "=" * 80)
35+
print("RUNNING ALL INSTRUMENTED FUNCTIONS")
36+
print("=" * 80)
37+
print("\nTraces will appear as JSON objects below each function call.\n")
38+
39+
# ============================================================================
40+
# Numerical Optimization
41+
# ============================================================================
42+
print("\n--- Numerical Optimization ---")
43+
print("Running gradient_descent...")
44+
X = np.array([[1, 2], [3, 4], [5, 6]])
45+
y = np.array([1, 2, 3])
46+
weights = gradient_descent(X, y, learning_rate=0.01, iterations=100)
47+
print(f"Result: {weights}\n")
48+
49+
# ============================================================================
50+
# Graph Algorithms
51+
# ============================================================================
52+
print("\n--- Graph Algorithms ---")
53+
54+
print("Running graph_traversal...")
55+
graph = {1: {2, 3}, 2: {4}, 3: {4}, 4: {}}
56+
visited = graph_traversal(graph, 1)
57+
print(f"Result: {visited}\n")
58+
59+
print("Running find_node_clusters...")
60+
nodes = [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}]
61+
edges = [{"source": 1, "target": 2}, {"source": 3, "target": 4}]
62+
clusters = find_node_clusters(nodes, edges)
63+
print(f"Result: {clusters}\n")
64+
65+
print("Running PathFinder.find_shortest_path...")
66+
path_finder = PathFinder({"A": ["B", "C"], "B": ["D"], "C": ["D"], "D": []})
67+
path = path_finder.find_shortest_path("A", "D")
68+
print(f"Result: {path}\n")
69+
70+
print("Running calculate_node_betweenness...")
71+
nodes_list = ["A", "B", "C", "D"]
72+
edges_list = [{"source": "A", "target": "B"}, {"source": "B", "target": "C"}, {"source": "C", "target": "D"}]
73+
betweenness = calculate_node_betweenness(nodes_list, edges_list)
74+
print(f"Result: {betweenness}\n")
75+
76+
# ============================================================================
77+
# Dynamic Programming
78+
# ============================================================================
79+
print("\n--- Dynamic Programming ---")
80+
81+
print("Running fibonacci...")
82+
fib_result = fibonacci(10)
83+
print(f"Result: {fib_result}\n")
84+
85+
print("Running matrix_sum...")
86+
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
87+
matrix_result = matrix_sum(matrix)
88+
print(f"Result: {matrix_result}\n")
89+
90+
print("Running matrix_chain_order...")
91+
matrices = [(10, 20), (20, 30), (30, 40)]
92+
chain_result = matrix_chain_order(matrices)
93+
print(f"Result: {chain_result}\n")
94+
95+
print("Running coin_change...")
96+
coins = [1, 2, 5]
97+
amount = 5
98+
coin_result = coin_change(coins, amount, 0)
99+
print(f"Result: {coin_result}\n")
100+
101+
print("Running knapsack...")
102+
weights = [10, 20, 30]
103+
values = [60, 100, 120]
104+
capacity = 50
105+
knapsack_result = knapsack(weights, values, capacity, len(weights))
106+
print(f"Result: {knapsack_result}\n")
107+
108+
# ============================================================================
109+
# Data Processing
110+
# ============================================================================
111+
print("\n--- Data Processing ---")
112+
113+
print("Running dataframe_filter...")
114+
df = pd.DataFrame({"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]})
115+
filtered = dataframe_filter(df, "A", 3)
116+
print(f"Result:\n{filtered}\n")
117+
118+
print("Running groupby_mean...")
119+
df_group = pd.DataFrame({
120+
"group": ["A", "A", "B", "B", "C"],
121+
"value": [10, 20, 30, 40, 50]
122+
})
123+
grouped = groupby_mean(df_group, "group", "value")
124+
print(f"Result: {grouped}\n")
125+
126+
print("Running dataframe_merge...")
127+
df_left = pd.DataFrame({"id": [1, 2, 3], "name": ["Alice", "Bob", "Charlie"]})
128+
df_right = pd.DataFrame({"id": [2, 3, 4], "age": [25, 30, 35]})
129+
merged = dataframe_merge(df_left, df_right, "id", "id")
130+
print(f"Result:\n{merged}\n")
131+
132+
# ============================================================================
133+
# Statistics
134+
# ============================================================================
135+
print("\n--- Statistics ---")
136+
137+
print("Running describe...")
138+
series = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
139+
stats = describe(series)
140+
print(f"Result: {stats}\n")
141+
142+
print("Running correlation...")
143+
df_corr = pd.DataFrame({
144+
"x": [1, 2, 3, 4, 5],
145+
"y": [2, 4, 6, 8, 10],
146+
"z": [1, 3, 5, 7, 9]
147+
})
148+
corr_result = correlation(df_corr)
149+
print(f"Result: {corr_result}\n")
150+
151+
print("=" * 80)
152+
print("ALL FUNCTIONS EXECUTED - Check the JSON trace spans above!")
153+
print("=" * 80)
154+
print("\nEach function call above generated a trace span with:")
155+
print(" - Function name")
156+
print(" - Execution time (start_time, end_time)")
157+
print(" - Captured arguments (for gradient_descent: iterations, learning_rate)")
158+
print(" - Status (OK or ERROR)")
159+
print(" - Service information (service.name, service.version)")
160+
print("\n")
161+

examples/run_custom_traces.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
Custom script to run specific functions and see their trace outputs.
3+
4+
Modify this script to call any functions you want to trace.
5+
"""
6+
import sys
7+
from pathlib import Path
8+
9+
# Add project root to path
10+
project_root = Path(__file__).parent.parent
11+
sys.path.insert(0, str(project_root))
12+
13+
import numpy as np
14+
import pandas as pd
15+
16+
from src.telemetry import setup_telemetry
17+
18+
# Import the functions you want to test
19+
from src.numerical.optimization import gradient_descent
20+
from src.algorithms.graph import graph_traversal
21+
from src.algorithms.dynamic_programming import fibonacci
22+
# Add more imports as needed...
23+
24+
# ============================================================================
25+
# Initialize OpenTelemetry
26+
# ============================================================================
27+
setup_telemetry(
28+
service_name="optimize-me",
29+
service_version="0.1.0",
30+
exporter_type="console", # Change to "otlp" for production
31+
)
32+
33+
# ============================================================================
34+
# Call your functions here - each will generate a trace span
35+
# ============================================================================
36+
37+
print("Running gradient_descent...")
38+
X = np.array([[1, 2], [3, 4]])
39+
y = np.array([1, 2])
40+
result = gradient_descent(X, y, learning_rate=0.01, iterations=50)
41+
print(f"Result: {result}\n")
42+
43+
print("Running fibonacci...")
44+
fib_result = fibonacci(8)
45+
print(f"Result: {fib_result}\n")
46+
47+
print("Running graph_traversal...")
48+
graph = {1: {2, 3}, 2: {4}, 3: {4}, 4: {}}
49+
visited = graph_traversal(graph, 1)
50+
print(f"Result: {visited}\n")
51+
52+
# Add more function calls here to see their traces...
53+
54+
print("\nDone! Check the JSON trace spans above each function result.")
55+

0 commit comments

Comments
 (0)