JsonApiClient v1.2.0 JsonApiClient.Middleware.StatsTracker View Source

Stats Tracking Middleware

Options

  • :name - name of the stats (used in logging)
  • :log - The log level to log at. No logging is done if false. Defaults to false

Middleware that adds stats data to response, and optionally logs it.

The JsonApiClient.Middleware.StatsTracker middleware provides instrumentation for your requests. StatsTracker can be added to the middleware stack to track the time spent in the middleware that comes after it and add that data to the Response struct. If log is spoecified in the options StatsTracker will then log all stats data in the Response struct at the specified log level. Here’s a sample configuration to add stats tracking to the http request and parsing.

config :json_api_client,
  middlewares: [
    {JsonApiClient.Middleware.StatsLogger, name: :parse_response, log: :info},
    {JsonApiClient.Middleware.DocumentParser, nil},
    {JsonApiClient.Middleware.StatsTracker, name: :http_request},
    {JsonApiClient.Middleware.HTTPClient, nil},
  ]

That would cause something like the following to be logged on each request:

15:57:30.198 [info]  total_ms=73.067 url=http://example.com/articles/123 parse_response_ms=7.01 http_request=66.057

Note that the StatsTracker middleware tracks the time spent in all the middleware that comes after it in the stack. When it logs this data it subtacts the time recorded by the next StatsTracker in the stack so that you can see the time spent in distinct potions of the middleware stack.

Consider this stack, for example:

config :json_api_client,
  middlewares: [
    {JsonApiClient.Middleware.StatsTracker, name: :custom_middleware, log: :info}, 
    {CustomMiddleware1, nil},
    {CustomMiddleware2, nil},
    {CustomMiddleware3, nil},
    {JsonApiClient.Middleware.StatsTracker, name: :request_and_parsing},
    {JsonApiClient.Middleware.DocumentParser, nil},
    {JsonApiClient.Middleware.HTTPClient, nil},
  ]

This will log the time spent in all three custom loggers as one value and the time spent preforming the http request and parsing the response as another.

15:57:30.198 [info]  total_ms=100 url=http://example.com/articles/123 custom_middleware_ms=12 request_and_parsing=88

Link to this section Summary

Link to this section Functions

Link to this function call(request, next, opts) View Source