Skip to content

Commit

Permalink
Added Max Samples per Custom Events Setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mirackara committed Jun 24, 2022
1 parent a0f3666 commit 0385173
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions internal/harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,11 @@ type HarvestConfigurer interface {
MaxSpanEvents() int
// MaxCustomEvents returns the maximum number of Custom Events that should be reported per period
MaxCustomEvents() int
// MaxSamplesStored returns the maximum number of Custom Event Samples stored in a Custom Event
MaxSamplesStored() int
// MaxErrorEvents returns the maximum number of Error Events that should be reported per period
MaxErrorEvents() int

MaxTxnEventer
}

Expand Down
8 changes: 4 additions & 4 deletions v3/internal/connect_reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,19 @@ func (r *ConnectReply) ConfigurablePeriod() time.Duration {
func uintPtr(x uint) *uint { return &x }

// DefaultEventHarvestConfig provides faster event harvest defaults.
func DefaultEventHarvestConfig(maxTxnEvents int) EventHarvestConfig {
func DefaultEventHarvestConfig(maxTxnEvents int, maxCustomEvents int) EventHarvestConfig {
cfg := EventHarvestConfig{}
cfg.ReportPeriodMs = DefaultConfigurableEventHarvestMs
cfg.Limits.TxnEvents = uintPtr(uint(maxTxnEvents))
cfg.Limits.CustomEvents = uintPtr(uint(MaxCustomEvents))
cfg.Limits.CustomEvents = uintPtr(uint(maxCustomEvents))
cfg.Limits.ErrorEvents = uintPtr(uint(MaxErrorEvents))
return cfg
}

// DefaultEventHarvestConfigWithDT is an extended version of DefaultEventHarvestConfig,
// with the addition that it takes into account distributed tracer span event harvest limits.
func DefaultEventHarvestConfigWithDT(maxTxnEvents int, dtEnabled bool, spanEventLimit int) EventHarvestConfig {
cfg := DefaultEventHarvestConfig(maxTxnEvents)
func DefaultEventHarvestConfigWithDT(maxTxnEvents int, dtEnabled bool, spanEventLimit int, maxCustomEvents int) EventHarvestConfig {
cfg := DefaultEventHarvestConfig(maxTxnEvents,maxCustomEvents)
if dtEnabled {
cfg.Limits.SpanEvents = uintPtr(uint(spanEventLimit))
}
Expand Down
8 changes: 4 additions & 4 deletions v3/newrelic/app_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"strings"
"time"

"github.com/newrelic/go-agent/v3/internal"
)

Expand Down Expand Up @@ -191,9 +190,7 @@ func (run *appRun) ptrErrorEvents() *uint { return run.Reply.EventData.Limits.E
func (run *appRun) ptrSpanEvents() *uint { return run.Reply.EventData.Limits.SpanEvents }

func (run *appRun) MaxTxnEvents() int { return run.limit(run.Config.maxTxnEvents(), run.ptrTxnEvents) }
func (run *appRun) MaxCustomEvents() int {
return run.limit(internal.MaxCustomEvents, run.ptrCustomEvents)
}

func (run *appRun) MaxErrorEvents() int {
return run.limit(internal.MaxErrorEvents, run.ptrErrorEvents)
}
Expand All @@ -204,6 +201,9 @@ func (run *appRun) MaxErrorEvents() int {
func (run *appRun) MaxSpanEvents() int {
return run.limit(run.Config.DistributedTracer.ReservoirLimit, run.ptrSpanEvents)
}
func (run *appRun) MaxCustomEvents() int {
return run.limit(run.Config.maxCustomEvents(), run.ptrCustomEvents)
}

func (run *appRun) limit(dflt int, field func() *uint) int {
if field() != nil {
Expand Down
16 changes: 14 additions & 2 deletions v3/newrelic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ type Config struct {
// custom analytics events. High security mode overrides this
// setting.
Enabled bool
// MaxSamplesStored sets the desired maximum custom events stored
MaxSamplesStored int
}

// TransactionEvents controls the behavior of transaction analytics
// events.
TransactionEvents struct {
Expand Down Expand Up @@ -382,6 +383,7 @@ func defaultConfig() Config {
c.Enabled = true
c.Labels = make(map[string]string)
c.CustomInsightsEvents.Enabled = true
c.CustomInsightsEvents.MaxSamplesStored = internal.MaxCustomEvents
c.TransactionEvents.Enabled = true
c.TransactionEvents.Attributes.Enabled = true
c.TransactionEvents.MaxSamplesStored = internal.MaxTxnEvents
Expand Down Expand Up @@ -503,6 +505,16 @@ func (c Config) validateTraceObserverConfig() (*observerURL, error) {
}, nil
}

// maxCustomEvents returns the configured maximum number of Custom Events if it has been configured
// and is less than the default maximum; otherwise it returns the default max.
func (c Config) maxCustomEvents() int {
configured := c.CustomInsightsEvents.MaxSamplesStored
if configured < 0 || configured > internal.MaxCustomEvents {
return internal.MaxTxnEvents
}
return configured
}

// maxTxnEvents returns the configured maximum number of Transaction Events if it has been configured
// and is less than the default maximum; otherwise it returns the default max.
func (c Config) maxTxnEvents() int {
Expand Down Expand Up @@ -667,7 +679,7 @@ func configConnectJSONInternal(c Config, pid int, util *utilization.Data, e envi
Util: util,
SecurityPolicies: securityPolicies,
Metadata: metadata,
EventData: internal.DefaultEventHarvestConfigWithDT(c.maxTxnEvents(), c.DistributedTracer.Enabled, c.DistributedTracer.ReservoirLimit),
EventData: internal.DefaultEventHarvestConfigWithDT(c.maxTxnEvents(), c.DistributedTracer.Enabled, c.DistributedTracer.ReservoirLimit, c.maxCustomEvents()),
}})
}

Expand Down
8 changes: 8 additions & 0 deletions v3/newrelic/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func ConfigDistributedTracerEnabled(enabled bool) ConfigOption {
return func(cfg *Config) { cfg.DistributedTracer.Enabled = enabled }
}

// ConfigCustomInsightsEventsMaxSamplesLimit alters the sample size allowing control
// of how many custom events are stored in an agent for a given harvest cycle.
// Alters the CustomInsightsEvents.MaxSamplesStored setting.
func ConfigCustomInsightsEventsMaxSamplesStored(limit int) ConfigOption {
return func(cfg *Config) { cfg.CustomInsightsEvents.MaxSamplesStored = limit}
}


// ConfigDistributedTracerReservoirLimit alters the sample reservoir size (maximum
// number of span events to be collected) for distributed tracing instead of
// using the built-in default.
Expand Down
5 changes: 4 additions & 1 deletion v3/newrelic/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ func TestCopyConfigReferenceFieldsPresent(t *testing.T) {
"Enabled":true
},
"CrossApplicationTracer":{"Enabled":false},
"CustomInsightsEvents":{"Enabled":true},
"CustomInsightsEvents":{
"Enabled":true,
"MaxSamplesStored":1000
},
"DatastoreTracer":{
"DatabaseNameReporting":{"Enabled":true},
"InstanceReporting":{"Enabled":true},
Expand Down

0 comments on commit 0385173

Please sign in to comment.