Agent

class Agent(val options: AgentOptions, inProcessServerName: String = "", testMode: Boolean = false, initBlock: Agent.() -> Unit? = null) : GenericService<ConfigVals> (source)

Prometheus Agent that connects to a Proxy to enable metrics scraping across firewalls.

The Agent is the component that runs inside the firewall alongside the services being monitored. It establishes an outbound connection to the Proxy, registers the metrics endpoints it can scrape, and responds to scrape requests by fetching metrics from the actual endpoints and returning them to the Proxy.

Architecture

The Agent maintains a persistent gRPC connection to the Proxy and handles:

  • Connection Management: Establishes and maintains connection to proxy with automatic reconnection

  • Path Registration: Registers configured metrics endpoints with the proxy

  • Scrape Processing: Responds to scrape requests by fetching metrics from local endpoints

  • Concurrent Scraping: Handles multiple concurrent scrape requests with configurable limits

  • Heartbeat: Sends periodic heartbeat messages to maintain connection health

  • Metrics Collection: Exposes operational metrics about the agent itself

Configuration

Agents are configured via HOCON configuration files that specify:

  • Proxy connection details (hostname, port, TLS settings)

  • Path configurations (endpoints to scrape and their metadata)

  • Operational parameters (timeouts, concurrency limits, retry policies)

  • Admin and metrics endpoints for monitoring the agent itself

Usage Examples

Basic Usage

val agent = Agent(AgentOptions(args))
agent.startSync()

Embedded Usage

val agentInfo = Agent.startAsyncAgent("config.conf", true)
println("Started agent: ${agentInfo.agentName}")

With Custom Initialization

val agent = Agent(options) {
// Custom initialization logic
logger.info { "Agent initialized with custom settings" }
}
agent.startSync()

Connection Lifecycle

  1. Startup: Agent reads configuration and initializes services

  2. Connection: Establishes gRPC connection to proxy

  3. Registration: Registers configured paths with proxy

  4. Operation: Processes scrape requests and sends heartbeats

  5. Reconnection: Automatically reconnects on connection failures

  6. Shutdown: Gracefully closes connections and cleans up resources

Error Handling

The Agent includes robust error handling for:

  • Network connectivity issues with exponential backoff

  • Individual scrape failures without affecting other scrapes

  • Configuration errors with detailed error messages

  • Resource exhaustion with proper cleanup

Since

1.0.0

Parameters

options

Configuration options for the agent, typically loaded from command line or config files

inProcessServerName

Optional in-process server name for testing scenarios. When specified, the agent will connect to an in-process gRPC server instead of a network server.

testMode

Whether to run in test mode. Test mode may disable certain features or use different defaults suitable for testing environments.

initBlock

Optional initialization block executed after construction but before startup. Useful for custom configuration or testing setup.

See also

for configuration details

for the corresponding proxy component

for information about embedded agent instances

Constructors

Link copied to clipboard
constructor(options: AgentOptions, inProcessServerName: String = "", testMode: Boolean = false, initBlock: Agent.() -> Unit? = null)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
internal var agentId: String
Link copied to clipboard
internal val agentName: String
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
lateinit var jmxReporter: com/codahale/metrics/jmx/JmxReporter
Link copied to clipboard
internal val launchId: String
Link copied to clipboard
internal val metrics: AgentMetrics
Link copied to clipboard
lateinit var metricsService: MetricsService
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
internal val proxyHost: String
Link copied to clipboard
Link copied to clipboard
lateinit var servletService: ServletService
Link copied to clipboard
Link copied to clipboard
lateinit var zipkinReporterService: ZipkinReporterService

Functions

Link copied to clipboard
fun addListener(listener: Service.Listener, executor: Executor)
Link copied to clipboard
internal fun awaitInitialConnection(timeout: Duration): Boolean

Waits for the initial connection to the proxy to be established.

Link copied to clipboard
fun awaitRunning(timeout: Duration)
fun awaitRunning(timeout: Long, unit: TimeUnit)
Link copied to clipboard
fun awaitTerminated(timeout: Long, unit: TimeUnit)
Link copied to clipboard
open override fun close()
Link copied to clipboard
internal fun decrementBacklog(delta: Int)

Atomically decrements scrapeRequestBacklogSize by delta, clamping at zero.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun initServletService(servletInit: ServletGroup.() -> Unit)
Link copied to clipboard
internal fun markMsgSent()

Marks the current time as when the last message was sent to the proxy.

Link copied to clipboard
internal fun metrics(args: AgentMetrics.() -> Unit)

Executes metrics operations if metrics collection is enabled.

Link copied to clipboard
@CanIgnoreReturnValue
fun startAsync(): Service
Link copied to clipboard
fun startSync(timeout: Duration)
Link copied to clipboard
internal fun startTimer(agent: Agent): Summary.Timer?
Link copied to clipboard
fun state(): Service.State
Link copied to clipboard
@CanIgnoreReturnValue
fun stopAsync(): Service
Link copied to clipboard
fun stopSync(timeout: Duration)
Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
internal fun updateScrapeCounter(type: String)

Updates the scrape counter metrics for the specified operation type.