项目作者: wavefrontHQ

项目描述 :
Wavefront plugin for go-metrics
高级语言: Go
项目地址: git://github.com/wavefrontHQ/go-metrics-wavefront.git
创建时间: 2016-05-20T06:45:28Z
项目社区:https://github.com/wavefrontHQ/go-metrics-wavefront

开源协议:Apache License 2.0

下载


Warning

VMware has ended active development of this project. this repository will no longer be updated.

go-metrics-wavefront Go Report Card GoDoc

This is a plugin for go-metrics which adds a Wavefront reporter and a simple abstraction that supports tagging at the host and metric level.

Imports

  1. import (
  2. metrics "github.com/rcrowley/go-metrics"
  3. "github.com/wavefronthq/go-metrics-wavefront/reporting"
  4. "github.com/wavefronthq/wavefront-sdk-go/application"
  5. "github.com/wavefronthq/wavefront-sdk-go/senders"
  6. )

Set Up a Wavefront Reporter

This SDK provides a WavefrontMetricsReporter that allows you to:

  • Report metrics to Wavefront at regular intervals or
  • Manually report metrics to Wavefront

The steps for creating a WavefrontMetricsReporter are:

  1. Create a Wavefront Sender for managing communication with Wavefront.
  2. Create a WavefrontMetricsReporter

1. Set Up a Wavefront Sender

A “Wavefront sender” is an object that implements the low-level interface for sending data to Wavefront. You can choose to send data using either the Wavefront proxy or direct ingestion.

  • If you have already set up a Wavefront sender for another SDK that will run in the same process, use that one. (For details, see Share a Wavefront Sender.)
  • Otherwise, follow the steps in Set Up a Wavefront Sender to configure a proxy Sender or a direct Sender.

The following example configures a direct Sender with default direct ingestion properties:

  1. directCfg := &senders.DirectConfiguration{
  2. Server: "https://INSTANCE.wavefront.com",
  3. Token: "YOUR_API_TOKEN",
  4. }
  5. sender, err := senders.NewDirectSender(directCfg)
  6. if err != nil {
  7. panic(err)
  8. }

2. Create the WavefrontMetricsReporter

The WavefrontMetricsReporter supports tagging at the host level. Any tags passed to the reporter here will be applied to every metric before being sent to Wavefront.

To create the WavefrontMetricsReporter you initialize it with the sender instance you created in the previous step along with a few other properties:

  1. reporter := reporting.NewMetricsReporter(
  2. sender,
  3. reporting.ApplicationTag(application.New("app", "srv")),
  4. reporting.Source("go-metrics-test"),
  5. reporting.Prefix("some.prefix"),
  6. reporting.LogErrors(true),
  7. )

Tagging Metrics

In addition to tagging at the reporter level, you can add tags to individual metrics:

  1. tags := map[string]string{
  2. "key1": "val1",
  3. "key2": "val2",
  4. }
  5. counter := metrics.NewCounter() // Create a counter
  6. reporter.RegisterMetric("foo", counter, tags) // will create a 'some.prefix.foo.count' metric with tags
  7. counter.Inc(47)

Extended Code Example

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "time"
  7. metrics "github.com/rcrowley/go-metrics"
  8. "github.com/wavefronthq/go-metrics-wavefront/reporting"
  9. "github.com/wavefronthq/wavefront-sdk-go/application"
  10. "github.com/wavefronthq/wavefront-sdk-go/senders"
  11. )
  12. func main() {
  13. //Tags we'll add to the metric
  14. tags := map[string]string{
  15. "key2": "val2",
  16. "key1": "val1",
  17. "key0": "val0",
  18. "key4": "val4",
  19. "key3": "val3",
  20. }
  21. // Create a direct sender
  22. directCfg := &senders.DirectConfiguration{
  23. Server: "https://" + os.Getenv("WF_INSTANCE") + ".reporting.com",
  24. Token: os.Getenv("WF_TOKEN"),
  25. BatchSize: 10000,
  26. MaxBufferSize: 50000,
  27. FlushIntervalSeconds: 1,
  28. }
  29. sender, err := senders.NewDirectSender(directCfg)
  30. if err != nil {
  31. panic(err)
  32. }
  33. reporter := reporting.NewMetricsReporter(
  34. sender,
  35. reporting.ApplicationTag(application.New("app", "srv")),
  36. reporting.Source("go-metrics-test"),
  37. reporting.Prefix("some.prefix"),
  38. reporting.LogErrors(true),
  39. )
  40. counter := metrics.NewCounter() //Create a counter
  41. reporter.RegisterMetric("foo", counter, tags) // will create a 'some.prefix.foo.count' metric with tags
  42. counter.Inc(47)
  43. histogram := reporting.NewHistogram()
  44. reporter.RegisterMetric("duration", histogram, tags) // will create a 'some.prefix.duration' histogram metric with tags
  45. histogram2 := reporting.NewHistogram()
  46. reporter.Register("duration2", histogram2) // will create a 'some.prefix.duration2' histogram metric with no tags
  47. deltaCounter := metrics.NewCounter()
  48. reporter.RegisterMetric(reporting.DeltaCounterName("delta.metric"), deltaCounter, tags)
  49. deltaCounter.Inc(10)
  50. fmt.Println("Search wavefront: ts(\"some.prefix.foo.count\")")
  51. fmt.Println("Entering loop to simulate metrics flushing. Hit ctrl+c to cancel")
  52. for {
  53. counter.Inc(rand.Int63())
  54. histogram.Update(rand.Int63())
  55. histogram2.Update(rand.Int63())
  56. deltaCounter.Inc(10)
  57. time.Sleep(time.Second * 10)
  58. }
  59. }

Golang Runtime Metrics

To enable golang runtime metrics reporting, set the RuntimeMetric flag in reporter to true:

  1. reporting.NewMetricsReporter(
  2. sender,
  3. reporting.ApplicationTag(application.New("app", "srv")),
  4. reporting.Source("go-metrics-test"),
  5. reporting.Prefix("some.prefix"),
  6. reporting.RuntimeMetric(true),
  7. )
  8. }