Skip to content

Commit 31a09b0

Browse files
geigerj0geofffranks
authored andcommitted
send network metric in dedicated batches, only send metrics when they are set
1 parent fe68cee commit 31a09b0

File tree

2 files changed

+148
-86
lines changed

2 files changed

+148
-86
lines changed

client.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type ContainerMetric struct {
3939
AbsoluteCPUUsage uint64
4040
AbsoluteCPUEntitlement uint64
4141
ContainerAge uint64
42-
RxBytes uint64
43-
TxBytes uint64
42+
RxBytes *uint64
43+
TxBytes *uint64
4444
Tags map[string]string
4545
}
4646

@@ -225,14 +225,12 @@ func (c client) SendAppMetrics(m ContainerMetric) error {
225225
loggregator.WithGaugeValue("disk", float64(m.DiskBytes), "bytes"),
226226
loggregator.WithGaugeValue("memory_quota", float64(m.MemoryBytesQuota), "bytes"),
227227
loggregator.WithGaugeValue("disk_quota", float64(m.DiskBytesQuota), "bytes"),
228-
loggregator.WithGaugeValue("rx_bytes", float64(m.RxBytes), "bytes"),
229-
loggregator.WithGaugeValue("tx_bytes", float64(m.TxBytes), "bytes"),
230228
loggregator.WithEnvelopeTags(m.Tags),
231229
)
232230

233-
// Emit the new metrics in a separate envelope. Loggregator will convert a
231+
// Emit the new metrics in a separate envelope. Loggregator will convert a
234232
// gauge envelope with cpu, memory, disk, etc. to a container metric
235-
// envelope and ignore the rest of the fields. Emitting absolute_usage,
233+
// envelope and ignore the rest of the fields. Emitting absolute_usage,
236234
// absolute_entitlement & container_age in a separate envelope allows v1
237235
// subscribers (cf nozzle) to be able to see those fields. Note,
238236
// Loggregator will emit each value in a separate envelope for v1
@@ -245,6 +243,14 @@ func (c client) SendAppMetrics(m ContainerMetric) error {
245243
loggregator.WithEnvelopeTags(m.Tags),
246244
)
247245

246+
if m.RxBytes != nil {
247+
c.client.EmitCounter("rx_bytes", loggregator.WithCounterSourceInfo(m.Tags["source_id"], m.Tags["instance_id"]), loggregator.WithTotal(*m.RxBytes))
248+
}
249+
250+
if m.TxBytes != nil {
251+
c.client.EmitCounter("tx_bytes", loggregator.WithCounterSourceInfo(m.Tags["source_id"], m.Tags["instance_id"]), loggregator.WithTotal(*m.TxBytes))
252+
}
253+
248254
return nil
249255
}
250256

client_test.go

