mirror of https://github.com/grafana/grafana
feat(instrumentation): work on settings model for internal metrics publishing, #4696
parent
74101eaf7c
commit
6b2a4fe8e8
@ -0,0 +1,55 @@ |
||||
package publishers |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"net" |
||||
"time" |
||||
|
||||
"github.com/grafana/grafana/pkg/log" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
type GraphitePublisher struct { |
||||
Address string |
||||
Protocol string |
||||
Prefix string |
||||
} |
||||
|
||||
func CreateGraphitePublisher() (*GraphitePublisher, error) { |
||||
graphiteSection, err := setting.Cfg.GetSection("metrics.graphite") |
||||
if err != nil { |
||||
return nil, nil |
||||
} |
||||
|
||||
graphiteReceiver := &GraphitePublisher{} |
||||
graphiteReceiver.Protocol = "tcp" |
||||
graphiteReceiver.Address = graphiteSection.Key("address").MustString("localhost:2003") |
||||
graphiteReceiver.Prefix = graphiteSection.Key("prefix").MustString("service.grafana.%(instance_name)s") |
||||
|
||||
return graphiteReceiver, nil |
||||
} |
||||
|
||||
func (this *GraphitePublisher) Publish(metrics map[string]interface{}) { |
||||
conn, err := net.DialTimeout(this.Protocol, this.Address, time.Second*5) |
||||
|
||||
if err != nil { |
||||
log.Error(3, "Metrics: GraphitePublisher: Failed to connect to %s!", err) |
||||
return |
||||
} |
||||
|
||||
buf := bytes.NewBufferString("") |
||||
now := time.Now().Unix() |
||||
for key, value := range metrics { |
||||
metricName := this.Prefix + key |
||||
line := fmt.Sprintf("%s %d %d\n", metricName, value, now) |
||||
buf.WriteString(line) |
||||
} |
||||
|
||||
log.Trace("Metrics: GraphitePublisher.Publish() \n%s", buf) |
||||
_, err = conn.Write(buf.Bytes()) |
||||
|
||||
if err != nil { |
||||
log.Error(3, "Metrics: GraphitePublisher: Failed to send metrics! %s", err) |
||||
} |
||||
} |
||||
@ -1,44 +0,0 @@ |
||||
package receiver |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"github.com/grafana/grafana/pkg/log" |
||||
"net" |
||||
"time" |
||||
) |
||||
|
||||
type GraphiteSender struct { |
||||
Host string |
||||
Port string |
||||
Protocol string |
||||
Prefix string |
||||
} |
||||
|
||||
func (this *GraphiteSender) Send(metrics map[string]interface{}) error { |
||||
log.Debug("GraphiteSender: Sending metrics to graphite") |
||||
|
||||
address := fmt.Sprintf("%s:%s", this.Host, this.Port) |
||||
conn, err := net.DialTimeout(this.Protocol, address, time.Second*5) |
||||
|
||||
if err != nil { |
||||
return fmt.Errorf("Graphite Sender: Failed to connec to %s!", err) |
||||
} |
||||
|
||||
buf := bytes.NewBufferString("") |
||||
now := time.Now().Unix() |
||||
for key, value := range metrics { |
||||
metricName := this.Prefix + key |
||||
line := fmt.Sprintf("%s %d %d\n", metricName, value, now) |
||||
log.Debug("SendMetric: sending %s", line) |
||||
buf.WriteString(line) |
||||
} |
||||
|
||||
_, err = conn.Write(buf.Bytes()) |
||||
|
||||
if err != nil { |
||||
return fmt.Errorf("Graphite Sender: Failed to send metrics! %s", err) |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
@ -0,0 +1,47 @@ |
||||
package metrics |
||||
|
||||
import ( |
||||
"github.com/grafana/grafana/pkg/log" |
||||
"github.com/grafana/grafana/pkg/metrics/publishers" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
type MetricPublisher interface { |
||||
Publish(metrics map[string]interface{}) |
||||
} |
||||
|
||||
type MetricSettings struct { |
||||
Enabled bool |
||||
IntervalSeconds int64 |
||||
|
||||
Publishers []MetricPublisher |
||||
} |
||||
|
||||
func readSettings() *MetricSettings { |
||||
var settings = &MetricSettings{ |
||||
Enabled: false, |
||||
Publishers: make([]MetricPublisher, 0), |
||||
} |
||||
|
||||
var section, err = setting.Cfg.GetSection("metrics") |
||||
if err != nil { |
||||
log.Fatal(3, "Unable to find metrics config section") |
||||
return nil |
||||
} |
||||
|
||||
settings.Enabled = section.Key("enabled").MustBool(false) |
||||
settings.IntervalSeconds = section.Key("interval_seconds").MustInt64(10) |
||||
|
||||
if !settings.Enabled { |
||||
return settings |
||||
} |
||||
|
||||
if graphitePublisher, err := publishers.CreateGraphitePublisher(); err != nil { |
||||
log.Error(3, "Metrics: Failed to init Graphite metric publisher", err) |
||||
} else if graphitePublisher != nil { |
||||
log.Info("Metrics: Internal metrics publisher Graphite initialized") |
||||
settings.Publishers = append(settings.Publishers, graphitePublisher) |
||||
} |
||||
|
||||
return settings |
||||
} |
||||
Loading…
Reference in new issue