This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove varz shim from metron"
This reverts commit dee5e0f. [#97731982]
- Loading branch information
Showing
6 changed files
with
426 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package varzforwarder | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/cloudfoundry/sonde-go/events" | ||
) | ||
|
||
type metrics struct { | ||
metricsByName map[string]float64 | ||
timer *time.Timer | ||
} | ||
|
||
func (metrics *metrics) processMetric(metric *events.Envelope) { | ||
switch metric.GetEventType() { | ||
case events.Envelope_ValueMetric: | ||
metrics.processValueMetric(metric) | ||
case events.Envelope_CounterEvent: | ||
metrics.processCounterEvent(metric) | ||
case events.Envelope_HttpStartStop: | ||
metrics.processHTTPStartStop(metric) | ||
} | ||
} | ||
|
||
func (metrics *metrics) processValueMetric(metric *events.Envelope) { | ||
metrics.metricsByName[metric.GetValueMetric().GetName()] = metric.GetValueMetric().GetValue() | ||
} | ||
|
||
func (metrics *metrics) processCounterEvent(metric *events.Envelope) { | ||
eventName := metric.GetCounterEvent().GetName() | ||
count := metrics.metricsByName[eventName] | ||
metrics.metricsByName[eventName] = count + float64(metric.GetCounterEvent().GetDelta()) | ||
} | ||
|
||
func (metrics *metrics) processHTTPStartStop(metric *events.Envelope) { | ||
eventName := "requestCount" | ||
count := metrics.metricsByName[eventName] | ||
metrics.metricsByName[eventName] = count + 1 | ||
|
||
startStop := metric.GetHttpStartStop() | ||
status := startStop.GetStatusCode() | ||
switch { | ||
case status >= 100 && status < 200: | ||
metrics.metricsByName["responseCount1XX"] = metrics.metricsByName["responseCount1XX"] + 1 | ||
case status >= 200 && status < 300: | ||
metrics.metricsByName["responseCount2XX"] = metrics.metricsByName["responseCount2XX"] + 1 | ||
case status >= 300 && status < 400: | ||
metrics.metricsByName["responseCount3XX"] = metrics.metricsByName["responseCount3XX"] + 1 | ||
case status >= 400 && status < 500: | ||
metrics.metricsByName["responseCount4XX"] = metrics.metricsByName["responseCount4XX"] + 1 | ||
case status >= 500 && status < 600: | ||
metrics.metricsByName["responseCount5XX"] = metrics.metricsByName["responseCount5XX"] + 1 | ||
default: | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package varzforwarder | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"metron/writers" | ||
|
||
"github.com/cloudfoundry/gosteno" | ||
"github.com/cloudfoundry/loggregatorlib/cfcomponent/instrumentation" | ||
"github.com/cloudfoundry/sonde-go/events" | ||
) | ||
|
||
type VarzForwarder struct { | ||
metricsByOrigin map[string]*metrics | ||
componentName string | ||
ttl time.Duration | ||
|
||
logger *gosteno.Logger | ||
outputWriter writers.EnvelopeWriter | ||
lock sync.RWMutex | ||
} | ||
|
||
func New(componentName string, ttl time.Duration, outputWriter writers.EnvelopeWriter, logger *gosteno.Logger) *VarzForwarder { | ||
return &VarzForwarder{ | ||
metricsByOrigin: make(map[string]*metrics), | ||
componentName: componentName, | ||
ttl: ttl, | ||
logger: logger, | ||
outputWriter: outputWriter, | ||
} | ||
} | ||
|
||
func (vf *VarzForwarder) Write(envelope *events.Envelope) { | ||
vf.addMetric(envelope) | ||
vf.resetTimer(envelope.GetOrigin()) | ||
|
||
vf.outputWriter.Write(envelope) | ||
} | ||
|
||
func (vf *VarzForwarder) Emit() instrumentation.Context { | ||
vf.lock.RLock() | ||
defer vf.lock.RUnlock() | ||
|
||
c := instrumentation.Context{Name: "forwarder"} | ||
metrics := []instrumentation.Metric{} | ||
tags := map[string]interface{}{ | ||
"component": vf.componentName, | ||
} | ||
|
||
for origin, originMetrics := range vf.metricsByOrigin { | ||
for name, value := range originMetrics.metricsByName { | ||
metricName := fmt.Sprintf("%s.%s", origin, name) | ||
metrics = append(metrics, instrumentation.Metric{Name: metricName, Value: value, Tags: tags}) | ||
} | ||
} | ||
|
||
c.Metrics = metrics | ||
return c | ||
} | ||
|
||
func (vf *VarzForwarder) addMetric(metric *events.Envelope) { | ||
vf.lock.Lock() | ||
defer vf.lock.Unlock() | ||
|
||
originMetrics, ok := vf.metricsByOrigin[metric.GetOrigin()] | ||
if !ok { | ||
vf.metricsByOrigin[metric.GetOrigin()] = vf.createMetrics(metric.GetOrigin()) | ||
originMetrics = vf.metricsByOrigin[metric.GetOrigin()] | ||
} | ||
|
||
originMetrics.processMetric(metric) | ||
} | ||
|
||
func (vf *VarzForwarder) createMetrics(origin string) *metrics { | ||
vf.logger.Debugf("creating metrics for origin %v", origin) | ||
return &metrics{ | ||
metricsByName: make(map[string]float64), | ||
timer: time.AfterFunc(vf.ttl, func() { vf.deleteMetrics(origin) }), | ||
} | ||
} | ||
|
||
func (vf *VarzForwarder) deleteMetrics(origin string) { | ||
vf.logger.Debugf("deleting metrics for origin %v", origin) | ||
vf.lock.Lock() | ||
defer vf.lock.Unlock() | ||
|
||
delete(vf.metricsByOrigin, origin) | ||
} | ||
|
||
func (vf *VarzForwarder) resetTimer(origin string) { | ||
vf.lock.RLock() | ||
defer vf.lock.RUnlock() | ||
|
||
metrics, ok := vf.metricsByOrigin[origin] | ||
if ok { | ||
metrics.timer.Reset(vf.ttl) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/metron/writers/varzforwarder/varz_forwarder_suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package varzforwarder_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestVarzForwarder(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "VarzForwarder Suite") | ||
} |
Oops, something went wrong.