Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a warning about BaseHTTPMiddleware to Starlette docs #1735

Merged
merged 3 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ endif::[]
//===== Bug fixes
//

=== Unreleased

// Unreleased changes go here
// When the next release happens, nest these changes under the "Python Agent version 6.x" heading
//[float]
//===== Features

[float]
===== Bug fixes

* Small fix to underlying Starlette logic to prevent duplicate Client objects {pull}1735[#1735]



[[release-notes-6.x]]
=== Python Agent version 6.x
Expand Down
18 changes: 11 additions & 7 deletions docs/starlette.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ initialization arguments.
You can find a list of all available settings in the
<<configuration, Configuration>> page.

To initialize the agent for your application using environment variables:
To initialize the agent for your application using environment variables, add
the ElasticAPM middleware to your Starlette application:

[source,python]
----
from starlette.applications import Starlette
from elasticapm.contrib.starlette import make_apm_client, ElasticAPM
from elasticapm.contrib.starlette ElasticAPM

apm = make_apm_client()
app = Starlette()
app.add_middleware(ElasticAPM, client=apm)
app.add_middleware(ElasticAPM)
----

WARNING: If you are using any `BaseHTTPMiddleware` middleware, you must add them
*before* the ElasticAPM middleware. This is because `BaseHTTPMiddleware` breaks
`contextvar` propagation, as noted
https://www.starlette.io/middleware/#limitations[here].

To configure the agent using initialization arguments:

[source,python]
Expand All @@ -67,11 +72,10 @@ is almost exactly the same as with Starlette:
[source,python]
----
from fastapi import FastAPI
from elasticapm.contrib.starlette import make_apm_client, ElasticAPM
from elasticapm.contrib.starlette ElasticAPM

apm = make_apm_client()
app = FastAPI()
app.add_middleware(ElasticAPM, client=apm)
app.add_middleware(ElasticAPM)
----

[float]
Expand Down
4 changes: 3 additions & 1 deletion elasticapm/contrib/starlette/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import elasticapm
import elasticapm.instrumentation.control
from elasticapm.base import Client
from elasticapm.base import Client, get_client
from elasticapm.conf import constants
from elasticapm.contrib.asyncio.traces import set_context
from elasticapm.contrib.starlette.utils import get_body, get_data_from_request, get_data_from_response
Expand Down Expand Up @@ -115,6 +115,8 @@ def __init__(self, app: ASGIApp, client: Optional[Client], **kwargs):
if client:
self.client = client
else:
self.client = get_client()
if not self.client:
self.client = make_apm_client(**kwargs)

if self.client.config.instrument and self.client.config.enabled:
Expand Down