Skip to content

Commit 77fd1a3

Browse files
committed
basic integratation of open telemetry
1 parent e776522 commit 77fd1a3

File tree

13 files changed

+317
-0
lines changed

13 files changed

+317
-0
lines changed

env.example

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# OpenTelemetry Configuration
2+
# Copy this file to .env and update values as needed
3+
4+
# Enable/Disable OpenTelemetry SDK
5+
# Set to "true" to disable telemetry, "false" to enable (default: false)
6+
OTEL_SDK_DISABLED=false
7+
8+
# Service Information
9+
# Service name (default: "optimize-me")
10+
OTEL_SERVICE_NAME=optimize-me
11+
12+
# Service version (default: "0.1.0")
13+
OTEL_SERVICE_VERSION=0.1.0
14+
15+
# Exporter Configuration
16+
# Type of exporter: "console" (development), "otlp" (production), or "jaeger"
17+
# (default: "console")
18+
OTEL_EXPORTER_TYPE=console
19+
20+
# OTLP Exporter Endpoint
21+
# Endpoint for OTLP exporter (used when OTEL_EXPORTER_TYPE is "otlp" or "jaeger")
22+
# Format: http://host:port (default: "http://localhost:4318")
23+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
24+
25+
# Sampling Rate
26+
# Sampling rate for traces (0.0 to 1.0)
27+
# 1.0 = 100% sampling (all traces), 0.1 = 10% sampling (default: 1.0)
28+
OTEL_TRACES_SAMPLER_ARG=1.0
29+
30+
# Log Level
31+
# Logging level for OpenTelemetry (DEBUG, INFO, WARNING, ERROR)
32+
# (default: "INFO")
33+
OTEL_LOG_LEVEL=INFO

examples/run_with_telemetry.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
from pathlib import Path
3+
4+
project_root = Path(__file__).parent.parent
5+
sys.path.insert(0, str(project_root))
6+
7+
from src.telemetry import setup_telemetry
8+
from src.numerical.optimization import gradient_descent
9+
from src.algorithms.graph import graph_traversal, find_node_clusters
10+
from src.statistics.descriptive import describe
11+
import numpy as np
12+
import pandas as pd
13+
14+
setup_telemetry(
15+
service_name="optimize-me",
16+
service_version="0.1.0",
17+
exporter_type="console",
18+
)
19+
20+
print("Running gradient descent...")
21+
X = np.array([[1, 2], [3, 4], [5, 6]])
22+
y = np.array([1, 2, 3])
23+
weights = gradient_descent(X, y, learning_rate=0.01, iterations=100)
24+
print(f"Final weights: {weights}\n")
25+
26+
print("Running graph traversal...")
27+
graph = {1: {2, 3}, 2: {4}, 3: {4}, 4: {}}
28+
visited = graph_traversal(graph, 1)
29+
print(f"Visited nodes: {visited}\n")
30+
31+
print("Running statistical describe...")
32+
series = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
33+
stats = describe(series)
34+
print(f"Statistics: {stats}\n")
35+
36+
print("Running node clustering...")
37+
nodes = [{"id": 1}, {"id": 2}, {"id": 3}]
38+
edges = [{"source": 1, "target": 2}]
39+
clusters = find_node_clusters(nodes, edges)
40+
print(f"Clusters: {clusters}\n")
41+
42+
print("Telemetry demonstration complete!")
43+

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ dependencies = [
77
"codeflash>=0.17.3",
88
"networkx>=3.5",
99
"numpy>=2.3.3",
10+
"opentelemetry-api>=1.38.0",
11+
"opentelemetry-exporter-otlp-proto-grpc>=1.38.0",
12+
"opentelemetry-instrumentation>=0.59b0",
13+
"opentelemetry-sdk>=1.38.0",
1014
"pandas>=2.3.3",
1115
]
1216

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ numpy
22
pandas
33
codeflash
44
networkx
5+
opentelemetry-api>=1.38.0
6+
opentelemetry-exporter-otlp-proto-grpc>=1.38.0
7+
opentelemetry-instrumentation>=0.59b0
8+
opentelemetry-sdk>=1.38.0

src/algorithms/dynamic_programming.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
from __future__ import annotations
22

3+
from src.telemetry.decorators import trace_function
34

5+
6+
@trace_function
47
def fibonacci(n):
58
if n <= 1:
69
return n
710
return fibonacci(n - 1) + fibonacci(n - 2)
811

912

13+
@trace_function
1014
def matrix_sum(matrix: list[list[int]]) -> list[int]:
1115
return [sum(matrix[i]) for i in range(len(matrix)) if sum(matrix[i]) > 0]
1216

