项目作者: ejschoen

项目描述 :
Wrap clj-http requests and ring handlers and responses with correlation ids for use in distributed systems.
高级语言: Clojure
项目地址: git://github.com/ejschoen/ring-correlation-id.git
创建时间: 2019-06-23T16:16:50Z
项目社区:https://github.com/ejschoen/ring-correlation-id

开源协议:Eclipse Public License 1.0

下载


ring-correlation-id

A Ring-compatible Clojure library for correlation id middleware, to aid in tracing activity in a distributed system.

Usage

  1. (use '[ring.middleware.correlation-id :as r-id])
  2. ;; For clj-http, provides an additional-middleware function the creates or conveys the current
  3. ;; correlation id:
  4. (use '[clj-http.client :as http])
  5. (use '[clj-http.middleware.correlation-id :as c-id])
  6. ;; For Ring: r-id/wrap-correlation-id ensures a :correlation-id header, and binds
  7. ;; ring.middleware.correlation-id/*correlation-id*.
  8. ;; Also sets Timbre *context* to {:correlation-id *correlation-id*},
  9. ;; so that the correlation id is natively accessible to appenders.
  10. ;; c-id/ring-wrap-correlation-id adds additional middleware to clj-http, to inject
  11. ;; *correlation-id* into the request headers.
  12. (def app
  13. (-> handler
  14. c-id/ring-wrap-correlation-id
  15. r-id/wrap-correlation-id))
  16. (defn my-handler [request]
  17. {:status 200
  18. :body (format "Your correlation ID: %s" r-id/*correlation-id*)})
  19. ;; To just add correlation-id middleware to clj-http:
  20. (http/with-additional-middleware
  21. [#'c-id/wrap-correlation-id]
  22. (http/get ...))
  23. ;; For Timbre, provides middleware that adds :correlation-id to the data map, so it's available
  24. ;; to all appenders.
  25. ;; This is most useful, for instance, with a custom appender that puts correlation-id into
  26. ;; saved state--perhaps into logstash.
  27. (use '[timbre.middleware.correlation-id :as t-id])
  28. (t-id/merge-correlation-id-middleware!)
  29. ;; or
  30. (t-id/with-correlation-id-middleware
  31. ... your body here )

To tie this all together, use a Timbre appender (such as taoensso.timbre.appenders.3rd-party.logstash) in all communicating applications to write to a common data store.

License

Copyright © 2019 Eric Schoen

Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.