diff --git a/README.md b/README.md index 41bb482..533749a 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..546e29e --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,4 @@ +version: "3.9" +services: + web: + build: . \ No newline at end of file diff --git a/event_handler.go b/event_handler.go index 301c4fd..37aeaff 100644 --- a/event_handler.go +++ b/event_handler.go @@ -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 { @@ -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, } } @@ -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, } } @@ -81,7 +83,7 @@ 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, @@ -89,6 +91,7 @@ func (eh *eventHandler) addContainer(id, name, imageID string) *container { imageID: imageID, pod: pod, namespace: namespace, + container_name: container_name, } c.create() diff --git a/main.go b/main.go index 79679ee..ee533e1 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ 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 "", "" @@ -60,7 +60,8 @@ func main() { 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) diff --git a/prometheus.go b/prometheus.go index 01b3b67..142e47e 100644 --- a/prometheus.go +++ b/prometheus.go @@ -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",