relay

package module
v0.0.0-...-27c87f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 26 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Perfloop relay

A small program you run inside your network so Perfloop can read the telemetry you keep there: metrics, logs, profiles, and traces from providers the public internet cannot reach. It also carries end-to-end TLS to self-managed GitLab and GitHub Enterprise Server. This repository is the complete source of what runs there. The image Perfloop publishes is built from it, by the workflow in this repository, and is tagged with the commit it was built from.

Sources the relay reads today: Prometheus, VictoriaMetrics, Loki, and Go pprof. Each telemetry kind is one table of read routes in routes.go. The codehost kind carries opaque CONNECT streams to one configured HTTPS origin. A kind the relay does not know is refused when it starts.

1084 lines of Go outside tests, three runtime dependencies (coder/websocket, go.yaml.in/yaml/v3, golang.org/x/net): read it in an afternoon.

What it does

  • Opens outbound tunnels to the Perfloop API. It binds no port.
  • Answers reads that Perfloop already validated by forwarding each one to an upstream you named in its configuration file, and nothing else.
  • For telemetry, forwards only GET requests to documented read routes. Admin, write, push, and delete routes are refused before any upstream request.
  • For code hosts, accepts HTTP/2 CONNECT to a fixed upstream name and streams bytes only to its configured origin. The proxy owns TLS, credentials, and repository authorization. Transport grants reachability, never repository permission.
  • Logs one line per read or completed CONNECT, with a request id you can join to the Perfloop session that asked.

What it does not do

  • Telemetry credentials stay in your network. Upstream URLs and headers are read from the relay's own file and sent only to that upstream. Code-host credentials stay at the Perfloop proxy and cross only inside end-to-end TLS; the relay does not receive them in plaintext or add provider headers.
  • Perfloop cannot push configuration or commands. The file is read once, at start. There is no shell, no exec route, and no remote update.
  • It does not inspect or rewrite upstream responses. Give telemetry upstreams read-only tokens. Code-host streams do not use the telemetry GET route table or response limits; the relay cannot inspect their encrypted requests.
  • Revoking the relay token in Perfloop Setup ends its tunnels within 40 seconds, enforced by the Perfloop API. Stopping the relay ends its tunnels at once.

The full model, with the code that enforces each point: docs/security.md.

Telemetry quick start

  1. In Perfloop Setup, under Telemetry, Relay tokens, create a relay token. It is shown once.

  2. Write relay.yaml:

    api: https://app.perfloop.ai
    token: ${PERFLOOP_RELAY_TOKEN}
    upstreams:
      - name: vm
        kind: victoriametrics
        url: http://vmselect.monitoring.svc:8481/select/0/prometheus
        headers:
          Authorization: Bearer ${VM_READ_TOKEN}
    
  3. See exactly what this file lets the relay do, before running it:

    perfloop-relay -config relay.yaml -print-routes
    
  4. Run it, in your cluster, with outbound access to the Perfloop API host on 443 and to the upstreams:

    perfloop-relay -config relay.yaml
    

    For Kubernetes, start from examples/kubernetes/: a hardened Deployment and a NetworkPolicy that allows egress only to the Perfloop API and your upstreams.

  5. In Perfloop Setup, register the source as relay://vm.

The same steps as an ordered procedure with a checkpoint after each one, and a table of what each failure means: docs/setup.md. Agents start there (AGENTS.md).

Every configuration field: docs/configuration.md. Running, -print-routes, and the audit log: docs/operations.md.

Self-managed code hosts

Add one fixed HTTPS origin for each code host the relay may reach:

upstreams:
  - name: git
    kind: codehost
    url: https://git.example.com

Do not add headers, ca, pprof options, or targets to this upstream. Configure the code-host credentials, trust, and repository authorization in Perfloop, not in the relay. The CONNECT authority is git, with no path or query; the relay dials only git.example.com:443. An explicit port is also allowed. See the configuration contract and the security model.

The image

ghcr.io/perfloop/relay:<commit sha>

Pin it by digest. The binary is reproducible from the commit, and each published digest is signed with keyless cosign; how to verify both: docs/image.md.

Vulnerabilities: SECURITY.md.

Development

go test -race ./..., go vet ./..., golangci-lint run ./.... What the tests prove: docs/development.md.

License

Apache License 2.0. See LICENSE and NOTICE.

Documentation

Overview

Package relay is the customer end of a Perfloop relay tunnel. It runs inside the customer network, opens WebSockets to the Perfloop API, and answers the proxy's validated telemetry reads by forwarding them to the configured upstreams, and carries opaque code-host TLS streams to fixed destinations. It collects nothing: no telemetry is stored or pushed and no inbound port is opened. Telemetry credentials stay here; code-host credentials and authorization stay at the proxy, inside end-to-end TLS. README.md describes the contract; package link owns the wire shape.

Index

Constants

View Source
const (
	// DefaultMaxSeconds is the `seconds` cap of a pprof upstream that sets
	// none: net/http/pprof's own default CPU profile length. The largest cap
	// a config may set is link.MaxProfileSeconds.
	DefaultMaxSeconds = 30
	// DefaultMaxConcurrent is the in-flight cap of a pprof upstream that
	// sets none: one profile at a time per upstream.
	DefaultMaxConcurrent = 1
	// MaxConcurrentBound is the largest max_concurrent a config may set. It
	// is a config bound on how many processes profile at once, not a tunnel
	// property: the relay keeps two tunnels of maxStreams each.
	MaxConcurrentBound = 8
)

Variables

This section is empty.

Functions

func Routes

func Routes(w io.Writer, cfg Config) error

