Skip to content

Commit ed338c0

Browse files
committed
add enable_telemetry setting to the BaseFactory
1 parent 4392f2d commit ed338c0

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/titiler/application/titiler/application/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def validate_access_token(access_token: str = Security(api_key_query)):
118118
cogViewerExtension(),
119119
stacExtension(),
120120
],
121+
enable_telemtry=api_settings.telemetry_enabled,
121122
)
122123

123124
app.include_router(
@@ -138,6 +139,7 @@ def validate_access_token(access_token: str = Security(api_key_query)):
138139
stacViewerExtension(),
139140
stacRenderExtension(),
140141
],
142+
enable_telemtry=api_settings.telemetry_enabled,
141143
)
142144

143145
app.include_router(
@@ -151,7 +153,10 @@ def validate_access_token(access_token: str = Security(api_key_query)):
151153
###############################################################################
152154
# Mosaic endpoints
153155
if not api_settings.disable_mosaic:
154-
mosaic = MosaicTilerFactory(router_prefix="/mosaicjson")
156+
mosaic = MosaicTilerFactory(
157+
router_prefix="/mosaicjson",
158+
enable_telemtry=api_settings.telemetry_enabled,
159+
)
155160
app.include_router(
156161
mosaic.router,
157162
prefix="/mosaicjson",

src/titiler/core/titiler/core/factory.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ class BaseFactory(metaclass=abc.ABCMeta):
158158

159159
conforms_to: Set[str] = field(factory=set)
160160

161+
enable_telemtry: bool = field(default=False)
162+
161163
def __attrs_post_init__(self):
162164
"""Post Init: register route and configure specific options."""
163165
# prefix for endpoint's operationId
@@ -175,7 +177,8 @@ def __attrs_post_init__(self):
175177
for scopes, dependencies in self.route_dependencies:
176178
self.add_route_dependencies(scopes=scopes, dependencies=dependencies)
177179

178-
self.add_telemetry()
180+
if self.enable_telemtry:
181+
self.add_telemetry()
179182

180183
@abc.abstractmethod
181184
def register_routes(self):
@@ -241,6 +244,9 @@ def add_telemetry(self):
241244
of each APIRoute to ensure consistent OpenTelemetry tracing.
242245
"""
243246
if not factory_trace.decorator_enabled:
247+
logger.warning(
248+
"telemetry enabled for the factory class but tracing is not available"
249+
)
244250
return
245251

246252
for route in self.router.routes:

src/titiler/core/titiler/core/telemetry.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def add_span_attributes(attributes: Dict[str, Any]) -> None:
3535

3636

3737
def flatten_dict(d: dict, parent_key: str = "", sep: str = ".") -> dict:
38-
"""Flattens a nested dictionary."""
38+
"""Flattens a nested dictionary for adding span attributes."""
3939
items = {}
4040
for k, v in d.items():
4141
new_key = parent_key + sep + k if parent_key else k
@@ -101,20 +101,27 @@ def factory_trace(
101101
*,
102102
factory_instance: Optional[Any] = None,
103103
) -> Any:
104-
"""A decorator for Factory methods that automatically handles tracing."""
104+
"""A decorator for Factory methods that automatically handles tracing for factory methods"""
105105

106106
def decorator(func: Callable[P, Any]) -> Callable[P, Any]:
107107
if not tracer:
108108
return func
109109

110110
op_name = func.__name__
111111

112+
attributes = {}
113+
if factory_instance:
114+
if hasattr(factory_instance, "reader"):
115+
attributes["reader"] = str(factory_instance.reader)
116+
if hasattr(factory_instance, "backend"):
117+
attributes["backend"] = str(factory_instance.backend)
118+
112119
if inspect.iscoroutinefunction(func):
113120

114121
@functools.wraps(func)
115122
async def async_wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
116123
span_name = _get_span_name(op_name, factory_instance)
117-
with operation_tracer(span_name):
124+
with operation_tracer(span_name, attributes=attributes):
118125
return await func(*args, **kwargs)
119126

120127
return async_wrapper
@@ -123,7 +130,7 @@ async def async_wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
123130
@functools.wraps(func)
124131
def sync_wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
125132
span_name = _get_span_name(op_name, factory_instance)
126-
with operation_tracer(span_name):
133+
with operation_tracer(span_name, attributes=attributes):
127134
return func(*args, **kwargs)
128135

129136
return sync_wrapper

0 commit comments

Comments
 (0)