Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ First 6 bytes of the Docker container id.

Container id represented in the same format as in metrics of k8s pods - prefixed with `docker://`. This enables easy joins in Prometheus to kube_pod_container_info metric.

### `container_name`

If `io.kubernetes.container.name` label is set on the container, it's value
will be set as the `container_name` label in the metric

### `name`

Name of the container.
Expand All @@ -124,6 +129,7 @@ Image id represented in the same format as in metrics of k8s pods - prefixed wit
If `io.kubernetes.pod.name` label is set on the container, it's value
will be set as the `pod` label in the metric


### `namespace`

If `io.kubernetes.pod.namespace` label is set on the container, it's value
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "3.9"
services:
web:
build: .
21 changes: 12 additions & 9 deletions event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
)

type container struct {
id string
name string
imageID string
pod string
namespace string
id string
name string
imageID string
pod string
namespace string
container_name string
}

func (c *container) labels() prometheus.Labels {
Expand All @@ -28,6 +29,7 @@ func (c *container) labels() prometheus.Labels {
"image_id": fmt.Sprintf("docker-pullable://%s", c.imageID),
"pod": c.pod,
"namespace": c.namespace,
"container_name": c.container_name,
}
}

Expand Down Expand Up @@ -58,14 +60,14 @@ func (c *container) destroy() {
type eventHandler struct {
containers map[string]*container
mu *sync.Mutex
getContainerPodAndNamespace func(string) (string, string)
getContainerInfo func(string) (string, string, string)
}

func newEventHandler(getContainerPodAndNamespace func(string) (string, string)) *eventHandler {
func newEventHandler(getContainerInfo func(string) (string, string, string)) *eventHandler {
return &eventHandler{
containers: map[string]*container{},
mu: &sync.Mutex{},
getContainerPodAndNamespace: getContainerPodAndNamespace,
getContainerInfo: getContainerInfo,
}
}

Expand All @@ -81,14 +83,15 @@ func (eh *eventHandler) addContainer(id, name, imageID string) *container {
return cnt
}

pod, namespace := eh.getContainerPodAndNamespace(id)
pod, namespace, container_name := eh.getContainerInfo(id)

c := &container{
id: id,
name: name,
imageID: imageID,
pod: pod,
namespace: namespace,
container_name: container_name,
}

c.create()
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ func main() {
return errors.Wrap(err, "while listing containers")
}

h := newEventHandler(func(containerID string) (pod string, namespace string) {
h := newEventHandler(func(containerID string) (pod string, namespace string, container_name: string) {
res, err := dc.ContainerInspect(context.Background(), containerID)
if err != nil {
return "", ""
}

pod = res.Config.Labels["io.kubernetes.pod.name"]
namespace = res.Config.Labels["io.kubernetes.pod.namespace"]
return pod, namespace
container_name = res.Config.Labels["io.kubernetes.container.name"]
return pod, namespace, container_name
})
for _, c := range containers {
ci, err := dc.ContainerInspect(ctx, c.ID)
Expand Down
1 change: 1 addition & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/prometheus/client_golang/prometheus"

var labelNames = []string{
"container_id",
"container_name",
"container_short_id",
"docker_container_id",
"name",
Expand Down