Routes writes what a validated configuration permits, one upstream at a time: the kind, the URL the relay forwards to, the request headers it passes from Perfloop, the names of the headers it adds, and every request the relay will forward. For codehost it lists the fixed CONNECT name and destination, not HTTP read routes. The relay forwards a telemetry query unchanged unless it is a pprof query, whose seconds cap it checks. Secrets are never written: header values and the token stay out. The listing is written whole, in one write, so a failed write never leaves a partial listing that reports success.

Types

type Config

type Config struct {
	// API is the URL of the Perfloop API the relay opens its tunnels to, for
	// example https://app.perfloop.ai. The relay reaches it through
	// HTTPS_PROXY when that is set; such a proxy must carry WebSockets.
	API string `yaml:"api"`
	// APICA is an optional PEM file trusted for the API certificate instead
	// of the system roots, for networks whose egress proxy re-signs TLS.
	APICA string `yaml:"api_ca"`
	// Token is the operator-issued relay token for this tenant.
	Token string `yaml:"token"`
	// Upstreams lists the sources this relay may reach.
	Upstreams []Upstream `yaml:"upstreams"`
	// LogLevel is debug, info, warn, or error.
	LogLevel string `yaml:"log_level"`
}

Config is the YAML shape of perfloop-relay. `${VAR}` references in the file expand from the environment before parsing, so the token and upstream credentials can come from a Kubernetes Secret; an unset variable fails at start, not at the first read.

func Load

func Load(path string) (Config, error)

Load reads, expands, and strictly parses a relay configuration. New validates it.

func (Config) Level

func (c Config) Level() slog.Level

Level is the configured log level. Validate refuses any value that does not parse, so on a validated configuration this is exact; before that, an unparsable value reads as info and Validate names it.

func (Config) Validate

func (c Config) Validate() error

Validate checks the configuration without touching the network.

type Relay

type Relay struct {
	// contains filtered or unexported fields
}

Relay answers proxy telemetry reads and carries code-host TLS streams to configured upstreams over tunnels to Perfloop.

func New

func New(cfg Config, logger *slog.Logger) (*Relay, error)

New validates the configuration and builds a relay.

func (*Relay) Run

func (r *Relay) Run(ctx context.Context) error

Run keeps the tunnels open until ctx ends. A tunnel that ends, for any reason, is reopened with backoff; a tunnel that lasted a minute resets the backoff. When ctx ends the tunnels close at once, including every CONNECT stream. Perfloop may move a telemetry read whose tunnel failed before its response started to another tunnel. CONNECT bytes are never replayed.

func (*Relay) ServeHTTP

func (r *Relay) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP answers one proxy request from the tunnel. The Host names the upstream. A codehost upstream accepts only a pathless HTTP/2 CONNECT and streams opaque bytes to its fixed address. For telemetry, the path is forwarded under the upstream URL when its route is a documented read. For a pprof upstream the first path segment names the target process and the query is checked against the configured caps. One audit line records every decision with the read id Perfloop gave it, `<session id>/<tool call ref>`, which names the transcript turn that asked; a read no tool call made has none.

type Upstream

type Upstream struct {
	// Name is the `relay://<name>` endpoint that Setup registers. It is one
	// lowercase DNS label.
	Name string `yaml:"name"`
	// Kind selects the transport or read routes the relay permits; routes.go
	// lists the kinds (prometheus, victoriametrics, loki, pprof, and codehost).
	Kind string `yaml:"kind"`
	// URL is the provider base: scheme, host, port, and an optional path
	// prefix for telemetry. The relay appends the proxy's request path to it.
	// A codehost URL is an HTTPS origin with no path; the relay dials only its
	// TCP address. A pprof upstream has Targets instead.
	URL string `yaml:"url"`
	// Targets are the processes of a pprof upstream: target name to the
	// http(s) base of that process's net/http/pprof handler. Perfloop names
	// one target per read as the first path segment; a name not listed here
	// is refused. Each name is one lowercase DNS label.
	Targets map[string]string `yaml:"targets"`
	// MaxSeconds caps the `seconds` a pprof read may ask for, so a CPU
	// profile cannot run longer than the customer allows. Zero means
	// DefaultMaxSeconds. The bound is link.MaxProfileSeconds.
	MaxSeconds int `yaml:"max_seconds"`
	// MaxConcurrent caps the pprof reads in flight on this upstream across
	// all of its targets, per relay process; a further read is refused at
	// once with status 429. Perfloop spreads reads over every relay a tenant
	// runs with the same token, so N relay processes admit N times this cap.
	// The customer controls that count. Zero means DefaultMaxConcurrent. The
	// bound is MaxConcurrentBound.
	MaxConcurrent int `yaml:"max_concurrent"`
	// Headers are added to every upstream request, for example an
	// Authorization header holding a read token. They replace any header of
	// the same name from the proxy. A codehost upstream refuses Headers.
	Headers map[string]string `yaml:"headers"`
	// CA is an optional PEM file trusted for this upstream's TLS certificate
	// instead of the system roots. A codehost upstream refuses CA: its TLS
	// goes end to end from the proxy, which owns certificate trust.
	CA string `yaml:"ca"`
}

Upstream is one source inside the customer network.

Directories

Path Synopsis
cmd
perfloop-relay command
Command perfloop-relay runs inside a customer network.
Command perfloop-relay runs inside a customer network.
Package link is what the two ends of a relay tunnel share: the wire shape (the relay that a customer runs and the Perfloop API that terminates the tunnel must agree on it) and the one way both read their configuration.
Package link is what the two ends of a relay tunnel share: the wire shape (the relay that a customer runs and the Perfloop API that terminates the tunnel must agree on it) and the one way both read their configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL