Client¶
OtelCollectorClient is the entry point. Create one per process (or per set of credentials), share
it between threads/tasks and close it on shutdown. Service functions hang off the client (see
Functions).
| Function | Returns | Notes |
|---|---|---|
constructor / create / new |
client | validates the config (CONFIG error) |
fromEnv() |
client | config from GROW_OPENTELEMETRY_COLLECTOR_*, GROW_OTEL_* and OTEL_EXPORTER_OTLP_* variables |
health() |
HealthStatus { healthy, status, details } |
unauthenticated health_check extension (port 13133), zpages /debug/servicez fallback (port 55679); never throws for an unhealthy or unreachable collector, only for CANCELLED and CONFIG |
waitUntilReady(timeout) |
HealthStatus |
polls health() every 500 ms; TIMEOUT after timeout (default 30 s) |
ping() |
latency | empty OTLP trace export (HTTP or gRPC) with the configured headers; AUTHENTICATION / PERMISSION_DENIED when an authenticator rejects them |
serverVersion() |
version string | zpages servicez "Version", else target_info; UNSUPPORTED when neither answers |
close() |
— | idempotent; flushes and shuts down the telemetry handles the client owns |
status is ok, degraded, unhealthy, unreachable (network error) or the vendor's word.
Create, use and close¶
import { OtelCollectorClient, OtelCollectorConfig } from "@grow-codes/opentelemetry-collector";
const client = new OtelCollectorClient(new OtelCollectorConfig({ url: "http://127.0.0.1:4318" }));
const health = await client.health(); // { healthy, status, details }
const latencyMs = await client.ping(); // number (ms)
const version = await client.serverVersion(); // string
await client.close(); // safe to call twice
// Cancellation and per-call deadline: every I/O method accepts { signal }
const status = await client.health({ signal: AbortSignal.timeout(2_000) });
use grow_opentelemetry_collector::{OtelCollectorClient, OtelCollectorConfig};
let config = OtelCollectorConfig::builder().url("http://127.0.0.1:4318").build()?;
let client = OtelCollectorClient::new(config)?;
let health = client.health().await?; // HealthStatus
let latency = client.ping().await?; // Duration
let version = client.server_version().await?; // String
client.close().await; // safe to call twice
// Cancellation: drop the future, e.g. with a deadline
let status = tokio::time::timeout(std::time::Duration::from_secs(2), client.health()).await;
Without an async runtime, enable the blocking feature:
client, err := otelcollector.NewClient(otelcollector.Config{
CommonConfig: otelcollector.CommonConfig{URL: "http://127.0.0.1:4318"},
})
if err != nil {
return err
}
defer client.Close() // safe to call twice
health, err := client.Health(ctx) // HealthStatus
latency, err := client.Ping(ctx) // time.Duration
version, err := client.ServerVersion(ctx) // string
// Cancellation and deadlines come from ctx
ctx2, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
health, err = client.Health(ctx2)
from grow_opentelemetry_collector import CancelToken, OtelCollectorClient, OtelCollectorConfig
with OtelCollectorClient(OtelCollectorConfig(url="http://127.0.0.1:4318")) as client:
health = client.health() # HealthStatus
latency_ms = client.ping() # float (ms)
version = client.server_version() # str
# Cancel from another thread
token = CancelToken()
health = client.health(cancel=token)
# close() ran; calling it again is safe
AsyncOtelCollectorClient has the same methods as coroutines (await client.health(),
await client.aclose(), async with), and honours task cancellation.
OtelCollectorConfig config = OtelCollectorConfig.builder().url("http://127.0.0.1:4318").build();
try (OtelCollectorClient client = OtelCollectorClient.create(config)) {
HealthStatus health = client.health();
Duration latency = client.ping();
String version = client.serverVersion();
// Async variants return CompletableFuture; cancel(true) cancels the request
CompletableFuture<HealthStatus> pending = client.healthAsync();
pending.orTimeout(2, TimeUnit.SECONDS).join();
} // close() is idempotent
Lifecycle rules¶
- The client is safe for concurrent use. Reuse it: it keeps a connection pool.
close()flushes queued work (for example log batches) within the configured deadline and is safe to call more than once. Any call on a closed client fails withCANCELLED("<operation>: client is closed").- Each language also offers its idiomatic scope: TypeScript
try/finally, Rustclose().await(plus best-effortDrop), Godefer client.Close(), Pythonwith/async with, Java try-with-resources.