Skip to content

Commit ab0f439

Browse files
authored
make sure Starlette's make_apm_client works without providing a config dict (#1161)
closes #1160
1 parent 61ed29a commit ab0f439

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

elasticapm/contrib/starlette/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
from __future__ import absolute_import
3333

34+
from typing import Dict, Optional
35+
3436
import starlette
3537
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
3638
from starlette.requests import Request
@@ -50,7 +52,7 @@
5052
logger = get_logger("elasticapm.errors.client")
5153

5254

53-
def make_apm_client(config: dict, client_cls=Client, **defaults) -> Client:
55+
def make_apm_client(config: Optional[Dict] = None, client_cls=Client, **defaults) -> Client:
5456
"""Builds ElasticAPM client.
5557
5658
Args:

tests/contrib/asyncio/starlette_tests.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import pytest # isort:skip
3232

33+
from tests.fixtures import TempStoreClient
34+
3335
starlette = pytest.importorskip("starlette") # isort:skip
3436

3537
import types
@@ -43,7 +45,7 @@
4345
import elasticapm
4446
from elasticapm import async_capture_span
4547
from elasticapm.conf import constants
46-
from elasticapm.contrib.starlette import ElasticAPM
48+
from elasticapm.contrib.starlette import ElasticAPM, make_apm_client
4749
from elasticapm.utils import wrapt
4850
from elasticapm.utils.disttracing import TraceParent
4951

@@ -367,3 +369,16 @@ def test_capture_body_error(app, elasticapm_client):
367369
client = TestClient(app)
368370
with pytest.raises(ValueError):
369371
response = client.post("/raise-exception", data="[0, 1]")
372+
373+
374+
def test_make_client_with_config():
375+
c = make_apm_client(config={"SERVICE_NAME": "foo"}, client_cls=TempStoreClient)
376+
c.close()
377+
assert c.config.service_name == "foo"
378+
379+
380+
def test_make_client_without_config():
381+
with mock.patch.dict("os.environ", {"ELASTIC_APM_SERVICE_NAME": "foo"}):
382+
c = make_apm_client(client_cls=TempStoreClient)
383+
c.close()
384+
assert c.config.service_name == "foo"

tests/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,9 @@ def get_config(self, current_version=None, keys=None):
331331

332332

333333
class TempStoreClient(Client):
334-
def __init__(self, **inline):
334+
def __init__(self, config=None, **inline):
335335
inline.setdefault("transport_class", "tests.fixtures.DummyTransport")
336-
super(TempStoreClient, self).__init__(**inline)
336+
super(TempStoreClient, self).__init__(config, **inline)
337337

338338
@property
339339
def events(self):

0 commit comments

Comments
 (0)