Skip to content

Commit be56a0f

Browse files
authored
chore: Add X-Goog-Api-Client metric header to requests (#826)
1 parent 32e8dd2 commit be56a0f

14 files changed

+255
-279
lines changed

Diff for: firebase_admin/_http_client.py

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import requests
2222
from requests.packages.urllib3.util import retry # pylint: disable=import-error
2323

24+
from firebase_admin import _utils
2425

2526
if hasattr(retry.Retry.DEFAULT, 'allowed_methods'):
2627
_ANY_METHOD = {'allowed_methods': None}
@@ -36,6 +37,9 @@
3637

3738
DEFAULT_TIMEOUT_SECONDS = 120
3839

40+
METRICS_HEADERS = {
41+
'X-GOOG-API-CLIENT': _utils.get_metrics_header(),
42+
}
3943

4044
class HttpClient:
4145
"""Base HTTP client used to make HTTP calls.
@@ -72,6 +76,7 @@ def __init__(
7276

7377
if headers:
7478
self._session.headers.update(headers)
79+
self._session.headers.update(METRICS_HEADERS)
7580
if retries:
7681
self._session.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries))
7782
self._session.mount('https://', requests.adapters.HTTPAdapter(max_retries=retries))

Diff for: firebase_admin/_utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Internal utilities common to all modules."""
1616

1717
import json
18+
from platform import python_version
1819

1920
import google.auth
2021
import requests
@@ -75,6 +76,8 @@
7576
16: exceptions.UNAUTHENTICATED,
7677
}
7778

79+
def get_metrics_header():
80+
return f'gl-python/{python_version()} fire-admin/{firebase_admin.__version__}'
7881

7982
def _get_initialized_app(app):
8083
"""Returns a reference to an initialized App instance."""

Diff for: firebase_admin/app_check.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class _AppCheckService:
5151
_scoped_project_id = None
5252
_jwks_client = None
5353

54+
_APP_CHECK_HEADERS = {
55+
'X-GOOG-API-CLIENT': _utils.get_metrics_header(),
56+
}
57+
5458
def __init__(self, app):
5559
# Validate and store the project_id to validate the JWT claims
5660
self._project_id = app.project_id
@@ -62,7 +66,8 @@ def __init__(self, app):
6266
'GOOGLE_CLOUD_PROJECT environment variable.')
6367
self._scoped_project_id = 'projects/' + app.project_id
6468
# Default lifespan is 300 seconds (5 minutes) so we change it to 21600 seconds (6 hours).
65-
self._jwks_client = PyJWKClient(self._JWKS_URL, lifespan=21600)
69+
self._jwks_client = PyJWKClient(
70+
self._JWKS_URL, lifespan=21600, headers=self._APP_CHECK_HEADERS)
6671

6772

6873
def verify_token(self, token: str) -> Dict[str, Any]:

Diff for: firebase_admin/storage.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ def bucket(name=None, app=None) -> storage.Bucket:
5555
class _StorageClient:
5656
"""Holds a Google Cloud Storage client instance."""
5757

58+
STORAGE_HEADERS = {
59+
'X-GOOG-API-CLIENT': _utils.get_metrics_header(),
60+
}
61+
5862
def __init__(self, credentials, project, default_bucket):
59-
self._client = storage.Client(credentials=credentials, project=project)
63+
self._client = storage.Client(
64+
credentials=credentials, project=project, extra_headers=self.STORAGE_HEADERS)
6065
self._default_bucket = default_bucket
6166

6267
@classmethod

0 commit comments

Comments
 (0)