github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/metric/emitter/influxdb_test.go (about)

     1  package emitter_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/lager"
     7  	"github.com/pf-qiu/concourse/v6/atc/metric"
     8  	"github.com/pf-qiu/concourse/v6/atc/metric/emitter"
     9  	"github.com/pf-qiu/concourse/v6/atc/metric/emitter/emitterfakes"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  func batchPointAt(influxDBClient *emitterfakes.FakeInfluxDBClient, index int) string {
    16  	if influxDBClient.WriteCallCount() == 1 {
    17  		points := influxDBClient.WriteArgsForCall(0).Points()
    18  		return (*(points[index])).String()
    19  	}
    20  	return ""
    21  }
    22  
    23  var _ = Describe("InfluxDBEmitter", func() {
    24  	var (
    25  		influxDBEmitter *emitter.InfluxDBEmitter
    26  		influxDBClient  *emitterfakes.FakeInfluxDBClient
    27  		testLogger      lager.Logger
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		testLogger = lager.NewLogger("test")
    32  
    33  		influxDBClient = &emitterfakes.FakeInfluxDBClient{}
    34  	})
    35  
    36  	Context("Emit", func() {
    37  		Context("with batch size 2", func() {
    38  			BeforeEach(func() {
    39  				influxDBEmitter = &emitter.InfluxDBEmitter{
    40  					Client:        influxDBClient,
    41  					BatchSize:     2,
    42  					BatchDuration: 300 * time.Second,
    43  				}
    44  			})
    45  
    46  			AfterEach(func() {
    47  				influxDBEmitter.SubmitBatch(testLogger)
    48  			})
    49  
    50  			It("should write no batches to InfluxDB", func() {
    51  				influxDBEmitter.Emit(testLogger, metric.Event{})
    52  				Eventually(influxDBClient.WriteCallCount).Should(BeNumerically("==", 0))
    53  			})
    54  
    55  			It("should write 1 batch to InfluxDB", func() {
    56  				for i := 0; i < 3; i++ {
    57  					influxDBEmitter.Emit(testLogger, metric.Event{})
    58  				}
    59  				Eventually(influxDBClient.WriteCallCount).Should(BeNumerically("==", 1))
    60  			})
    61  
    62  			It("should write 2 batches to InfluxDB", func() {
    63  				for i := 0; i < 4; i++ {
    64  					influxDBEmitter.Emit(testLogger, metric.Event{})
    65  				}
    66  				Eventually(influxDBClient.WriteCallCount).Should(BeNumerically("==", 2))
    67  			})
    68  
    69  			It("should populate the batch points", func() {
    70  				influxDBEmitter.Emit(testLogger, metric.Event{
    71  					Name:  "build started",
    72  					Value: 123,
    73  					Attributes: map[string]string{
    74  						"pipeline":   "test1",
    75  						"job":        "job1",
    76  						"build_name": "build1",
    77  						"build_id":   "123",
    78  						"team_name":  "team1",
    79  					},
    80  					Host: "localhost",
    81  				})
    82  				influxDBEmitter.Emit(testLogger, metric.Event{
    83  					Name:  "build finished",
    84  					Value: 100,
    85  					Attributes: map[string]string{
    86  						"pipeline":     "test2",
    87  						"job":          "job2",
    88  						"build_name":   "build2",
    89  						"build_id":     "456",
    90  						"build_status": "succeeded",
    91  						"team_name":    "team2",
    92  					},
    93  					Host: "localhost",
    94  				})
    95  
    96  				Eventually(func() string {
    97  					return batchPointAt(influxDBClient, 0)
    98  				}).Should(Equal(`build\ started,build_id=123,build_name=build1,host=localhost,job=job1,pipeline=test1,team_name=team1 value=123`))
    99  
   100  				Eventually(func() string {
   101  					return batchPointAt(influxDBClient, 1)
   102  				}).Should(Equal(`build\ finished,build_id=456,build_name=build2,build_status=succeeded,host=localhost,job=job2,pipeline=test2,team_name=team2 value=100`))
   103  			})
   104  		})
   105  
   106  		Context("with batch duration 1 nanosecond", func() {
   107  			BeforeEach(func() {
   108  				influxDBEmitter = &emitter.InfluxDBEmitter{
   109  					Client:        influxDBClient,
   110  					BatchSize:     5000,
   111  					BatchDuration: 1 * time.Nanosecond,
   112  				}
   113  			})
   114  
   115  			AfterEach(func() {
   116  				influxDBEmitter.SubmitBatch(testLogger)
   117  			})
   118  
   119  			It("should write no batches to InfluxDB", func() {
   120  				Eventually(influxDBClient.WriteCallCount).Should(BeNumerically("==", 0))
   121  			})
   122  
   123  			It("should write 1 batch to InfluxDB", func() {
   124  				influxDBEmitter.Emit(testLogger, metric.Event{})
   125  				time.Sleep(2 * time.Nanosecond)
   126  				Eventually(influxDBClient.WriteCallCount).Should(BeNumerically("==", 1))
   127  			})
   128  
   129  			It("should write 2 batches to InfluxDB", func() {
   130  				for i := 0; i < 2; i++ {
   131  					influxDBEmitter.Emit(testLogger, metric.Event{})
   132  					time.Sleep(2 * time.Nanosecond)
   133  				}
   134  				Eventually(influxDBClient.WriteCallCount).Should(BeNumerically("==", 2))
   135  			})
   136  		})
   137  	})
   138  })