Coverage for /Users/antonigmitruk/golf/src/golf/core/builder_telemetry.py: 0%
17 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-08-16 18:46 +0200
« prev ^ index » next coverage.py v7.6.12, created at 2025-08-16 18:46 +0200
1"""OpenTelemetry integration for the GolfMCP build process.
3This module provides functions for generating OpenTelemetry initialization
4and instrumentation code for FastMCP servers built with GolfMCP.
5"""
8def generate_telemetry_imports() -> list[str]:
9 """Generate import statements for telemetry instrumentation.
11 Returns:
12 List of import statements for telemetry
13 """
14 return [
15 "# OpenTelemetry instrumentation imports",
16 "from golf.telemetry import (",
17 " instrument_tool,",
18 " instrument_resource,",
19 " instrument_prompt,",
20 " telemetry_lifespan,",
21 ")",
22 ]
25def generate_component_registration_with_telemetry(
26 component_type: str,
27 component_name: str,
28 module_path: str,
29 entry_function: str,
30 docstring: str = "",
31 uri_template: str = None,
32) -> str:
33 """Generate component registration code with telemetry instrumentation.
35 Args:
36 component_type: Type of component ('tool', 'resource', 'prompt')
37 component_name: Name of the component
38 module_path: Full module path to the component
39 entry_function: Entry function name
40 docstring: Component description
41 uri_template: URI template for resources (optional)
43 Returns:
44 Python code string for registering the component with instrumentation
45 """
46 func_ref = f"{module_path}.{entry_function}"
47 escaped_docstring = docstring.replace('"', '\\"') if docstring else ""
49 if component_type == "tool":
50 wrapped_func = f"instrument_tool({func_ref}, '{component_name}')"
51 return (
52 f"_tool = Tool.from_function({wrapped_func}, "
53 f'name="{component_name}", description="{escaped_docstring}")\n'
54 f"mcp.add_tool(_tool)"
55 )
57 elif component_type == "resource":
58 wrapped_func = f"instrument_resource({func_ref}, '{uri_template}')"
59 return (
60 f"_resource = Resource.from_function({wrapped_func}, "
61 f'uri="{uri_template}", name="{component_name}", '
62 f'description="{escaped_docstring}")\n'
63 f"mcp.add_resource(_resource)"
64 )
66 elif component_type == "prompt":
67 wrapped_func = f"instrument_prompt({func_ref}, '{component_name}')"
68 return (
69 f"_prompt = Prompt.from_function({wrapped_func}, "
70 f'name="{component_name}", description="{escaped_docstring}")\n'
71 f"mcp.add_prompt(_prompt)"
72 )
74 else:
75 raise ValueError(f"Unknown component type: {component_type}")
78def get_otel_dependencies() -> list[str]:
79 """Get list of OpenTelemetry dependencies to add to pyproject.toml.
81 Returns:
82 List of package requirements strings
83 """
84 return [
85 "opentelemetry-api>=1.18.0",
86 "opentelemetry-sdk>=1.18.0",
87 "opentelemetry-instrumentation-asgi>=0.40b0",
88 "opentelemetry-exporter-otlp-proto-http>=0.40b0",
89 ]