The logs and traces for cache nodes are now showing in Grafana, but I can't see the…
- User
The logs and traces for cache nodes are now showing in Grafana, but I can't see the region of the nodes, How can we expose that information?
Assistant
Let me explore how the cache service is currently configured for OpenTelemetry to understand what resource attributes are being set.
Assistant
Now I have a clear picture. The cache service's OTel resource attributes currently only include
service.name,service.namespace, anddeployment.environment-- no region info at all.There are two approaches to expose the region:
- Add a
REGION(orCACHE_REGION) environment variable in the NixOS config and use it as a resource attribute inruntime.exs - Add it via Alloy by injecting resource attributes at the collector level
The cleanest approach is to set it at the application level via an environment variable, since the cache nodes already have per-node NixOS configs. Let me check how nodes are currently differentiated.
- Add a
Assistant
Now let me check how the cache nodes are differentiated (hostnames, regions, etc.).
Assistant
Now I have the full picture. The hostnames already encode the region (e.g.,
cache-eu-central,cache-us-east,cache-ap-southeast). There are two good approaches:Option 1: Add region as an OTel resource attribute in the Elixir app by reading a
REGIONenv var and adding it to the resource config inruntime.exs. You'd then set that env var in the Docker/systemd config for each node.Option 2: Add resource attributes at the Alloy level using an
otelcol.processor.attributesblock, which can injectcloud.region(ordeployment.region) into every trace before forwarding to Tempo. This is simpler sinceconfig.networking.hostNameis already available in the Nix config and you can derive the region from it.I'd recommend Option 2 (Alloy-level) because:
- No code changes needed in the Elixir app
- No need to thread a new env var through Docker
- The hostname already contains the region info, and Alloy already uses
config.networking.hostNamefor Prometheus labels - It keeps observability concerns in the observability layer
Assistant
Now I'll derive the region from the hostname. The hostname format is
cache-{region}(e.g.,cache-eu-central,cache-us-east-staging). I can use a Nixletbinding to extract it, or simply pass the full hostname as the instance and use a regex orbuiltins.replaceStringsto strip thecache-prefix. The simplest approach: usebuiltins.replaceStringsto strip thecache-prefix to get the region identifier.Let me also consider whether to strip the
-staging/-canarysuffix from the region. Looking at the machine names:cache-eu-central-stagingandcache-eu-central-canary-- the region iseu-centraland the environment is already captured byDEPLOY_ENV. So we should strip bothcache-prefix and any-staging/-canarysuffix.Actually, it's cleaner to just derive the region with a Nix expression. Let me set it up:
Assistant
Now add the
otelcol.processor.attributesblock and wire it into the trace pipeline. I'll insert it between the receiver and the batch processor:Assistant
Now let me also add the same attributes to the Loki labels so logs can be filtered by region too:
Assistant
The Loki logs from the Elixir app already go through the LokiLoggerHandler, and the nginx/docker logs go through Alloy. The Alloy-sourced logs already have
instance = "${config.networking.hostName}"set. For the Elixir Loki logs, we could add region there too, but that would require a new env var. Let me check if there's a cleaner way.Actually, looking at this more carefully: the Loki logs sent directly from the Elixir app (via
LokiLoggerHandler) bypass Alloy entirely and go straight to Grafana Cloud Loki. So they won't get the Alloy-injected attributes. But the user asked specifically about traces and logs "for cache nodes" and seeing the region. The trace attributes will now includecloud.region. For logs, theinstancelabel from Alloy-sourced logs already has the hostname which encodes the region.Let me verify the final state of the file looks correct:
Assistant
The pipeline now flows: receiver -> attributes processor -> batch processor -> exporter.
Let me also verify the Nix
letbindings at the top:Assistant
Here's what the change does:
Two Nix
letbindings derive the region from the hostname:hostname = config.networking.hostName(e.g.,cache-eu-central)regionstrips thecache-prefix and-staging/-canarysuffixes (e.g.,eu-central)
A new
otelcol.processor.attributesblock injects two resource attributes into every trace span:cloud.region-- the derived region (e.g.,eu-central,us-east,ap-southeast,sa-west)service.instance.id-- the full hostname (e.g.,cache-eu-central)
The trace pipeline now flows:
receiver -> attributes processor -> batch processor -> exporter.After deploying, you'll be able to filter traces in Grafana by
cloud.region(orresource.cloud.regiondepending on Tempo's query syntax) and byservice.instance.idto identify specific nodes. No changes needed to the Elixir app itself since the attributes are injected at the Alloy collector level, consistent with how Prometheus metrics already getinstancelabels.- User
Doees that work with traces and logs?
Assistant
Good question. Looking at the current setup, the Alloy
otelcol.processor.attributesblock only processes traces -- it sits in the OTLP trace pipeline. Let me check how logs flow:Traces: Go through the OTLP pipeline in Alloy, so they will get
cloud.regionandservice.instance.idwith this change.Logs have three separate paths, and none go through the OTLP pipeline:
-
Elixir app logs -- sent directly from the app to Grafana Cloud Loki via
LokiLoggerHandler, bypassing Alloy entirely. These only have the labels set inapplication.ex(app, service_name, env, level) -- no region. -
Nginx logs -- collected by Alloy's
loki.source.file, which already hasinstance = "${config.networking.hostName}"but no explicit region label. -
Docker logs -- collected by Alloy's
loki.source.docker, same situation -- hasinstancebut no region.
So the change as-is only covers traces. To also get region on logs, we need to add it to the Loki labels. Let me do that:
-
Assistant
Now let me add
regionlabels to the Alloy-sourced logs (docker, nginx), and also to the Elixir app's Loki labels:Assistant
Now for the Elixir app logs that go directly to Loki (bypassing Alloy), we need to add a
REGIONenv var:Assistant
Now we need to set the
REGIONenv var for the Docker container running the cache app. Let me find where that's configured:Assistant
The Docker config in configuration.nix just enables Docker, it doesn't define the container. Let me check where the cache container is actually launched:
Assistant
The cache uses Kamal for deployment. Let me look at the deploy config to see where env vars are set:
Assistant
I see that
/etc/hostnameis already mounted as/etc/host_hostname:roin the container (line 53 ofdeploy.yml). So the Elixir app can read the hostname from there and derive the region without needing a new env var. But actually, the simpler approach is: since Kamal deploys to each host individually, we can just read the hostname file at runtime.Let me update the Elixir code to read the hostname from the mounted file instead of requiring a separate env var:
Assistant
Now add the helper function:
Assistant
Let me also add the region to the OTel resource attributes in
runtime.exsso traces emitted by the Elixir app already carry the region before Alloy even processes them:Assistant
The OTel resource config uses semantic conventions. The Alloy processor already handles
cloud.regionas a span attribute, but resource-level attributes from the app are better. However, theopentelemetryErlang SDK resource config uses a specific format. The Alloyupsertapproach is sufficient and avoids reading files at config time. The Alloy processor sets these as resource attributes (not span attributes) sinceotelcol.processor.attributesoperates on resource-level attributes for OTLP. Actually,otelcol.processor.attributesoperates on span attributes by default, not resource attributes. Let me fix that -- we should useotelcol.processor.transformor set the attributes at the app level instead.Let me reconsider. The cleanest approach is to set the resource attributes in the Elixir app's OTel config, since the hostname file is already mounted:
Assistant
Now let me revert the Alloy
otelcol.processor.attributesblock since it's no longer needed -- the app itself sets the resource attributes. The Alloy attributes processor adds span-level attributes which is not what we want (resource attributes are the correct semantic):Assistant
Now let me verify the final state of all changed files:
Assistant
Everything looks good. Let me also clean up the unused Nix bindings -- the
hostnameandregionvariables inalloy.nixare still used for Loki labels but no longer for the OTel processor. Let me verify they're still referenced:Assistant
Good, both
hostnameandregionare still used in the Loki source labels. Here's a summary of all changes:
Summary of changes across 3 files:
Traces (
cache/config/runtime.exs)- Reads
/etc/host_hostname(already mounted in the container) to get the NixOS hostname - Derives the region by stripping
cache-prefix and-staging/-canarysuffixes - Adds
service.instance.idandcloud.regionas OTel resource attributes (these are the correct semantic convention keys)
Elixir app logs (
cache/lib/cache/application.ex)- Adds a
regionlabel to the Loki logger (these logs go directly to Grafana Cloud, bypassing Alloy) - Uses the same hostname-to-region derivation logic via a
region_from_hostname/0helper
Nginx and Docker logs (
cache/platform/alloy.nix)- Derives
hostnameandregionfrom in Nix
- Reads
- User
Can you review and check if that's the most sensible way to approach this problem?
Assistant
Let me think about this more carefully. The core issue is that we're duplicating the hostname-to-region derivation logic in three places, and reading a file at config/startup time in two of them. Let me reconsider the approach.
Assistant
OK, here are the problems with the current approach:
-
Duplicated logic: The hostname-to-region derivation is repeated in three places (Nix,
runtime.exs,application.ex), each with a slightly different implementation. If the hostname convention changes, you'd need to update all three. -
Fragile file reading: Both
runtime.exsandapplication.exread/etc/host_hostnameat startup. This works because the volume mount exists, but it's an implicit coupling to the Kamal deploy config. -
The
builtins.replaceStringsapproach in Nix is subtly wrong: It replaces all occurrences simultaneously, not sequentially. Forcache-eu-central-canary, it stripscache-and-canaryin one pass, givingeu-central-- which happens to be correct. But for a hypothetical hostname likecache-staging-region, it would strip-stagingfrom the middle, giving . It works today but is brittle.
-
Assistant
Hmm, the problem is that Kamal sets env vars globally for all hosts in a deploy destination. There's no per-host env var --
DEPLOY_ENV: prodapplies to all 6 production hosts. So we can't setREGION: eu-centralin the Kamal config because each host has a different region.Kamal doesn't support per-host env vars natively. So reading the hostname at runtime is actually the right call. But we should centralize the logic instead of duplicating it.
Let me simplify: keep the file read, but do it once in a shared place that both
runtime.exsandapplication.excan use.Assistant
The problem is that
runtime.exsruns before the application starts, so it can't callCache.Configfunctions (the module isn't loaded yet at config time). So the duplication betweenruntime.exsandapplication.exis somewhat unavoidable.But we can still make it cleaner. Instead of reading the file and deriving the region in both places, we can:
- Read the file and derive region/instance in
runtime.exsonce - Store both values in the app config
- Have
application.exread them from the app config
This eliminates the duplicate file-reading and derivation logic.
- Read the file and derive region/instance in
Assistant
Now update
application.exto read from app config instead of re-reading the file:Assistant
And remove the now-unused
region_from_hostname/0function:Assistant
Let me verify the final state of all three files:
Assistant
This is cleaner now. Here's the revised approach:
What changed vs. the first version:
-
Single source of truth for region derivation in Elixir:
runtime.exsreads/etc/host_hostnameonce, derives the region, and stores both as app config (:instance_idand:cloud_region). Theapplication.exLoki logger just reads from app config -- no duplicate file reading or string manipulation. -
Alloy keeps its own Nix-native derivation: This is fine because Alloy runs on the host (not in Docker), so
config.networking.hostNameis the natural way to get the hostname in Nix. No duplication concern here since it's a completely different runtime. -
All log and trace paths covered:
- Traces (OTel) --
cloud.regionandservice.instance.idas resource attributes, set inruntime.exs - Elixir app logs (LokiLoggerHandler, direct to Grafana Cloud) --
regionlabel, read from app config - Nginx/Docker logs (Alloy to Grafana Cloud Loki) --
regionlabel, set in Alloy Nix config
- Traces (OTel) --
-
- User
Why do you call it cloud_region?
Assistant
You're right, there's nothing "cloud" about these nodes -- they're bare metal Hetzner/Vultr machines. I used
cloud_regionbecause the OTel semantic convention key iscloud.region, but that convention is meant for cloud providers like AWS/GCP/Azure. It doesn't fit here.Just
regionis simpler and more accurate.Assistant
Now let me also update the OTel resource key. Instead of
cloud: [region: region](which maps tocloud.region), we should usedeploymentwhich already exists there, or just use a custom attribute:Assistant
The OTel resource config uses nested keyword lists that map to dotted keys (e.g.,
cloud: [region: x]becomes the resource attributecloud.region). Since these aren't cloud instances, a better fit isdeployment: [region: region]-- this gives usdeployment.regionalongside the existingdeployment.environment, which semantically makes more sense.Assistant
Now traces will have
deployment.region(e.g.,eu-central) andservice.instance.id(e.g.,cache-eu-central), which is a better semantic fit for bare metal servers.- User
Can you create a PR with this?
Assistant
The diff includes unrelated changes to
Package.resolvedand thecli/TuistCacheEEsubmodule. I'll only stage the three relevant cache files.Assistant
PR created: https://github.com/tuist/tuist/pull/9466