11package main
22
33import (
4- "context"
54 "encoding/json"
65 "fmt"
76 "os"
@@ -50,6 +49,8 @@ type ProviderNewRelic struct {
5049 worker * worker.RelicWorker
5150 // pluginHealth current state of extension
5251 pluginHealth model.HealthState
52+ // pluginIfo short info of new relic provider
53+ pluginInfo model.ProviderDescription
5354}
5455
5556// LoadConfig of new relic extension
@@ -93,31 +94,23 @@ func (nr *ProviderNewRelic) DispatchGraph() (model.Graph, error) {
9394// FetchNewData returns latest assembled graph
9495func (nr * ProviderNewRelic ) FetchNewData (log logger.ILogger ) (model.Graph , error ) {
9596 g := nr .prepareGraph ()
97+ timeout := time .After (time .Duration (nr .Config .SurveyIntervalSec * 2 + 1 ) * time .Second )
98+ isProcessed := make (chan bool , 1 )
9699
97- log .Debug (fmt .Sprintf ("Harvesting data from NewRelic API..." ))
98-
99- ctx , cancel := context .WithCancel (context .Background ())
100-
101- // Get info from API with timeout
102- ticker := time .NewTicker (time .Duration (nr .Config .SurveyIntervalSec * 2 + 1 ) * time .Second )
103- defer ticker .Stop ()
104- isExecuted := false // all only one coroutine
100+ go func (g * model.Graph ) {
101+ log .Debug (fmt .Sprintf ("Harvesting data from NewRelic API..." ))
102+ nr .fetchMetricsWithGraph (g , log )
103+ isProcessed <- true
104+ }(g )
105105
106106 for {
107107 select {
108- default :
109- if isExecuted {
110- continue
111- }
112-
113- isExecuted = true
114- go func (g * model.Graph ) {
115- defer cancel ()
116- nr .fetchMetricsWithGraph (g , log )
117- }(g )
118- case <- ticker .C :
119- cancel ()
120- case <- ctx .Done ():
108+ case <- isProcessed :
109+ nr .pluginHealth = model .HealthNormal
110+ return * g , nil
111+ case <- timeout :
112+ log .Error ("Timeout got from NewRelic provider..." )
113+ nr .pluginHealth = model .HealthWarning
121114 return * g , nil
122115 }
123116 }
@@ -128,6 +121,11 @@ func (nr *ProviderNewRelic) ProvideHealth() model.HealthState {
128121 return nr .pluginHealth
129122}
130123
124+ // GetDescription of provider
125+ func (nr * ProviderNewRelic ) GetDescription () model.ProviderDescription {
126+ return nr .pluginInfo
127+ }
128+
131129// fetchMetricsWithGraph from API and put in to graph
132130func (nr * ProviderNewRelic ) fetchMetricsWithGraph (g * model.Graph , log logger.ILogger ) {
133131 appList := g .GetAllVertices ()
@@ -191,5 +189,11 @@ func (nr *ProviderNewRelic) prepareGraph() (g *model.Graph) {
191189
192190// NewProvider returns instance with implemented interface
193191func NewProvider () extension.IProvider {
194- return & ProviderNewRelic {pluginHealth : model .HealthNormal }
192+ return & ProviderNewRelic {
193+ pluginHealth : model .HealthNormal ,
194+ pluginInfo : model.ProviderDescription {
195+ Name : "New Relic API" ,
196+ MetricsDescription : "Requests per minute" ,
197+ },
198+ }
195199}
0 commit comments