Skip to content

Emit metrics about API calls #134

@iainmaitland88

Description

@iainmaitland88

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions