Skip to content

Commit

Permalink
set prefix based on owner property
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Feb 3, 2021
1 parent 4b66473 commit 7f50765
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions collector/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func containerClose(c hcsshim.Container) {
}

func (c *ContainerMetricsCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {

// Types Container is passed to get the containers compute systems only
containers, err := hcsshim.GetContainers(hcsshim.ComputeSystemQuery{Types: []string{"Container"}})
if err != nil {
Expand All @@ -168,70 +167,67 @@ func (c *ContainerMetricsCollector) collect(ch chan<- prometheus.Metric) (*prome
}

for _, containerDetails := range containers {
containerId := containerDetails.ID

container, err := hcsshim.OpenContainer(containerId)
container, err := hcsshim.OpenContainer(containerDetails.ID)
if container != nil {
defer containerClose(container)
}
if err != nil {
log.Error("err in opening container: ", containerId, err)
log.Error("err in opening container: ", containerDetails.ID, err)
continue
}

cstats, err := container.Statistics()
if err != nil {
log.Error("err in fetching container Statistics: ", containerId, err)
log.Error("err in fetching container Statistics: ", containerDetails.ID, err)
continue
}
// HCS V1 is for docker runtime. Add the docker:// prefix on container_id
containerId = "docker://" + containerId
containerIdWithPrefix := getContainerIdWithPrefix(containerDetails)

ch <- prometheus.MustNewConstMetric(
c.ContainerAvailable,
prometheus.CounterValue,
1,
containerId,
containerIdWithPrefix,
)
ch <- prometheus.MustNewConstMetric(
c.UsageCommitBytes,
prometheus.GaugeValue,
float64(cstats.Memory.UsageCommitBytes),
containerId,
containerIdWithPrefix,
)
ch <- prometheus.MustNewConstMetric(
c.UsageCommitPeakBytes,
prometheus.GaugeValue,
float64(cstats.Memory.UsageCommitPeakBytes),
containerId,
containerIdWithPrefix,
)
ch <- prometheus.MustNewConstMetric(
c.UsagePrivateWorkingSetBytes,
prometheus.GaugeValue,
float64(cstats.Memory.UsagePrivateWorkingSetBytes),
containerId,
containerIdWithPrefix,
)
ch <- prometheus.MustNewConstMetric(
c.RuntimeTotal,
prometheus.CounterValue,
float64(cstats.Processor.TotalRuntime100ns)*ticksToSecondsScaleFactor,
containerId,
containerIdWithPrefix,
)
ch <- prometheus.MustNewConstMetric(
c.RuntimeUser,
prometheus.CounterValue,
float64(cstats.Processor.RuntimeUser100ns)*ticksToSecondsScaleFactor,
containerId,
containerIdWithPrefix,
)
ch <- prometheus.MustNewConstMetric(
c.RuntimeKernel,
prometheus.CounterValue,
float64(cstats.Processor.RuntimeKernel100ns)*ticksToSecondsScaleFactor,
containerId,
containerIdWithPrefix,
)

if len(cstats.Network) == 0 {
log.Info("No Network Stats for container: ", containerId)
log.Info("No Network Stats for container: ", containerDetails.ID)
continue
}

Expand All @@ -242,41 +238,53 @@ func (c *ContainerMetricsCollector) collect(ch chan<- prometheus.Metric) (*prome
c.BytesReceived,
prometheus.CounterValue,
float64(networkInterface.BytesReceived),
containerId, networkInterface.EndpointId,
containerIdWithPrefix, networkInterface.EndpointId,
)
ch <- prometheus.MustNewConstMetric(
c.BytesSent,
prometheus.CounterValue,
float64(networkInterface.BytesSent),
containerId, networkInterface.EndpointId,
containerIdWithPrefix, networkInterface.EndpointId,
)
ch <- prometheus.MustNewConstMetric(
c.PacketsReceived,
prometheus.CounterValue,
float64(networkInterface.PacketsReceived),
containerId, networkInterface.EndpointId,
containerIdWithPrefix, networkInterface.EndpointId,
)
ch <- prometheus.MustNewConstMetric(
c.PacketsSent,
prometheus.CounterValue,
float64(networkInterface.PacketsSent),
containerId, networkInterface.EndpointId,
containerIdWithPrefix, networkInterface.EndpointId,
)
ch <- prometheus.MustNewConstMetric(
c.DroppedPacketsIncoming,
prometheus.CounterValue,
float64(networkInterface.DroppedPacketsIncoming),
containerId, networkInterface.EndpointId,
containerIdWithPrefix, networkInterface.EndpointId,
)
ch <- prometheus.MustNewConstMetric(
c.DroppedPacketsOutgoing,
prometheus.CounterValue,
float64(networkInterface.DroppedPacketsOutgoing),
containerId, networkInterface.EndpointId,
containerIdWithPrefix, networkInterface.EndpointId,
)
break
}
}

return nil, nil
}

func getContainerIdWithPrefix(containerDetails hcsshim.ContainerProperties) string {
switch containerDetails.Owner {
case "docker":
return "docker://" + containerDetails.ID
case "containerd-shim-runhcs-v1.exe":
return "containerd://" + containerDetails.ID
default:
// If not set fall back prior behavior
return "docker://" + containerDetails.ID
}
}

0 comments on commit 7f50765

Please sign in to comment.