set prefix based on owner property

Signed-off-by: James Sturtevant <jstur@microsoft.com>
This commit is contained in:
James Sturtevant 2021-02-02 10:41:37 -08:00
parent 4b66473d2e
commit b615301efc
1 changed files with 28 additions and 21 deletions

View File

@ -168,70 +168,67 @@ func (c *ContainerMetricsCollector) collect(ch chan<- prometheus.Metric) (*prome
} }
for _, containerDetails := range containers { for _, containerDetails := range containers {
containerId := containerDetails.ID container, err := hcsshim.OpenContainer(containerDetails.ID)
container, err := hcsshim.OpenContainer(containerId)
if container != nil { if container != nil {
defer containerClose(container) defer containerClose(container)
} }
if err != nil { if err != nil {
log.Error("err in opening container: ", containerId, err) log.Error("err in opening container: ", containerDetails.ID, err)
continue continue
} }
cstats, err := container.Statistics() cstats, err := container.Statistics()
if err != nil { if err != nil {
log.Error("err in fetching container Statistics: ", containerId, err) log.Error("err in fetching container Statistics: ", containerDetails.ID, err)
continue continue
} }
// HCS V1 is for docker runtime. Add the docker:// prefix on container_id containerIdWithPrefix := getContainerIdWithPrefix(containerDetails)
containerId = "docker://" + containerId
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.ContainerAvailable, c.ContainerAvailable,
prometheus.CounterValue, prometheus.CounterValue,
1, 1,
containerId, containerIdWithPrefix,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.UsageCommitBytes, c.UsageCommitBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(cstats.Memory.UsageCommitBytes), float64(cstats.Memory.UsageCommitBytes),
containerId, containerIdWithPrefix,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.UsageCommitPeakBytes, c.UsageCommitPeakBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(cstats.Memory.UsageCommitPeakBytes), float64(cstats.Memory.UsageCommitPeakBytes),
containerId, containerIdWithPrefix,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.UsagePrivateWorkingSetBytes, c.UsagePrivateWorkingSetBytes,
prometheus.GaugeValue, prometheus.GaugeValue,
float64(cstats.Memory.UsagePrivateWorkingSetBytes), float64(cstats.Memory.UsagePrivateWorkingSetBytes),
containerId, containerIdWithPrefix,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.RuntimeTotal, c.RuntimeTotal,
prometheus.CounterValue, prometheus.CounterValue,
float64(cstats.Processor.TotalRuntime100ns)*ticksToSecondsScaleFactor, float64(cstats.Processor.TotalRuntime100ns)*ticksToSecondsScaleFactor,
containerId, containerIdWithPrefix,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.RuntimeUser, c.RuntimeUser,
prometheus.CounterValue, prometheus.CounterValue,
float64(cstats.Processor.RuntimeUser100ns)*ticksToSecondsScaleFactor, float64(cstats.Processor.RuntimeUser100ns)*ticksToSecondsScaleFactor,
containerId, containerIdWithPrefix,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.RuntimeKernel, c.RuntimeKernel,
prometheus.CounterValue, prometheus.CounterValue,
float64(cstats.Processor.RuntimeKernel100ns)*ticksToSecondsScaleFactor, float64(cstats.Processor.RuntimeKernel100ns)*ticksToSecondsScaleFactor,
containerId, containerIdWithPrefix,
) )
if len(cstats.Network) == 0 { if len(cstats.Network) == 0 {
log.Info("No Network Stats for container: ", containerId) log.Info("No Network Stats for container: ", containerDetails.ID)
continue continue
} }
@ -242,37 +239,37 @@ func (c *ContainerMetricsCollector) collect(ch chan<- prometheus.Metric) (*prome
c.BytesReceived, c.BytesReceived,
prometheus.CounterValue, prometheus.CounterValue,
float64(networkInterface.BytesReceived), float64(networkInterface.BytesReceived),
containerId, networkInterface.EndpointId, containerIdWithPrefix, networkInterface.EndpointId,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.BytesSent, c.BytesSent,
prometheus.CounterValue, prometheus.CounterValue,
float64(networkInterface.BytesSent), float64(networkInterface.BytesSent),
containerId, networkInterface.EndpointId, containerIdWithPrefix, networkInterface.EndpointId,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PacketsReceived, c.PacketsReceived,
prometheus.CounterValue, prometheus.CounterValue,
float64(networkInterface.PacketsReceived), float64(networkInterface.PacketsReceived),
containerId, networkInterface.EndpointId, containerIdWithPrefix, networkInterface.EndpointId,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.PacketsSent, c.PacketsSent,
prometheus.CounterValue, prometheus.CounterValue,
float64(networkInterface.PacketsSent), float64(networkInterface.PacketsSent),
containerId, networkInterface.EndpointId, containerIdWithPrefix, networkInterface.EndpointId,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.DroppedPacketsIncoming, c.DroppedPacketsIncoming,
prometheus.CounterValue, prometheus.CounterValue,
float64(networkInterface.DroppedPacketsIncoming), float64(networkInterface.DroppedPacketsIncoming),
containerId, networkInterface.EndpointId, containerIdWithPrefix, networkInterface.EndpointId,
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.DroppedPacketsOutgoing, c.DroppedPacketsOutgoing,
prometheus.CounterValue, prometheus.CounterValue,
float64(networkInterface.DroppedPacketsOutgoing), float64(networkInterface.DroppedPacketsOutgoing),
containerId, networkInterface.EndpointId, containerIdWithPrefix, networkInterface.EndpointId,
) )
break break
} }
@ -280,3 +277,13 @@ func (c *ContainerMetricsCollector) collect(ch chan<- prometheus.Metric) (*prome
return nil, nil return nil, nil
} }
func getContainerIdWithPrefix(containerDetails hcsshim.ContainerProperties) string {
switch containerDetails.Owner {
case "containerd-shim-runhcs-v1.exe":
return "containerd://" + containerDetails.ID
default:
// default to docker or if owner is not set
return "docker://" + containerDetails.ID
}
}