11import concurrent .futures
22import json
3+ import platform
34import time
45import types
56import urllib .error
67import urllib .request
78from typing import ClassVar , Optional , Type
89
10+ import apilytics
11+
912__all__ = ["ApilyticsSender" ]
1013
1114
@@ -32,20 +35,42 @@ class ApilyticsSender:
3235
3336 _executor : ClassVar [concurrent .futures .Executor ]
3437
35- def __init__ (self , * , api_key : str , path : str , method : str ) -> None :
38+ _apilytics_version_template : ClassVar [
39+ str
40+ ] = f"{{integration}}/{ apilytics .__version__ } ;python/{ platform .python_version ()} "
41+
42+ def __init__ (
43+ self ,
44+ * ,
45+ api_key : str ,
46+ path : str ,
47+ method : str ,
48+ apilytics_integration : Optional [str ] = None ,
49+ integrated_library : Optional [str ] = None ,
50+ ) -> None :
3651 """
3752 Initialize the context manager with info from the HTTP request object.
3853
3954 Args:
4055 api_key: The API key for your Apilytics origin.
4156 path: Path of the user's HTTP request, e.g. "/foo/bar/123".
4257 method: Method of the user's HTTP request, e.g. "GET".
58+ apilytics_integration: Name of the Apilytics integration that's calling this,
59+ e.g. "apilytics-python-django". No need to pass this when calling from user code.
60+ integrated_library: Name and version of the integration that this is used in,
61+ e.g. "django/3.2.1". No need to pass this when calling from user code.
4362 """
4463 self ._api_key = api_key
4564 self ._path = path
4665 self ._method = method
4766 self ._status_code : Optional [int ] = None
4867
68+ self ._apilytics_version = self ._apilytics_version_template .format (
69+ integration = apilytics_integration or "apilytics-python-core"
70+ )
71+ if integrated_library :
72+ self ._apilytics_version += f";{ integrated_library } "
73+
4974 def __enter__ (self ) -> "ApilyticsSender" :
5075 """Start the timer, measuring how long the ``with`` block takes to execute."""
5176 self ._start_time_ns = time .perf_counter_ns ()
@@ -85,6 +110,7 @@ def _send_metrics(self) -> None:
85110 headers = {
86111 "Content-Type" : "application/json" ,
87112 "X-API-Key" : self ._api_key ,
113+ "Apilytics-Version" : self ._apilytics_version ,
88114 },
89115 )
90116 data = {
0 commit comments