Skip to content

Commit ee1a2ac

Browse files
improve(apm): enhance Elastic APM client initialization to prevent duplicates
- Use elasticapm.get_client() before creating new clients in tracing decorators - Apply same pattern to gRPC server interceptors (sync and async) - Prevent potential memory leaks and improve performance by reusing existing clients - Maintain backward compatibility while optimizing resource usage
1 parent 0fbb239 commit ee1a2ac

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

archipy/helpers/decorators/tracing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
7878
logging.exception("Failed to initialize Sentry or start transaction")
7979

8080
# Initialize and track with Elastic APM if enabled
81-
elastic_client = None
8281
if config.ELASTIC_APM.IS_ENABLED:
8382
try:
8483
import elasticapm
8584

8685
# Initialize Elastic APM client with config
87-
elastic_client = elasticapm.Client(config.ELASTIC_APM.model_dump())
86+
elastic_client = elasticapm.get_client()
87+
if not elastic_client:
88+
elastic_client = elasticapm.Client(config.ELASTIC_APM.model_dump())
8889
elastic_client.begin_transaction(transaction_type="function")
8990
except ImportError:
9091
logging.debug("elasticapm is not installed, skipping Elastic APM transaction capture.")

archipy/helpers/interceptors/grpc/trace/server_interceptor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ def intercept(
8585
logging.exception("Failed to create Sentry transaction for gRPC server call")
8686

8787
# Handle Elastic APM if enabled
88-
elastic_client = None
8988
if config.ELASTIC_APM.IS_ENABLED:
9089
try:
9190
# Get the Elastic APM client
92-
elastic_client = elasticapm.Client(config.ELASTIC_APM.model_dump())
93-
91+
elastic_client = elasticapm.get_client()
92+
if not elastic_client:
93+
elastic_client = elasticapm.Client(config.ELASTIC_APM.model_dump())
9494
# Check if a trace parent header is present in the metadata
9595
if parent := elasticapm.trace_parent_from_headers(metadata_dict):
9696
# Start a transaction linked to the distributed trace
@@ -203,11 +203,12 @@ async def intercept(
203203
logging.exception("Failed to create Sentry transaction for async gRPC server call")
204204

205205
# Handle Elastic APM if enabled
206-
elastic_client = None
207206
if config.ELASTIC_APM.IS_ENABLED:
208207
try:
209208
# Get the Elastic APM client
210-
elastic_client = elasticapm.Client(config.ELASTIC_APM.model_dump())
209+
elastic_client = elasticapm.get_client()
210+
if not elastic_client:
211+
elastic_client = elasticapm.Client(config.ELASTIC_APM.model_dump())
211212

212213
# Check if a trace parent header is present in the metadata
213214
if parent := elasticapm.trace_parent_from_headers(metadata_dict):

docs/changelog.md

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

33
All notable changes to ArchiPy are documented in this changelog, organized by version.
44

5+
## [3.13.9] - 2025-10-15
6+
7+
### Improved
8+
9+
#### Elastic APM Client Initialization
10+
11+
- **Enhanced Client Reuse** - Improved Elastic APM client initialization to prevent duplicate client creation
12+
- Updated tracing decorators to use `elasticapm.get_client()` before creating new clients
13+
- Applied same pattern to gRPC server interceptors (both sync and async)
14+
- Prevents potential memory leaks and improves performance by reusing existing clients
15+
- Maintains backward compatibility while optimizing resource usage
16+
517
## [3.13.8] - 2025-10-15
618

719
### Changed

0 commit comments

Comments
 (0)