{"id":20768150,"url":"https://github.com/softwaremill/correlator","last_synced_at":"2025-04-30T11:24:03.998Z","repository":{"id":36272556,"uuid":"197043251","full_name":"softwaremill/correlator","owner":"softwaremill","description":null,"archived":false,"fork":false,"pushed_at":"2022-05-15T04:50:13.000Z","size":233,"stargazers_count":15,"open_issues_count":13,"forks_count":5,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-03-30T15:47:02.355Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/softwaremill.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-15T17:28:38.000Z","updated_at":"2021-09-03T04:49:36.000Z","dependencies_parsed_at":"2022-08-09T06:30:42.294Z","dependency_job_id":null,"html_url":"https://github.com/softwaremill/correlator","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwaremill%2Fcorrelator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwaremill%2Fcorrelator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwaremill%2Fcorrelator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/softwaremill%2Fcorrelator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/softwaremill","download_url":"https://codeload.github.com/softwaremill/correlator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251689325,"owners_count":21627886,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T11:35:56.218Z","updated_at":"2025-04-30T11:24:03.961Z","avatar_url":"https://github.com/softwaremill.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Correlation id support\n\n[![Build Status](https://travis-ci.org/softwaremill/correlator.svg?branch=master)](https://travis-ci.org/softwaremill/correlator)\n\nSee [the blog](https://blog.softwaremill.com/correlation-ids-in-scala-using-monix-3aa11783db81) for introduction.\n\nCurrently supports [monix](https://monix.io) \u0026 [logback](https://logback.qos.ch).\n\nGeneric usage:\n\n* add the dependency: `\"com.softwaremill.correlator\" %% \"monix-logback-http4s\" % \"0.1.9\"` to your project\n* create an object extending the `CorrelationIdDecorator` class, e.g. `object MyCorrelationId extends CorrelationIdDecorator()`\n* call `MyCorrelationId.init()` immediately after your program starts (in the `main()` method)\n* create an implicit instance of `CorrelationIdSource` for given `T` from which you want to extract correlation id\n* wrap any `T =\u003e Task[R]` function with `MyCorrelationId.withCorrelationId`, so that a correlation id is\nextracted from the argument (using the defined `CorrelationIdSource`), or a new one is created.\n* you can access the current correlation id (if any is set) using `MyCorrelationId.apply()` or `MyCorrelationId.applySync()`. \n\n\nFor [http4s](https://http4s.org) integration:\n\n* add the dependency: `\"com.softwaremill.correlator\" %% \"monix-logback-http4s\" % \"0.1.9\"` to your project\n* create an object extending the `CorrelationIdDecorator` class, e.g. `object MyCorrelationId extends CorrelationIdDecorator()`\n* call `MyCorrelationId.init()` immediately after your program starts (in the `main()` method)\n* wrap your `HttpRoutes[Task]` with `Http4sCorrelationMiddleware(MyCorrelationId).withCorrelationId`, so that a correlation id is\nextracted from the request (using the provided header name), or a new one is created.\n* you can access the current correlation id (if any is set) using `MyCorrelationId.apply()` or `MyCorrelationId.applySync()`.\n\nLogging each request with corresponding correlationId can be done in following way:\n```scala\ndef loggingMiddleware[T, R](\n                           service: HttpRoutes[Task],\n                           logStartRequest: Request[Task] =\u003e Task[Unit] = req =\u003e\n                             Task(MyLogger.debug(s\"Starting request to: ${req.uri.path}\"))\n                         ): HttpRoutes[Task] = Kleisli{ req: Request[Task] =\u003e\n    val setupAndService = for {\n      _ \u003c- logStartRequest(req)\n      r \u003c- service(req).value\n    } yield r\n    OptionT(setupAndService)\n}\n\nval middleware = Http4sCorrelationMiddleware(correlationIdDecorator)\nmiddleware.withCorrelationId(loggingMiddleware(service))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwaremill%2Fcorrelator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftwaremill%2Fcorrelator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwaremill%2Fcorrelator/lists"}