MetaData at different levels

Today I learned that if I put metadata at the the “service” level, Nomad will pass that on to Consul, where it will be available to use for other uses, such as for Prometheus metric labeling.

For example this key/value pair:

service {
  meta {
    api-version = "1.2.3"
  }
}

Becomes this label in a Prometheus scrape:

__meta_consul_service_metadata_api_version="1.2.3"

(notice that dashes are converted to underscores)


However, if I put metadata at the the “job”, “group”, or “task” levels, it does not show up in Consul… or anywhere else that I have looked. I am curious about how the metadata at these levels is accessed and used.

Hi @SunSparc; the meta block within the service block is specific to Consul and the service registration functionality. The metadata blocks at the job, group, and task level share the same name, but behave differently. These metadata blocks are merged upwards, meaning key values defined at the job level are applied to all groups and tasks within the job. The metadata values can be used as variables during interpolation.

An example shortened job specification using this could be:

job "example" {
  meta {
    foo = "bar"
  }
  ...
  group "example" {
    ...
    task "example {
      service {
        name = "${NOMAD_META_foo}"
      }
    }
  }
}

Thanks,
jrasell and the Nomad team

2 Likes

@jrasell, ok, that makes more sense to me know. Thank you for the explanation and the example.