From 6a9d06cd50c2d86cb4423e31bd83610acf041a37 Mon Sep 17 00:00:00 2001 From: robson-coelho-zoom Date: Mon, 7 Feb 2022 13:08:09 -0300 Subject: [PATCH 1/4] addded TransactionEvent.MaxSamplesStored as environment variable --- main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index e28469c..b1dba50 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,8 @@ import ( "fmt" "net/http" "net/url" + "os" + "strconv" "github.com/b2wdigital/restQL-golang/v6/pkg/restql" "github.com/newrelic/go-agent/v3/newrelic" @@ -32,7 +34,17 @@ func init() { } func MakeNewRelicPlugin(logger restql.Logger) (restql.LifecyclePlugin, error) { - app, err := newrelic.NewApplication(newrelic.ConfigFromEnvironment()) + app, err := newrelic.NewApplication( + newrelic.ConfigFromEnvironment(), + func(c *newrelic.Config) { + maxSamples := os.Getenv("NEW_RELIC_TRANSACTION_EVENTS_MAX_SAMPLES_STORED") + if maxSamples != "" { + if m, err := strconv.Atoi(maxSamples); nil != err { + c.TransactionEvents.MaxSamplesStored = m + } + } + }, + ) if err != nil { logger.Error("failed to initialize new relic", err) From f4bec4ccc2bc155e210dd9b2832970e07b8d3c78 Mon Sep 17 00:00:00 2001 From: robson-coelho-zoom Date: Mon, 7 Feb 2022 13:18:21 -0300 Subject: [PATCH 2/4] update README with new env var --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f724b93..da04182 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ This plugin can be configured [through environment variables](https://github.com - `NEW_RELIC_UTILIZATION_BILLING_HOSTNAME`: sets Utilization.BillingHostname - `NEW_RELIC_UTILIZATION_LOGICAL_PROCESSORS`: sets Utilization.LogicalProcessors using [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi). - `NEW_RELIC_UTILIZATION_TOTAL_RAM_MIB`: sets Utilization.TotalRAMMIB using [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi). +- `NEW_RELIC_TRANSACTION_EVENTS_MAX_SAMPLES_STORED`: sets TransactionEvents.MaxSamplesStored using [strconv.Atoi](https://golang.org/pkg/strconv/#Atoi). ## License From 084cbf9e0fe96ee054a2fd4024b60c5183618afb Mon Sep 17 00:00:00 2001 From: robson-coelho-zoom Date: Mon, 7 Feb 2022 17:59:06 -0300 Subject: [PATCH 3/4] added func to load extra variables not exposed in the newrelic package --- config.go | 35 +++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 11 +---------- 4 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 config.go diff --git a/config.go b/config.go new file mode 100644 index 0000000..5aaf8d1 --- /dev/null +++ b/config.go @@ -0,0 +1,35 @@ +package restqlnewrelic + +import ( + "fmt" + "os" + "strconv" + + "github.com/newrelic/go-agent/v3/newrelic" +) + +const transactionEventsMaxSamples = "NEW_RELIC_TRANSACTION_EVENTS_MAX_SAMPLES_STORED" + +func ExtraConfigFromEnvironment() newrelic.ConfigOption { + return extraConfigFromEnvironment(os.Getenv) +} + +func extraConfigFromEnvironment(getenv func(string) string) newrelic.ConfigOption { + return func(cfg *newrelic.Config) { + // Because fields could have been assigned in a previous + // ConfigOption, we only want to assign fields using environment + // variables that have been populated. This is especially + // relevant for the string case where no processing occurs. + assignInt := func(field *int, name string) { + if env := getenv(name); env != "" { + if i, err := strconv.Atoi(env); nil != err { + cfg.Error = fmt.Errorf("invalid %s value: %s", name, env) + } else { + *field = i + } + } + } + + assignInt(&cfg.TransactionEvents.MaxSamplesStored, transactionEventsMaxSamples) + } +} diff --git a/go.mod b/go.mod index 8428709..9b2bf66 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/b2wdigital/restQL-golang/v6 v6.2.0 + github.com/go-errors/errors v1.4.2 github.com/newrelic/go-agent/v3 v3.9.0 github.com/pkg/errors v0.9.1 ) diff --git a/go.sum b/go.sum index 548752e..e27ded1 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fasthttp/router v1.3.2/go.mod h1:athTSKMdel0Qhh3W4nB8qn+EPYuyj6YZMUo6ZcXWTgc= +github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= +github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= diff --git a/main.go b/main.go index b1dba50..d8439ca 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,6 @@ import ( "fmt" "net/http" "net/url" - "os" - "strconv" "github.com/b2wdigital/restQL-golang/v6/pkg/restql" "github.com/newrelic/go-agent/v3/newrelic" @@ -36,14 +34,7 @@ func init() { func MakeNewRelicPlugin(logger restql.Logger) (restql.LifecyclePlugin, error) { app, err := newrelic.NewApplication( newrelic.ConfigFromEnvironment(), - func(c *newrelic.Config) { - maxSamples := os.Getenv("NEW_RELIC_TRANSACTION_EVENTS_MAX_SAMPLES_STORED") - if maxSamples != "" { - if m, err := strconv.Atoi(maxSamples); nil != err { - c.TransactionEvents.MaxSamplesStored = m - } - } - }, + ExtraConfigFromEnvironment(), ) if err != nil { From 220fa21bedb28d143293cceab394bea970ffab3b Mon Sep 17 00:00:00 2001 From: robson-coelho-zoom Date: Mon, 7 Feb 2022 18:04:00 -0300 Subject: [PATCH 4/4] removed unused go-errors package --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index 9b2bf66..8428709 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.13 require ( github.com/b2wdigital/restQL-golang/v6 v6.2.0 - github.com/go-errors/errors v1.4.2 github.com/newrelic/go-agent/v3 v3.9.0 github.com/pkg/errors v0.9.1 ) diff --git a/go.sum b/go.sum index e27ded1..548752e 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fasthttp/router v1.3.2/go.mod h1:athTSKMdel0Qhh3W4nB8qn+EPYuyj6YZMUo6ZcXWTgc= -github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= -github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=