Custom label based on containers name

I have the loki-docker-driver configured on my server to ship logs from all running containers to Loki. According to the Loki documentation (Docker driver client configuration | Grafana Loki documentation), the Docker driver will add the following labels to each log line by default:

  • filename: where the log is written to on disk
  • host: the hostname where the log has been generated
  • swarm_stack, swarm_service: added when deploying from Docker Swarm
  • compose_project, compose_service: added when deploying with Docker Compose

I need a custom label named branch. On the server, there are containers with names like name1-something-dev, name1-something-ui, name1-something-prod, and so on. The branch label should categorize containers by dev, ui, and prod.

Here is my current loki-docker-driver configuration:
{
“debug”: true,
“log-driver”: “loki”,
“log-opts”: {
“loki-url”: “http://loki:3100/loki/api/v1/push”,
“keep-file”: “true”,
“loki-retries”: “2”,
“loki-max-backoff”: “800ms”,
“loki-timeout”: “1s”
}
}

I’m new to such kind of tools, and don’t have experience with them. Where I should start?

Couple of solutions:

  1. If you have different container cluster for different environment (branch), you can simply hardcode the branch or environment name.

  2. You can parse container_name label.

  3. You can make branch or environment a docker label and set it during deployment.