Skip to content
fbegynPublic

About

Bringing Tailscale to Prometheus

Resources

Stars

9 stars

Watchers

1 watching

Forks

Repository files navigation

Tailfire

Tailfire brings the magic of Tailscale to Prometheus users. It offers both a file-based and HTTP-based service discovery mechanism for your Tailnet to be used by Prometheus.

Usage

Currently, Tailfire is in a prototypical/PoC stage. It's built with kong, so to know its options, simply check the --help output:

$ tailfire --help
Usage: tailfire <command> [flags]

A Prometheus service discovery tool for Tailscale

Flags:
  -h, --help                         Show context-sensitive help.
      --config.file="config.yaml"    location of the config path ($TAILFIRE_CONFIG_FILE)
      --version                      print the version and exit

Commands:
  serve [flags]
    serve a Prometheus HTTP SD endpoint

  generate [flags]
    generate a Prometheus file SD file

Run "tailfire <command> --help" for more information on a command.

To build Tailfire with a specific version (as reported by --version), set it at build time:

go build -ldflags "-X main.version=1.0.0" ./cmd/tailfire

Tailfire supports two running modes: serve, which spins up an endpoint that can be consumed by Prometheus HTTP SD, and generate, which is a oneshot operation that writes a file compliant with Prometheus file SD.

serve

$ tailfire serve --help
Usage: tailfire serve [flags]

serve a Prometheus HTTP SD endpoint

Flags:
  -h, --help                         Show context-sensitive help.
      --config.file="config.yaml"    location of the config path ($TAILFIRE_CONFIG_FILE)
      --version                      print the version and exit

      --port=8080                    http port for tailfire sd endpoint ($TAILFIRE_PORT)
      --host="localhost"             http host for tailfire sd endpoint ($TAILFIRE_HOST)
      --[no-]refresh-update          update the refresh interval based on the
                                     X-Prometheus-Refresh-Interval-Seconds header (default: true)
                                     ($TAILFIRE_REFRESH_UPDATE)
      --refresh.interval=60          initial refresh duration in seconds
                                     ($TAILFIRE_REFRESH_INTERVAL)
      --refresh.min=10               lowest refresh duration in seconds the refresh header can set
                                     ($TAILFIRE_REFRESH_MIN)

serve exposes two endpoints:

  • /prometheus/targets: the Prometheus HTTP SD endpoint.
  • /metrics: Tailfire's own metrics, such as tailfire_sd_failures_total (the number of failed Tailscale refreshes), next to the standard Go runtime metrics.

By default serve only listens on localhost; use --host 0.0.0.0 to expose it to other machines. A simple Prometheus configuration can then scrape the service discovery:

scrape_configs:
  - job_name: "tailfire"
    http_sd_configs:
      - url: "http://localhost:8080/prometheus/targets"

Refresh interval

Tailfire fetches the devices from the Tailscale API in the background and serves the cached result. The refresh interval starts at --refresh.interval (or refresh_interval from the config file, see precedence).

Prometheus sends its own refresh interval in the X-Prometheus-Refresh-Interval-Seconds header. When that is lower than the current interval, Tailfire lowers its refresh interval to match, but never below --refresh.min. The interval is never raised at runtime. Use --no-refresh-update to ignore the header. Starting Tailfire with a refresh interval below --refresh.min is an error.

When a refresh fails, Tailfire logs the error, increments tailfire_sd_failures_total and keeps serving the last known targets.

generate

$ tailfire generate --help
Usage: tailfire generate [flags]

generate a Prometheus file SD file

Flags:
  -h, --help                            Show context-sensitive help.
      --config.file="config.yaml"       location of the config path ($TAILFIRE_CONFIG_FILE)
      --version                         print the version and exit

      --output.file="tailscale.json"    file to write the tailscale targets to
                                        ($TAILFIRE_OUTPUT_FILE)
      --output.perm=0644                file permissions of the output file ($TAILFIRE_OUTPUT_PERM)

The generated file can be picked up by Prometheus file SD, for example by running tailfire generate from a cron job or systemd timer:

scrape_configs:
  - job_name: "tailfire"
    file_sd_configs:
      - files:
          - "/etc/prometheus/tailscale.json"

Configuration

A minimal configuration file only needs an API token:

