{"id":28763747,"url":"https://github.com/cockroachdb/logtags","last_synced_at":"2025-06-17T09:10:26.381Z","repository":{"id":57483271,"uuid":"192339704","full_name":"cockroachdb/logtags","owner":"cockroachdb","description":"key/value annotations for Go contexts","archived":false,"fork":false,"pushed_at":"2024-12-15T23:26:45.000Z","size":25,"stargazers_count":40,"open_issues_count":0,"forks_count":11,"subscribers_count":47,"default_branch":"master","last_synced_at":"2025-06-13T14:02:33.978Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cockroachdb.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":"2019-06-17T12:08:35.000Z","updated_at":"2025-06-01T09:07:44.000Z","dependencies_parsed_at":"2024-12-16T00:21:16.195Z","dependency_job_id":"0187c850-b3a0-4aba-af66-e56e445aa659","html_url":"https://github.com/cockroachdb/logtags","commit_stats":{"total_commits":7,"total_committers":2,"mean_commits":3.5,"dds":0.2857142857142857,"last_synced_commit":"21c54148d20b46d658d4cf001e85302739a698d1"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cockroachdb/logtags","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Flogtags","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Flogtags/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Flogtags/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Flogtags/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cockroachdb","download_url":"https://codeload.github.com/cockroachdb/logtags/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cockroachdb%2Flogtags/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260092917,"owners_count":22957560,"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":"2025-06-17T09:10:25.610Z","updated_at":"2025-06-17T09:10:26.359Z","avatar_url":"https://github.com/cockroachdb.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# logtags: key/value annotations for Go contexts\n\nThis package provides a way to attach key/value annotations\nto a Go `context.Context`.\n\nThis feature is used e.g. in CockroachDB to annotate contexts with\nuser-facing details from the call stack, for use in logging output.\n\n**How to use:**\n\n- adding k/v data to a context:\n\n  ```go\n  func AddTag(ctx context.Context, key string, value interface{}) context.Context\n  ```\n  \n  For example:\n  \n  ```go\n  func foo(ctx context.Context) {\n\t  ctx = logtags.AddTag(ctx, \"foo\", 123)\n\t  bar(ctx)\n  }\n  ```\n\n- retrieving k/v data from a context:\n\n  ```go\n  func FromContext(ctx context.Context) *Buffer\n  func (b *Buffer) Get() []Tag\n  func (t *Tag) Key() string\n  func (t *Tag) Value() interface{}\n  ```\n\n**How it works:**\n\n`logtags` stores the provided key/value pairs into an object of type\n`Buffer`, then uses Go's standard `context.WithValue` to attach the\nbuffer.\n\nAn instance of `Buffer` inside a context is immutable. When adding a\nnew k/v pair to a context that already carries a `Buffer`, a new one\nis created with all previous k/v pair, and the new k/v pair is added\nto that.\n\nThe `FromContext()` function retrieves the topmost (most recent)\n`Buffer`.\n\n**Advanced uses:**\n\nTo add multiple k/v pairs in one go, without using quadratic space in\n`Buffer` instances:\n\n1. manually instantiate a `Buffer`.\n2. use `func (*Buffer) Add(key string, value interface{})` to populate the buffer.\n3. use `func AddTags(ctx context.Context, tags *Buffer) context.Context` to embark\n   all the k/v pairs at once.\n\nTo format all the contained k/v pairs in a `Buffer`, use its\n`String()` or `FormatToString(*strings.Builder)` methods:\n- when the `value` part is `nil`,  only the key is displayed.\n- when the `value` part is non-nil, and the key is just one character\n  long, the key and value are concatenated for display. This enables e.g.\n  printing k=`\"n\"`, v=123 as `n123`.\n- otherwise, the key and value are printed with `=` as separator.\n\nFor example:\n\n```go\n   ctx = logtags.AddTag(ctx, \"foo\", 123)\n   ctx = logtags.AddTag(ctx, \"x\", 456)\n   ctx = logtags.AddTag(ctx, \"bar\", nil)\n   fmt.Println(logtags.FromContext(ctx).String())\n   // prints foo=123,x456,bar\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcockroachdb%2Flogtags","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcockroachdb%2Flogtags","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcockroachdb%2Flogtags/lists"}