Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
Signed-off-by: sallyom <[email protected]>
  • Loading branch information
sallyom committed Jun 25, 2024
1 parent e6f4a74 commit 3f74c9e
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions ci/trace-steps.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import os
import time
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

# Initialize Tracer
trace.set_tracer_provider(TracerProvider())
service_name = os.getenv("WORKFLOW_NAME", "default")

resource = Resource.create({"service.name": service_name})
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)
span_processor = BatchSpanProcessor(ConsoleSpanExporter())
trace.get_tracer_provider().add_span_processor(span_processor)
console_span_processor = BatchSpanProcessor(ConsoleSpanExporter())
trace.get_tracer_provider().add_span_processor(console_span_processor)

otlp_exporter = OTLPSpanExporter(endpoint="localhost:4317", insecure=True)
otlp_span_processor = BatchSpanProcessor(otlp_exporter)
trace.get_tracer_provider().add_span_processor(otlp_span_processor)

print("Tracer initialized with service name:", service_name)

def trace_step(step_name: str):
"""
Decorated function that traces execution time.
Args:
step_name (str): Name of the step to trace.
"""
def decorator(func):
def wrapper(*args, **kwargs):
print(f"Tracing step: {step_name}")
with tracer.start_as_current_span(step_name) as span:
start_time = time.time()
result = func(*args, **kwargs)
Expand All @@ -30,13 +36,9 @@ def wrapper(*args, **kwargs):
return decorator

def trace_job(job_name: str):
"""
Decorated function that traces execution time.
Args:
job_name (str): Name of the job to trace.
"""
def decorator(func):
def wrapper(*args, **kwargs):
print(f"Tracing job: {job_name}")
with tracer.start_as_current_span(job_name) as span:
start_time = time.time()
result = func(*args, **kwargs)
Expand All @@ -48,23 +50,37 @@ def wrapper(*args, **kwargs):
return wrapper
return decorator

@trace_step("Example Step")
def example_step():
time.sleep(5) # Simulate work
return "Step Completed: Example Step"
# Decorators
@trace_step("build-image")
def build_image():
print("Building image...")
pass # Placeholder

@trace_step("download-model")
def download_model():
print("Downloading model...")
pass # Placeholder

@trace_step("run-functional-tests")
def run_tests():
print("Running tests...")
pass # Placeholder

@trace_job("Example Job")
def example_job():
step_result = example_step()
return f"Job Completed: Example Job with {step_result}"
@trace_job("CI Build Job")
def execute_job():
build_image()
download_model()
run_tests()
return "Job Completed: CI Build Job"

if __name__ == "__main__":
job_name = os.getenv("WORKFLOW_NAME", "Default Job")
step_name = os.getenv("STEP_NAME", "Default Step")
trace_action = os.getenv("TRACE_ACTION", "start")

if trace_action == "start":
example_job()
print(f"Starting trace for {job_name} - {step_name}")
execute_job()
elif trace_action == "end":
print(f"Ending trace for {job_name} - {step_name}")

0 comments on commit 3f74c9e

Please sign in to comment.