|  | 
| 2 | 2 | package datadog | 
| 3 | 3 | 
 | 
| 4 | 4 | import ( | 
|  | 5 | +	"fmt" | 
| 5 | 6 | 	"strings" | 
| 6 | 7 | 	"sync" | 
| 7 | 8 | 	"time" | 
| @@ -78,10 +79,41 @@ func NewMetricRegistry(addr string, prefix string, pollFrequency time.Duration) | 
| 78 | 79 | 		return nil, err | 
| 79 | 80 | 	} | 
| 80 | 81 | 	return &MetricRegistry{ | 
| 81 |  | -		client:        client, | 
| 82 |  | -		prefix:        prefix, | 
| 83 |  | -		pollFrequency: pollFrequency, | 
| 84 |  | -		stopper:       make(chan bool, 1), | 
|  | 82 | +		client:              client, | 
|  | 83 | +		prefix:              prefix, | 
|  | 84 | +		pollFrequency:       pollFrequency, | 
|  | 85 | +		stopper:             make(chan bool, 1), | 
|  | 86 | +		registeredGauges:    make(map[string]*metricPoller, 0), | 
|  | 87 | +		registeredListeners: make(map[string]*metricSampleListener, 0), | 
|  | 88 | +	}, nil | 
|  | 89 | +} | 
|  | 90 | + | 
|  | 91 | +// NewMetricRegistryWithClient will create a new Datadog MetricRegistry with the provided client instead. | 
|  | 92 | +// This registry reports metrics to datadog using the datadog dogstatsd forwarding. | 
|  | 93 | +func NewMetricRegistryWithClient( | 
|  | 94 | +	client *dogstatsd.Client, | 
|  | 95 | +	prefix string, | 
|  | 96 | +	pollFrequency time.Duration, | 
|  | 97 | +) (*MetricRegistry, error) { | 
|  | 98 | +	if client == nil { | 
|  | 99 | +		return nil, fmt.Errorf("client is nil") | 
|  | 100 | +	} | 
|  | 101 | + | 
|  | 102 | +	if !strings.HasSuffix(prefix, ".") { | 
|  | 103 | +		prefix = prefix + "." | 
|  | 104 | +	} | 
|  | 105 | + | 
|  | 106 | +	if pollFrequency == 0 { | 
|  | 107 | +		pollFrequency = defaultPollFrequency | 
|  | 108 | +	} | 
|  | 109 | + | 
|  | 110 | +	return &MetricRegistry{ | 
|  | 111 | +		client:              client, | 
|  | 112 | +		prefix:              prefix, | 
|  | 113 | +		pollFrequency:       pollFrequency, | 
|  | 114 | +		stopper:             make(chan bool, 1), | 
|  | 115 | +		registeredGauges:    make(map[string]*metricPoller, 0), | 
|  | 116 | +		registeredListeners: make(map[string]*metricSampleListener, 0), | 
| 85 | 117 | 	}, nil | 
| 86 | 118 | } | 
| 87 | 119 | 
 | 
|  | 
0 commit comments