// WavefrontEmitter creates a new direct sender to Wavefront and returns a handlerfunc
WavefrontEmitter creates a new direct sender to Wavefront and returns a handlerfunc
// WavefrontEmitter creates a new direct sender to Wavefront and returns a handlerfunc
func WavefrontEmitter(w *WavefrontConfig) (gin.HandlerFunc, error) {
	dc := &wavefront.DirectConfiguration{
		Server:               w.Server,
		Token:                w.Token,
		BatchSize:            w.BatchSize,
		MaxBufferSize:        w.MaxBufferSize,
		FlushIntervalSeconds: w.FlushInterval,
	}
	sender, err := wavefront.NewDirectSender(dc)
	if err != nil {
		return nil, fmt.Errorf(ErrCreateSender, err.Error())
	}
	if w.PointTags == nil {
		w.PointTags = make(map[string]string)
	}
	return func(c *gin.Context) {
		start := time.Now()
		c.Next()
		end := time.Now()
		latency := end.Sub(start)
		statusCode := c.Writer.Status()
		bytesOut := c.Writer.Size()
		bytesIn := c.Request.ContentLength
		w.PointTags["path"] = c.Request.URL.Path
		w.PointTags["clientIP"] = c.ClientIP()
		w.PointTags["method"] = c.Request.Method
		w.PointTags["userAgent"] = c.Request.UserAgent()
		sender.SendMetric(strings.Join([]string{w.MetricPrefix, ".latency"}, ""), float64(latency.Milliseconds()), end.Unix(), w.Source, w.PointTags)
		sender.SendMetric(strings.Join([]string{w.MetricPrefix, ".bytes.in"}, ""), float64(bytesIn), end.Unix(), w.Source, w.PointTags)
		sender.SendMetric(strings.Join([]string{w.MetricPrefix, ".bytes.out"}, ""), float64(bytesOut), end.Unix(), w.Source, w.PointTags)
		switch {
		case statusCode > 199 && statusCode < 300:
			sender.SendDeltaCounter(strings.Join([]string{w.MetricPrefix, ".status.success"}, ""), 1, w.Source, w.PointTags)
		case statusCode > 299 && statusCode < 400:
			sender.SendDeltaCounter(strings.Join([]string{w.MetricPrefix, ".status.redirection"}, ""), 1, w.Source, w.PointTags)
		case statusCode > 399 && statusCode < 500:
			sender.SendDeltaCounter(strings.Join([]string{w.MetricPrefix, ".status.error.client"}, ""), 1, w.Source, w.PointTags)
		case statusCode > 499 && statusCode < 600:
			sender.SendDeltaCounter(strings.Join([]string{w.MetricPrefix, ".status.error.server"}, ""), 1, w.Source, w.PointTags)
		}
	}, nil
}
func WavefrontEmitter ( w * WavefrontConfig ) ( gin . HandlerFunc , error ) { dc := & wavefront . DirectConfiguration { Server : w . Server , Token : w . Token , BatchSize : w . BatchSize , MaxBufferSize : w . MaxBufferSize , FlushIntervalSeconds : w . FlushInterval , } sender , err := wavefront . NewDirectSender ( dc ) if err != nil { return nil , fmt . Errorf ( ErrCreateSender , err . Error ( ) ) } if w . PointTags == nil { w . PointTags = make ( map [ string ] string ) } return func ( c * gin . Context ) { start := time . Now ( ) c . Next ( ) end := time . Now ( ) latency := end . Sub ( start ) statusCode := c . Writer . Status ( ) bytesOut := c . Writer . Size ( ) bytesIn := c . Request . ContentLength w . PointTags [ " " ] = c . Request . URL . Path w . PointTags [ " " ] = c . ClientIP ( ) w . PointTags [ " " ] = c . Request . Method w . PointTags [ " " ] = c . Request . UserAgent ( ) sender . SendMetric ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , float64 ( latency . Milliseconds ( ) ) , end . Unix ( ) , w . Source , w . PointTags ) sender . SendMetric ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , float64 ( bytesIn ) , end . Unix ( ) , w . Source , w . PointTags ) sender . SendMetric ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , float64 ( bytesOut ) , end . Unix ( ) , w . Source , w . PointTags ) switch { case statusCode > 199 && statusCode < 300 : sender . SendDeltaCounter ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , 1 , w . Source , w . PointTags ) case statusCode > 299 && statusCode < 400 : sender . SendDeltaCounter ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , 1 , w . Source , w . PointTags ) case statusCode > 399 && statusCode < 500 : sender . SendDeltaCounter ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , 1 , w . Source , w . PointTags ) case statusCode > 499 && statusCode < 600 : sender . SendDeltaCounter ( strings . Join ( [ ] string { w . MetricPrefix , " " } , " " ) , 1 , w . Source , w . PointTags ) } } , nil }
WavefrontEmitter creates a new direct sender to Wavefront and returns a handlerfunc
WavefrontEmitter creates a new direct sender to Wavefront and returns a handlerfunc
// Start timer // Process request // Stop timer // Add tags // Send metrics // <metricName> <metricValue> [<timestamp>] source=<source> [pointTags]