1317

18+
@trace_function
1419
def matrix_chain_order(matrices: list[tuple[int, int]]) -> int:
1520
"""
1621
Find the minimum number of operations needed to multiply a chain of matrices.
@@ -42,6 +47,7 @@ def dp(i: int, j: int) -> int:
4247
return dp(0, n - 1)
4348

4449

50+
@trace_function
4551
def coin_change(coins: list[int], amount: int, index: int) -> int:
4652
if amount == 0:
4753
return 1
@@ -53,6 +59,7 @@ def coin_change(coins: list[int], amount: int, index: int) -> int:
5359
)
5460

5561

62+
@trace_function
5663
def knapsack(weights: list[int], values: list[int], capacity: int, n: int) -> int:
5764
if n == 0 or capacity == 0:
5865
return 0

src/algorithms/graph.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import networkx as nx
44

5+
from src.telemetry.decorators import trace_function
56

7+
8+
@trace_function
69
def graph_traversal(graph: dict[int, dict[int]], node: int) -> dict[int]:
710
visited = []
811

@@ -21,6 +24,7 @@ class PathFinder:
2124
def __init__(self, graph: dict[str, list[str]]):
2225
self.graph = graph
2326

27+
@trace_function
2428
def find_shortest_path(self, start: str, end: str) -> list[str]:
2529
if start not in self.graph or end not in self.graph:
2630
return []
@@ -102,6 +106,7 @@ def find_node_with_highest_degree(
102106
return max_degree_node
103107

104108

109+
@trace_function
105110
def find_node_clusters(nodes: list[dict], edges: list[dict]) -> list[list[dict]]:
106111
"""Find connected components (clusters) in the graph."""
107112
# Create node ID to node mapping for easy lookup
@@ -149,6 +154,7 @@ def find_node_clusters(nodes: list[dict], edges: list[dict]) -> list[list[dict]]
149154
return clusters
150155

151156

157+
@trace_function
152158
def calculate_node_betweenness(
153159
nodes: list[str], edges: list[dict[str, str]]
154160
) -> dict[str, float]:

src/data_processing/dataframe.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import numpy as np
44
import pandas as pd
55

6+
from src.telemetry.decorators import trace_function
67

8+
9+
@trace_function
710
def dataframe_filter(df: pd.DataFrame, column: str, value: Any) -> pd.DataFrame:
811
indices = []
912
for i in range(len(df)):
@@ -12,6 +15,7 @@ def dataframe_filter(df: pd.DataFrame, column: str, value: Any) -> pd.DataFrame:
1215
return df.iloc[indices].reset_index(drop=True)
1316

1417

18+
@trace_function
1519
def groupby_mean(df: pd.DataFrame, group_col: str, value_col: str) -> dict[Any, float]:
1620
sums = {}
1721
counts = {}
@@ -30,6 +34,7 @@ def groupby_mean(df: pd.DataFrame, group_col: str, value_col: str) -> dict[Any,
3034
return result
3135

3236

37+
@trace_function
3338
def dataframe_merge(
3439
left: pd.DataFrame, right: pd.DataFrame, left_on: str, right_on: str
3540
) -> pd.DataFrame:

src/numerical/optimization.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import numpy as np
22

3+
from src.telemetry.decorators import trace_function
34

5+
6+
@trace_function(span_name="gradient_descent", capture_args=["iterations", "learning_rate"])
47
def gradient_descent(
58
X: np.ndarray, y: np.ndarray, learning_rate: float = 0.01, iterations: int = 1000
69
) -> np.ndarray:

src/statistics/descriptive.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import numpy as np
44
import pandas as pd
55

6+
from src.telemetry.decorators import trace_function
67

8+
9+
@trace_function
710
def describe(series: pd.Series) -> dict[str, float]:
811
values = [v for v in series if not pd.isna(v)]
912
n = len(values)
@@ -41,6 +44,7 @@ def percentile(p):
4144
}
4245

4346

47+
@trace_function
4448
def correlation(df: pd.DataFrame) -> dict[Tuple[str, str], float]:
4549
numeric_columns = [
4650
col for col in df.columns if np.issubdtype(df[col].dtype, np.number)

src/telemetry/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from src.telemetry.setup import setup_telemetry
2+
from src.telemetry.decorators import trace_function, trace_function_with_metrics
3+
4+
__all__ = [
5+
"setup_telemetry",
6+
"trace_function",
7+
"trace_function_with_metrics", # Placeholder for metrics implementation (currently behaves like trace_function)
8+
]
9+

0 commit comments

Comments
 (0)