Lines changed: 136 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -270,89 +270,145 @@ var _ = Describe("DiegoLoggingClient", func() {
270270
Describe("SendAppMetrics", func() {
271271
var batch *loggregator_v2.EnvelopeBatch
272272

273-
JustBeforeEach(func() {
274-
metrics := client.ContainerMetric{
275-
MemoryBytes: 50,
276-
MemoryBytesQuota: 100,
277-
278-
DiskBytes: 100,
279-
DiskBytesQuota: 200,
280-
281-
CpuPercentage: 50.0,
282-
AbsoluteCPUUsage: 1,
283-
AbsoluteCPUEntitlement: 2,
284-
ContainerAge: 3,
285-
286-
RxBytes: 42,
287-
TxBytes: 43,
288-
289-
Tags: map[string]string{
273+
Context("all container metrics are available", func() {
274+
JustBeforeEach(func() {
275+
rxBytes := uint64(42)
276+
txBytes := uint64(43)
277+
278+
metrics := client.ContainerMetric{
279+
MemoryBytes: 50,
280+
MemoryBytesQuota: 100,
281+
282+
DiskBytes: 100,
283+
DiskBytesQuota: 200,
284+
285+
CpuPercentage: 50.0,
286+
AbsoluteCPUUsage: 1,
287+
AbsoluteCPUEntitlement: 2,
288+
ContainerAge: 3,
289+
290+
RxBytes: &rxBytes,
291+
TxBytes: &txBytes,
292+
293+
Tags: map[string]string{
294+
"source_id": "some-source-id",
295+
"instance_id": "345",
296+
"some-key": "some-value",
297+
},
298+
}
299+
300+
Expect(c.SendAppMetrics(metrics)).To(Succeed())
301+
batch = getEnvelopeBatch()
302+
})
303+
304+
It("sets app info on all batches", func() {
305+
expectedSourceID := "some-source-id"
306+
expectedInstanceID := "345"
307+
308+
Expect(batch.Batch).To(HaveLen(1))
309+
Expect(batch.Batch[0].GetSourceId()).To(Equal(expectedSourceID))
310+
Expect(batch.Batch[0].GetInstanceId()).To(Equal(expectedInstanceID))
311+
312+
batch = getEnvelopeBatch()
313+
Expect(batch.Batch).To(HaveLen(1))
314+
Expect(batch.Batch[0].GetSourceId()).To(Equal(expectedSourceID))
315+
Expect(batch.Batch[0].GetInstanceId()).To(Equal(expectedInstanceID))
316+
317+
batch = getEnvelopeBatch()
318+
Expect(batch.Batch).To(HaveLen(1))
319+
Expect(batch.Batch[0].GetSourceId()).To(Equal(expectedSourceID))
320+
Expect(batch.Batch[0].GetInstanceId()).To(Equal(expectedInstanceID))
321+
322+
batch = getEnvelopeBatch()
323+
Expect(batch.Batch).To(HaveLen(1))
324+
Expect(batch.Batch[0].GetSourceId()).To(Equal(expectedSourceID))
325+
Expect(batch.Batch[0].GetInstanceId()).To(Equal(expectedInstanceID))
326+
})
327+
328+
It("sends memory usage and quota", func() {
329+
metrics := batch.Batch[0].GetGauge().GetMetrics()
330+
Expect(metrics["memory"].GetValue()).To(Equal(float64(50)))
331+
Expect(metrics["memory"].GetUnit()).To(Equal("bytes"))
332+
333+
Expect(metrics["memory_quota"].GetValue()).To(Equal(float64(100)))
334+
Expect(metrics["memory_quota"].GetUnit()).To(Equal("bytes"))
335+
})
336+
337+
It("sends disk usage and quota", func() {
338+
metrics := batch.Batch[0].GetGauge().GetMetrics()
339+
Expect(metrics["disk"].GetValue()).To(Equal(float64(100)))
340+
Expect(metrics["disk"].GetUnit()).To(Equal("bytes"))
341+
342+
Expect(metrics["disk_quota"].GetValue()).To(Equal(float64(200)))
343+
Expect(metrics["disk_quota"].GetUnit()).To(Equal("bytes"))
344+
})
345+
346+
It("sends cpu usage in a separate batch", func() {
347+
batch = getEnvelopeBatch()
348+
349+
metrics := batch.Batch[0].GetGauge().GetMetrics()
350+
351+
Expect(metrics["absolute_usage"].GetValue()).To(Equal(float64(1)))
352+
Expect(metrics["absolute_usage"].GetUnit()).To(Equal("nanoseconds"))
353+
354+
Expect(metrics["absolute_entitlement"].GetValue()).To(Equal(float64(2)))
355+
Expect(metrics["absolute_entitlement"].GetUnit()).To(Equal("nanoseconds"))
356+
357+
Expect(metrics["container_age"].GetValue()).To(Equal(float64(3)))
358+
Expect(metrics["container_age"].GetUnit()).To(Equal("nanoseconds"))
359+
})
360+
361+
It("sends network traffic usage in a separate batches", func() {
362+
batch = getEnvelopeBatch() // cpu usage batch
363+
364+
batch = getEnvelopeBatch() // network traffic usage batch received bytes
365+
counter := batch.Batch[0].GetCounter()
366+
Expect(counter.Name).To(Equal("rx_bytes"))
367+
Expect(counter.Total).To(Equal(uint64(42)))
368+
369+
batch = getEnvelopeBatch() // network traffic usage batch transmitted bytes
370+
counter = batch.Batch[0].GetCounter()
371+
Expect(counter.Name).To(Equal("tx_bytes"))
372+
Expect(counter.Total).To(Equal(uint64(43)))
373+
})
374+
375+
It("sends tags", func() {
376+
Expect(batch.Batch).To(HaveLen(1))
377+
Expect(batch.Batch[0].GetTags()).To(Equal(map[string]string{
378+
"origin": "some-origin",
290379
"source_id": "some-source-id",
291380
"instance_id": "345",
292381
"some-key": "some-value",
293-
},
294-
}
295-
296-
Expect(c.SendAppMetrics(metrics)).To(Succeed())
297-
batch = getEnvelopeBatch()
298-
})
299-
300-
It("sets app info", func() {
301-
Expect(batch.Batch).To(HaveLen(1))
302-
Expect(batch.Batch[0].GetSourceId()).To(Equal("some-source-id"))
303-
Expect(batch.Batch[0].GetInstanceId()).To(Equal("345"))
304-
})
305-
306-
It("sends memory usage and quota", func() {
307-
metrics := batch.Batch[0].GetGauge().GetMetrics()
308-
Expect(metrics["memory"].GetValue()).To(Equal(float64(50)))
309-
Expect(metrics["memory"].GetUnit()).To(Equal("bytes"))
310-
311-
Expect(metrics["memory_quota"].GetValue()).To(Equal(float64(100)))
312-
Expect(metrics["memory_quota"].GetUnit()).To(Equal("bytes"))
313-
})
314-
315-
It("sends disk usage and quota", func() {
316-
metrics := batch.Batch[0].GetGauge().GetMetrics()
317-
Expect(metrics["disk"].GetValue()).To(Equal(float64(100)))
318-
Expect(metrics["disk"].GetUnit()).To(Equal("bytes"))
319-
320-
Expect(metrics["disk_quota"].GetValue()).To(Equal(float64(200)))
321-
Expect(metrics["disk_quota"].GetUnit()).To(Equal("bytes"))
322-
})
323-
324-
It("sends cpu usage in a separate batch", func() {
325-
batch = getEnvelopeBatch()
326-
327-
metrics := batch.Batch[0].GetGauge().GetMetrics()
328-
329-
Expect(metrics["absolute_usage"].GetValue()).To(Equal(float64(1)))
330-
Expect(metrics["absolute_usage"].GetUnit()).To(Equal("nanoseconds"))
331-
332-
Expect(metrics["absolute_entitlement"].GetValue()).To(Equal(float64(2)))
333-
Expect(metrics["absolute_entitlement"].GetUnit()).To(Equal("nanoseconds"))
334-
335-
Expect(metrics["container_age"].GetValue()).To(Equal(float64(3)))
336-
Expect(metrics["container_age"].GetUnit()).To(Equal("nanoseconds"))
337-
})
338-
339-
It("sends network traffic usage", func() {
340-
metrics := batch.Batch[0].GetGauge().GetMetrics()
341-
Expect(metrics["rx_bytes"].GetValue()).To(Equal(float64(42)))
342-
Expect(metrics["rx_bytes"].GetUnit()).To(Equal("bytes"))
343-
344-
Expect(metrics["tx_bytes"].GetValue()).To(Equal(float64(43)))
345-
Expect(metrics["tx_bytes"].GetUnit()).To(Equal("bytes"))
346-
})
347-
348-
It("sends tags", func() {
349-
Expect(batch.Batch).To(HaveLen(1))
350-
Expect(batch.Batch[0].GetTags()).To(Equal(map[string]string{
351-
"origin": "some-origin",
352-
"source_id": "some-source-id",
353-
"instance_id": "345",
354-
"some-key": "some-value",
355-
}))
382+
}))
383+
})
384+
})
385+
386+
Context("network traffic usage is nil", func() {
387+
JustBeforeEach(func() {
388+
metrics := client.ContainerMetric{
389+
RxBytes: nil,
390+
TxBytes: nil,
391+
}
392+
393+
Expect(c.SendAppMetrics(metrics)).To(Succeed())
394+
395+
// receive all envelopes before the ones for network traffic usage
396+
batch = getEnvelopeBatch()
397+
batch = getEnvelopeBatch()
398+
})
399+
400+
It("does not send network traffic usage", func() {
401+
// start waiting for a new envelope to be received
402+
var firstEnvelopeOfNetworkTrafficUsage *loggregator_v2.EnvelopeBatch
403+
go func() {
404+
firstEnvelopeOfNetworkTrafficUsage, _ = sender.Recv()
405+
}()
406+
407+
// expect that there is no new envelope since there network traffic usage is nil
408+
Eventually(func() *loggregator_v2.EnvelopeBatch {
409+
return firstEnvelopeOfNetworkTrafficUsage
410+
}).WithPolling(20 * time.Millisecond).MustPassRepeatedly(5).Should(BeNil())
411+
})
356412
})
357413
})
358414

0 commit comments

Comments
 (0)