jpetrucciani / hubspot3

python3.6+ hubspot client based on hapipy, but modified to use the newer endpoints and non-legacy python
MIT License
146 stars 72 forks source link

Emit metrics about API calls #134

Open iainmaitland88 opened 1 year ago

iainmaitland88 commented 1 year ago

Would you accept a PR that allowed users to pass a callback function or object to the Hubspot3 class allowing it to publish metrics? For example a simple counter of HTTP requests and response codes? Something along the lines of

# hubspot3.metrics or somewhere similar
class MetricsClient(Protocol):
    def counter(self, name: str, value: int, tags: Optional[dict[str, str | int | float]]):
        pass

# hubspot3.base
class BaseClient:
    def __init__(..., metrics_client: MetricsClient = None):
        self.metrics_client = metrics_client

    def _counter(self, name: str, value: int, tags: Optional[dict[str, str | int | float]]):
        if not self.metrics_client:
            return
        try:
            self.metrics_client.counter(name, value, tags)
        except Exception:
            logging.exception("something went wrong publishing a metric")

    def _execute_raw_request(self, conn, request):
        ...
        self._counter("endpoint", 1, {"http_status": 123})
        return result

# user code
class MyMetricsClient:
    def counter(self, name: str, value: int, tags: Optional[dict[str, str | int | float]]):
        # user implemented method to publish the metric somewhere

client = Hubspot3(access_token="blah", metrics_client=MyMetricsClient)

My motivation is using the new Private Apps there is no equivalent to /integrations/v1/limit/daily that shows your API usage. I've raised a feature request with HubSpot but I'm not hopeful.