docker plugin: add container_id as label

This commit is contained in:
Ulric Qin 2022-06-07 12:17:52 +08:00
parent d7cb4d6318
commit 4042718a64
1 changed files with 13 additions and 15 deletions

View File

@ -309,6 +309,8 @@ func (ins *Instance) gatherContainerInspect(container types.Container, slist *li
} }
} }
tags["container_id"] = container.ID
statefields := make(map[string]interface{}) statefields := make(map[string]interface{})
finished, err := time.Parse(time.RFC3339, info.State.FinishedAt) finished, err := time.Parse(time.RFC3339, info.State.FinishedAt)
if err == nil && !finished.IsZero() { if err == nil && !finished.IsZero() {
@ -335,14 +337,12 @@ func (ins *Instance) gatherContainerInspect(container types.Container, slist *li
slist.PushFront(inputs.NewSample("docker_container_health_failing_streak", info.ContainerJSONBase.State.Health.FailingStreak, tags, ins.Labels)) slist.PushFront(inputs.NewSample("docker_container_health_failing_streak", info.ContainerJSONBase.State.Health.FailingStreak, tags, ins.Labels))
} }
ins.parseContainerStats(v, slist, tags, container.ID, daemonOSType) ins.parseContainerStats(v, slist, tags, daemonOSType)
return nil return nil
} }
func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.SafeList, tags map[string]string, id, ostype string) { func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.SafeList, tags map[string]string, ostype string) {
idtags := map[string]string{"container_id": id}
// memory // memory
// memstats := []string{ // memstats := []string{
@ -403,7 +403,7 @@ func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.Safe
memfields["docker_container_mem_private_working_set"] = stat.MemoryStats.PrivateWorkingSet memfields["docker_container_mem_private_working_set"] = stat.MemoryStats.PrivateWorkingSet
} }
inputs.PushSamples(slist, memfields, idtags, tags, ins.Labels) inputs.PushSamples(slist, memfields, tags, ins.Labels)
// cpu // cpu
@ -428,7 +428,7 @@ func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.Safe
cpufields["docker_container_cpu_usage_percent"] = cpuPercent cpufields["docker_container_cpu_usage_percent"] = cpuPercent
} }
inputs.PushSamples(slist, cpufields, map[string]string{"cpu": "cpu-total"}, idtags, tags, ins.Labels) inputs.PushSamples(slist, cpufields, map[string]string{"cpu": "cpu-total"}, tags, ins.Labels)
} }
if choice.Contains("cpu", ins.PerDeviceInclude) && len(stat.CPUStats.CPUUsage.PercpuUsage) > 0 { if choice.Contains("cpu", ins.PerDeviceInclude) && len(stat.CPUStats.CPUUsage.PercpuUsage) > 0 {
@ -444,7 +444,6 @@ func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.Safe
"docker_container_cpu_usage_total", "docker_container_cpu_usage_total",
percpu, percpu,
map[string]string{"cpu": fmt.Sprintf("cpu%d", i)}, map[string]string{"cpu": fmt.Sprintf("cpu%d", i)},
idtags,
tags, tags,
ins.Labels, ins.Labels,
)) ))
@ -467,7 +466,7 @@ func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.Safe
} }
if choice.Contains("network", ins.PerDeviceInclude) { if choice.Contains("network", ins.PerDeviceInclude) {
inputs.PushSamples(slist, netfields, map[string]string{"network": network}, idtags, tags, ins.Labels) inputs.PushSamples(slist, netfields, map[string]string{"network": network}, tags, ins.Labels)
} }
if choice.Contains("network", ins.TotalInclude) { if choice.Contains("network", ins.TotalInclude) {
@ -494,15 +493,13 @@ func (ins *Instance) parseContainerStats(stat *types.StatsJSON, slist *list.Safe
// totalNetworkStatMap could be empty if container is running with --net=host. // totalNetworkStatMap could be empty if container is running with --net=host.
if choice.Contains("network", ins.TotalInclude) && len(totalNetworkStatMap) != 0 { if choice.Contains("network", ins.TotalInclude) && len(totalNetworkStatMap) != 0 {
inputs.PushSamples(slist, totalNetworkStatMap, map[string]string{"network": "total"}, idtags, tags, ins.Labels) inputs.PushSamples(slist, totalNetworkStatMap, map[string]string{"network": "total"}, tags, ins.Labels)
} }
ins.gatherBlockIOMetrics(slist, stat, tags, id) ins.gatherBlockIOMetrics(slist, stat, tags)
} }
func (ins *Instance) gatherBlockIOMetrics(slist *list.SafeList, stat *types.StatsJSON, tags map[string]string, id string) { func (ins *Instance) gatherBlockIOMetrics(slist *list.SafeList, stat *types.StatsJSON, tags map[string]string) {
idtags := map[string]string{"container_id": id}
perDeviceBlkio := choice.Contains("blkio", ins.PerDeviceInclude) perDeviceBlkio := choice.Contains("blkio", ins.PerDeviceInclude)
totalBlkio := choice.Contains("blkio", ins.TotalInclude) totalBlkio := choice.Contains("blkio", ins.TotalInclude)
@ -512,7 +509,7 @@ func (ins *Instance) gatherBlockIOMetrics(slist *list.SafeList, stat *types.Stat
totalStatMap := make(map[string]interface{}) totalStatMap := make(map[string]interface{})
for device, fields := range deviceStatMap { for device, fields := range deviceStatMap {
if perDeviceBlkio { if perDeviceBlkio {
inputs.PushSamples(slist, fields, map[string]string{"device": device}, idtags, tags, ins.Labels) inputs.PushSamples(slist, fields, map[string]string{"device": device}, tags, ins.Labels)
} }
if totalBlkio { if totalBlkio {
for field, value := range fields { for field, value := range fields {
@ -537,7 +534,7 @@ func (ins *Instance) gatherBlockIOMetrics(slist *list.SafeList, stat *types.Stat
} }
if totalBlkio { if totalBlkio {
inputs.PushSamples(slist, totalStatMap, map[string]string{"device": "total"}, idtags, tags, ins.Labels) inputs.PushSamples(slist, totalStatMap, map[string]string{"device": "total"}, tags, ins.Labels)
} }
} }
@ -695,6 +692,7 @@ func (ins *Instance) gatherInfo(slist *list.SafeList) {
"docker_n_containers_stopped": info.ContainersStopped, "docker_n_containers_stopped": info.ContainersStopped,
"docker_n_containers_paused": info.ContainersPaused, "docker_n_containers_paused": info.ContainersPaused,
"docker_n_images": info.Images, "docker_n_images": info.Images,
"docker_memory_total": info.MemTotal,
} }
inputs.PushSamples(slist, fields, ins.Labels) inputs.PushSamples(slist, fields, ins.Labels)