{"id":15657331,"url":"https://github.com/smacker/newrelic-context","last_synced_at":"2025-07-09T17:36:02.428Z","repository":{"id":54254362,"uuid":"73572880","full_name":"smacker/newrelic-context","owner":"smacker","description":"Contains different helpers to make life easier with NewRelic and Context.","archived":false,"fork":false,"pushed_at":"2021-03-01T11:19:39.000Z","size":28,"stargazers_count":23,"open_issues_count":3,"forks_count":20,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-19T09:58:39.294Z","etag":null,"topics":["context","golang","gorm","newrelic","redis"],"latest_commit_sha":null,"homepage":null,"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/smacker.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}},"created_at":"2016-11-12T19:13:39.000Z","updated_at":"2023-02-03T19:31:14.000Z","dependencies_parsed_at":"2022-08-13T10:10:24.377Z","dependency_job_id":null,"html_url":"https://github.com/smacker/newrelic-context","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fnewrelic-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fnewrelic-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fnewrelic-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fnewrelic-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smacker","download_url":"https://codeload.github.com/smacker/newrelic-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252136905,"owners_count":21700097,"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":["context","golang","gorm","newrelic","redis"],"created_at":"2024-10-03T13:06:21.665Z","updated_at":"2025-05-03T03:19:47.132Z","avatar_url":"https://github.com/smacker.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# newrelic-context\n\nContains different helpers to make life easier with NewRelic and Context.\n\n## Installation\n\n`go get github.com/smacker/newrelic-context`\n\n## In this package:\n\n* `ContextWithTxn` - Set NewRelic transaction to context\n* `GetTnxFromContext` - Get NewRelic transaction from context anywhere\n* `NewRelicMiddleware` - Reports time in NewRelic and sets transaction in context\n* `WrapHTTPClient` - Wraps client transport with newrelic RoundTripper with transaction from context\n* `SetTxnToGorm` - Sets transaction from Context to gorm settings\n* `AddGormCallbacks` - Adds callbacks to NewRelic, you should call SetTxnToGorm to make them work\n* `RedisWrapper` - Logs gopkg.in/redis.v5 time in newrelic\n* `WrapRedisClient` - Returns copy of redis client with newrelic for transaction\n\nAPI documentation is available on [godoc.org](https://godoc.org/github.com/smacker/newrelic-context)\n\n## Examples:\n\nUse NewRelicMiddleware:\n\n```go\npackage main\n\nimport (\n    \"github.com/smacker/newrelic-context\"\n    \"log\"\n    \"net/http\"\n)\n\nfunc indexHandlerFunc(rw http.ResponseWriter, req *http.Request) {\n    rw.Write([]byte(\"I'm an index page!\"))\n}\n\nfunc main() {\n    var handler http.Handler\n\n    handler = http.HandlerFunc(indexHandlerFunc)\n    nrmiddleware, err := nrcontext.NewMiddleware(\"test-app\", \"my-license-key\")\n    if err != nil {\n        log.Print(\"Can't create newrelic middleware: \", err)\n    } else {\n        handler = nrmiddleware.Handler(handler)\n    }\n\n    http.ListenAndServe(\":8000\", handler)\n}\n\n```\n\nUse HTTPClientWrap:\n\n```go\nfunc Consume(ctx context.Context, query string) {\n    client := \u0026http.Client{Timeout: 10}\n    nrcontext.WrapHTTPClient(ctx, client)\n    _, err := client.Get(fmt.Sprintf(\"https://www.google.com.vn/?q=%v\", query))\n    if err != nil {\n        log.Println(\"Can't fetch google :(\")\n        return\n    }\n    log.Println(\"Google fetched successfully!\")\n}\n```\n\nUse with Gorm:\n\n```go\nvar db *gorm.DB\n\nfunc initDB() *gorm.DB {\n    db, err := gorm.Open(\"sqlite3\", \"./foo.db\")\n    if err != nil {\n        panic(err)\n    }\n    nrgorm.AddGormCallbacks(db)\n    return db\n}\n\nfunc catalogPage(db *gorm.DB) http.Handler {\n    return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {\n        var products []Product\n        db := nrcontext.SetTxnToGorm(req.Context(), db)\n        db.Find(\u0026products)\n        for i, v := range products {\n            rw.Write([]byte(fmt.Sprintf(\"%v. %v\\n\", i, v.Name)))\n        }\n    })\n}\n\nfunc main() {\n    db = initDB()\n    defer db.Close()\n\n    handler := catalogPage(db)\n    nrmiddleware, _ := nrcontext.NewMiddleware(\"test-app\", \"my-license-key\")\n    handler = nrmiddleware.Handler(handler)\n\n    http.ListenAndServe(\":8000\", handler)\n}\n```\n\nUse with Redis:\n\n```go\nfunc catalogPage(redisClient *redis.Client) http.Handler {\n    return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {\n        redisClient := WrapRedisClient(req.Context(), redisClient)\n\n        redisClient.Set(\"key\", \"value\", 0)\n        // ...\n        redisClient.Get(\"other-key\")\n    })\n}\n\nfunc main() {\n    client := redis.NewClient(...)\n\n    handler := catalogPage(db)\n    nrmiddleware, _ := nrcontext.NewMiddleware(\"test-app\", \"my-license-key\")\n    handler = nrmiddleware.Handler(handler)\n\n    http.ListenAndServe(\":8000\", handler)\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmacker%2Fnewrelic-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmacker%2Fnewrelic-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmacker%2Fnewrelic-context/lists"}