Giter Club home page Giter Club logo

correlator's Introduction

Correlation id support

Build Status

See the blog for introduction.

Currently supports monix & logback.

Generic usage:

  • add the dependency: "com.softwaremill.correlator" %% "monix-logback-http4s" % "0.1.8" to your project
  • create an object extending the CorrelationIdDecorator class, e.g. object MyCorrelationId extends CorrelationIdDecorator()
  • call MyCorrelationId.init() immediately after your program starts (in the main() method)
  • create an implicit instance of CorrelationIdSource for given T from which you want to extract correlation id
  • wrap any T => Task[R] function with MyCorrelationId.withCorrelationId, so that a correlation id is extracted from the argument (using the defined CorrelationIdSource), or a new one is created.
  • you can access the current correlation id (if any is set) using MyCorrelationId.apply() or MyCorrelationId.applySync().

For http4s integration:

  • add the dependency: "com.softwaremill.correlator" %% "monix-logback-http4s" % "0.1.8" to your project
  • create an object extending the CorrelationIdDecorator class, e.g. object MyCorrelationId extends CorrelationIdDecorator()
  • call MyCorrelationId.init() immediately after your program starts (in the main() method)
  • wrap your HttpRoutes[Task] with Http4sCorrelationMiddleware(MyCorrelationId).withCorrelationId, so that a correlation id is extracted from the request (using the provided header name), or a new one is created.
  • you can access the current correlation id (if any is set) using MyCorrelationId.apply() or MyCorrelationId.applySync().

Logging each request with corresponding correlationId can be done in following way:

def loggingMiddleware[T, R](
                           service: HttpRoutes[Task],
                           logStartRequest: Request[Task] => Task[Unit] = req =>
                             Task(MyLogger.debug(s"Starting request to: ${req.uri.path}"))
                         ): HttpRoutes[Task] = Kleisli{ req: Request[Task] =>
    val setupAndService = for {
      _ <- logStartRequest(req)
      r <- service(req).value
    } yield r
    OptionT(setupAndService)
}

val middleware = Http4sCorrelationMiddleware(correlationIdDecorator)
middleware.withCorrelationId(loggingMiddleware(service))

correlator's People

Contributors

scala-steward avatar mergify[bot] avatar adamw avatar ghostbuster91 avatar kciesielski avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.