---
api_token: "tskey-api-foo-bar"

The access token can be obtained from the Tailscale Admin Panel.

All available fields, with their defaults:

---
api_token: "tskey-api-foo-bar"
api_url: "https://api.tailscale.com"
tailnet: "-"
tag_separator: ","
# only used by `serve`
port: 8080
refresh_interval: 60s
  • api_token: The Tailscale/Headscale/... API token used to access the Tailnet. Required, unless TAILFIRE_API_TOKEN is set.
  • api_url: The URL for the management server. Default: https://api.tailscale.com
  • tailnet: Specifies the Tailnet to use. Default: "-" (uses the default Tailnet of the account).
  • tag_separator: The separator to insert between multiple values in the list __meta labels. Default: ,
  • port: The port serve listens on. Default: 8080
  • refresh_interval: The initial refresh interval of serve, as a Prometheus duration (e.g. 30s, 5m). Default: 60s

Environment variables

Every flag can also be set through an environment variable:

Variable Flag Command
TAILFIRE_CONFIG_FILE --config.file all
TAILFIRE_PORT --port serve
TAILFIRE_HOST --host serve
TAILFIRE_REFRESH_UPDATE --[no-]refresh-update serve
TAILFIRE_REFRESH_INTERVAL --refresh.interval serve
TAILFIRE_REFRESH_MIN --refresh.min serve
TAILFIRE_OUTPUT_FILE --output.file generate
TAILFIRE_OUTPUT_PERM --output.perm generate

TAILFIRE_API_TOKEN overrides api_token from the config file. This keeps the token out of the config file, e.g. when it's injected from a secret store.

Precedence

The port and refresh interval can be set in several places. From highest to lowest priority:

  1. command line flag (--port, --refresh.interval)
  2. environment variable (TAILFIRE_PORT, TAILFIRE_REFRESH_INTERVAL)
  3. config file (port, refresh_interval)
  4. built-in default (8080, 60s)

Multiple Tailnets

By default, Tailfire logs into the account's own Tailnet. It's possible to specify other Tailnets that the account has access to, like this:

---
api_token: "tskey-api-foo-bar"
tailnet: "foo.bar@gmail.com"

Headscale

Tailfire also supports Headscale. This requires a slightly more detailed configuration, as shown below:

---
api_url: "https://where.headscale.foo.bar"
api_token: "tskey-api-foo-bar"

Labels

Each device becomes its own target group. The target address (__address__) is the device's first Tailscale address. Devices without any address are skipped.

The following meta labels are available during relabeling:

Label Description
__meta_tailscale_device_id the device ID
__meta_tailscale_device_name the device's MagicDNS name, e.g. host.foo.ts.net
__meta_tailscale_device_hostname the device's hostname
__meta_tailscale_device_user the user that owns the device
__meta_tailscale_device_os the device's operating system
__meta_tailscale_device_client_version the Tailscale client version
__meta_tailscale_device_addresses all Tailscale addresses of the device*
__meta_tailscale_device_tags the device's ACL tags*
__meta_tailscale_device_enabled_routes the subnet routes enabled for the device*
__meta_tailscale_device_advertised_routes the subnet routes the device advertises*
__meta_tailscale_device_authorized true if the device is authorized
__meta_tailscale_device_is_external true if the device is shared from another Tailnet
__meta_tailscale_device_update_available true if a client update is available
__meta_tailscale_device_key_expiry_disabled true if key expiry is disabled
__meta_tailscale_device_blocks_incoming_connections true if the device blocks incoming connections

* List labels are joined with tag_separator and wrapped in it on both sides (e.g. ,tag:prod,tag:web,), so a single value can be matched with a regex like .*,tag:prod,.*. They are only set when the list is not empty.

For example, to only scrape devices tagged tag:prod on port 9100:

scrape_configs:
  - job_name: "tailfire"
    http_sd_configs:
      - url: "http://localhost:8080/prometheus/targets"
    relabel_configs:
      - source_labels: [__meta_tailscale_device_tags]
        regex: ".*,tag:prod,.*"
        action: keep
      - source_labels: [__address__]
        target_label: __address__
        replacement: "$1:9100"
      - source_labels: [__meta_tailscale_device_hostname]
        target_label: instance

About

Bringing Tailscale to Prometheus

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages