Skip to content

analytics: structured access logging for Plumber API #49

@bbest

Description

@bbest

Context

The CalCOFI Plumber API has no usage logging. Add a request logging filter to capture endpoint usage over time.

Implementation

Add a plumber middleware filter before pr_run() in the run script or plumber.R:

pr |> pr_filter("logger", function(req, res) {
  cat(
    jsonlite::toJSON(list(
      ts     = format(Sys.time(), "%Y-%m-%dT%H:%M:%SZ"),
      method = req$REQUEST_METHOD,
      path   = req$PATH_INFO,
      query  = req$QUERY_STRING,
      ip     = req$REMOTE_ADDR
    ), auto_unbox = TRUE), "\n",
    file   = "/var/log/calcofi-api.log",
    append = TRUE
  )
  plumber::forward()
})

GCS Sync

Periodically sync /var/log/calcofi-api.log to a GCS bucket for long-term retention:

googleCloudStorageR::gcs_upload(
  "/var/log/calcofi-api.log",
  bucket = "calcofi-logs",
  name   = paste0("api/", Sys.Date(), ".log")
)

Consider the logger package for structured JSON output and log rotation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions