{"id":31757006,"url":"https://github.com/jcchavezs/mdctx","last_synced_at":"2025-10-09T19:32:43.213Z","repository":{"id":73797781,"uuid":"154379253","full_name":"jcchavezs/mdctx","owner":"jcchavezs","description":"Mapped Diagnostic Context for logging in golang","archived":false,"fork":false,"pushed_at":"2019-07-10T16:32:34.000Z","size":13,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-09T19:31:53.475Z","etag":null,"topics":["go","golang","logging","mdc","observability"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcchavezs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-23T18:41:17.000Z","updated_at":"2021-02-24T01:28:41.000Z","dependencies_parsed_at":"2024-02-18T04:00:32.520Z","dependency_job_id":null,"html_url":"https://github.com/jcchavezs/mdctx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jcchavezs/mdctx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcchavezs%2Fmdctx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcchavezs%2Fmdctx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcchavezs%2Fmdctx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcchavezs%2Fmdctx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcchavezs","download_url":"https://codeload.github.com/jcchavezs/mdctx/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcchavezs%2Fmdctx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001965,"owners_count":26083244,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["go","golang","logging","mdc","observability"],"created_at":"2025-10-09T19:30:59.011Z","updated_at":"2025-10-09T19:32:43.205Z","avatar_url":"https://github.com/jcchavezs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mdctx\n\n[![Build Status](https://travis-ci.com/jcchavezs/mdctx.svg?branch=master)](https://travis-ci.com/jcchavezs/mdctx)\n[![Go Report Card](https://goreportcard.com/badge/github.com/jcchavezs/mdctx)](https://goreportcard.com/report/github.com/jcchavezs/mdctx)\n[![GoDoc](https://godoc.org/github.com/jcchavezs/mdctx?status.svg)](https://godoc.org/github.com/jcchavezs/mdctx)\n\n\nMapped Diagnostic Context (MDC) for Go logging\n\nThe idea of Mapped Diagnostic Context is to provide a way to enrich log messages with pieces of information that could be not available in the scope where the logging actually occurs, but that can be indeed useful to better track the execution of the program.\n\n## Usage\n\n```go\nfunc Middleware(next http.Handler) http.Handler {\n  \treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tval := r.Header.Get(RequestIDHeader)\n\t\tif val != \"\" {\n            ctx := mdctx.Add(r.Context(), \"request_id\", val)\n\t\t\tr = r.WithContext(ctx)\n\t\t}\n\t\tnext.ServeHTTP(w, r)\n\t})\n}\n```\n\n```go\nfunc (r *repository) DoSomething(ctx context.Context, ...) {\n    logger := mdctx.With(ctx, r.logger)\n    ...\n    logger.Log(\"key\", \"value\")\n}\n```\n\n## Providers\n\nProviders allows you to include additional context to the logs coming from other\nsources. E.g. if a middleware include the `request_id` in the context under a private\nkey, you can use the API of that middleware to inject that `request_id` in the mdc.\n\n```go\npackage requestIDMiddleware\n\ntype reqIDKey string\n\nfunc Middleware(next http.Handler) http.Handler {\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        val := r.Header.Get(RequestIDHeader)\n        if val != \"\" {\n            r.WithContext(context.WithValue(r.Context(), reqIDKey, val))\n\t\t}\n\t\tnext.ServeHTTP(w, r)\n\t})\n}\n```\n\n```go\npackage requestID\n\nfunc Provider(ctx context.Context) context.Context {\n\treturn context.WithValue(ctx, \"request_id\", ctx.Value(reqIDKey))\n}\n```\n\n```go\npackage main\n\nfunc main() {\n\tmdctx.RegisterProvider(requestID.Provider)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcchavezs%2Fmdctx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcchavezs%2Fmdctx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcchavezs%2Fmdctx/lists"}