Skip to content

Commit

Permalink
Merge pull request #25 from newrelic/gsanchez/fix/cache_update
Browse files Browse the repository at this point in the history
Improve performance
  • Loading branch information
gsanchezgavier authored Sep 9, 2020
2 parents b73233c + e1c8281 commit 05274cf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 312 deletions.
1 change: 0 additions & 1 deletion accumulators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ All metrics include the PCF meta data. Only PCFContainerMetric and PCFLogMessage
| PCFContainerMetric | ContainerMetric | Application specific metrics | [`accumulators/container/container.go`](container/container.go)
| PCFValueMetric | ValueMetric | PCF System metrics of multiple metric types | [`accumulators/value/value.go`](value/value.go)
| PCFCounterEvent | CounterEvent | PCF System metrics as counter types only | [`accumulators/counter/counter.go`](counter/counter.go)
| PCFCapacity | ValueMetric | PCF System metric, derived from Total and Remaining samples in order to provide percent used. | [`accumulators/capacity/capacity.go`](capacity/capacity.go)
| PCFLogMessage | LogMessage | PCF Logs | [`accumulators/logmessage/logmessage.go`](logmessage/logmessage.go)
| PCFHttpStartStop | HttpStartStop | PCF HTTP request details | [`accumulators/http/http.go`](http/http.go)
239 changes: 0 additions & 239 deletions accumulators/capacity/capacity.go

This file was deleted.

33 changes: 26 additions & 7 deletions cfclient/cfapps/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ type Cache struct {
Collection map[string]*CFApp
WriteBuffer chan *CFApp
sync *sync.RWMutex
isUpdating bool
}

// NewCache ...
func NewCache() *Cache {
cache := &Cache{
Collection: map[string]*CFApp{},
WriteBuffer: make(chan *CFApp, 1024),
WriteBuffer: make(chan *CFApp, app.Get().Config.GetDuration("FIREHOSE_CACHE_WRITE_BUFFER_SIZE")),
sync: &sync.RWMutex{},
}
cache.Start()
Expand All @@ -32,7 +33,8 @@ func NewCache() *Cache {
func (c *Cache) Start() {
go func() {
cacheDuration := app.Get().Config.GetDuration("FIREHOSE_CACHE_DURATION_MINS")
update := time.NewTicker(30 * time.Second).C
cacheUpdate := app.Get().Config.GetDuration("FIREHOSE_CACHE_UPDATE_INTERVAL_SECS")
update := time.NewTicker(cacheUpdate * time.Second).C
timeoutCache := time.NewTicker(cacheDuration * time.Minute).C

for {
Expand All @@ -53,12 +55,9 @@ func (c *Cache) Start() {
c.sync.Unlock()

case <-update:
GetInstance().app.Log.Debug("Updating status of applications")
c.sync.RLock()
for _, v := range c.Collection {
v.UpdateInstances()
if !c.isUpdating {
go c.updateInstances()
}
c.sync.RUnlock()

case app := <-c.WriteBuffer:
c.sync.Lock()
Expand All @@ -69,6 +68,26 @@ func (c *Cache) Start() {
}()
}

func (c *Cache) updateInstances() {
c.isUpdating = true
GetInstance().app.Log.Debug("Updating status of applications")
now := time.Now()

var apps []*CFApp
c.sync.RLock()
for _, v := range c.Collection {
apps = append(apps, v)
}
c.sync.RUnlock()

for _, a := range apps {
a.UpdateInstances()
}

c.isUpdating = false
GetInstance().app.Log.Debugf("Finish cache updating %s", time.Since(now))
}

// Get ...
func (c *Cache) Get(id string) (app *CFApp, found bool) {
c.sync.RLock()
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func set() *Config {

// Cache purge threshold in minutes
v.SetDefault("FIREHOSE_CACHE_DURATION_MINS", 30)
// Cache instance update in seconds
v.SetDefault("FIREHOSE_CACHE_UPDATE_INTERVAL_SECS", 60)
// Cache instance update in seconds
v.SetDefault("FIREHOSE_CACHE_WRITE_BUFFER_SIZE", 2048)
// Rate limiter burst limit
v.SetDefault("FIREHOSE_RATE_BURST", 5)
// Rate limiter timeout in seconds.
Expand All @@ -92,8 +96,6 @@ func set() *Config {
v.SetDefault("NEWRELIC_DRAIN_INTERVAL", "59s")
v.SetDefault("NEWRELIC_ENQUEUE_TIMEOUT", "1s")

v.SetDefault("CAPACITY_ENTITY_AGE_MINS", 5)

v.SetDefault(NewRelicEventTypeContainer, "PCFContainerMetric")
v.SetDefault(NewRelicEventTypeValueMetric, "PCFValueMetric")
v.SetDefault(NewRelicEventTypeCounterEvent, "PCFCounterEvent")
Expand Down Expand Up @@ -128,7 +130,6 @@ func set() *Config {

// Filtering capabilities for envelope types - | separated values.
// By default, all message types are enabled. User configurations will override this behavior.
// Capacity accumulator is enabled by default when ValueMetric is enabled.
v.SetDefault("ENABLED_ENVELOPE_TYPES", "ContainerMetric|CounterEvent|HttpStartStop|LogMessage|ValueMetric")

// Default account location will be US unless set to EU by cf push or tile.
Expand Down
5 changes: 2 additions & 3 deletions dashboard/dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"data": [
{
"nrql": "SELECT count(*) AS 'Event Count' FROM PCFCapacity, PCFContainerMetric, PCFCounterEvent, PCFHttpStartStop, PCFLogMessage, PCFValueMetric facet pcf.domain"
"nrql": "SELECT count(*) AS 'Event Count' FROM PCFContainerMetric, PCFCounterEvent, PCFHttpStartStop, PCFLogMessage, PCFValueMetric facet pcf.domain"
}
],
"presentation": {
Expand Down Expand Up @@ -365,7 +365,7 @@
},
"data": [
{
"nrql": "SELECT count(*) as 'All Events' FROM PCFCapacity, PCFContainerMetric, PCFCounterEvent, PCFHttpStartStop, PCFLogMessage, PCFValueMetric TIMESERIES"
"nrql": "SELECT count(*) as 'All Events' FROM PCFContainerMetric, PCFCounterEvent, PCFHttpStartStop, PCFLogMessage, PCFValueMetric TIMESERIES"
}
],
"presentation": {
Expand All @@ -376,7 +376,6 @@
],
"filter": {
"event_types": [
"PCFCapacity",
"PCFContainerMetric",
"PCFCounterEvent",
"PCFHttpStartStop",
Expand Down
2 changes: 0 additions & 2 deletions newrelic/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package registry

import (
"github.com/newrelic/newrelic-pcf-nozzle-tile/accumulators/capacity"
"github.com/newrelic/newrelic-pcf-nozzle-tile/accumulators/container"
"github.com/newrelic/newrelic-pcf-nozzle-tile/accumulators/counter"
"github.com/newrelic/newrelic-pcf-nozzle-tile/accumulators/http"
Expand All @@ -21,7 +20,6 @@ var Register = &Accumulators{
counter.Metrics{},
container.Metrics{},
value.Metrics{},
capacity.Metrics{},
logmessage.Nrevents{},
http.Nrevents{},
}
Loading

0 comments on commit 05274cf

Please sign in to comment.