clienthttp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 12 Imported by: 1 module, 1 package

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

client-http

Outbound HTTP client for omcrgnt apps: ecfg catalog resource, one client bound to one base URL, OpenTelemetry traces via otelhttp, Prometheus client metrics via a small built-in RoundTripper.

Fills the gap client-grpc (gRPC) and client-s3 (S3) already closed for their transports — plain outbound REST clients (e.g. Wallet/Notify in bemvpgame/shop) had neither metrics nor tracing before this package.

Catalog

import clienthttp "github.com/omcrgnt/client-http"

type catalog struct {
	Ollama *clienthttp.Client `ecfg:"OLLAMA"`
}

Client depends on the shared *HTTPClientMetrics singleton (registered in unique.Global on import). Ops metrics actuator calls RegisterMetrics before Inject.

Typed API wrappers are built by the app from HTTPClient() + BaseURL(), e.g. ollamaapi.NewClient(u, c.Ollama.HTTPClient()) in client-ollama.

Environment

Prefix comes from the app (EnvPrefix); fields below are relative to the slot (e.g. OLLAMA).

Variable Description
OLLAMA_LABEL Resource label (otel attribute, metric label client)
OLLAMA_BASE_URL Absolute http(s):// base URL (http.v1.URL)

Example:

NPC_OLLAMA_LABEL=ollama
NPC_OLLAMA_BASE_URL=http://127.0.0.1:11434

Lifecycle

Hook Role
Build / BuildConfig Materialize from ecfg (Label, BaseURL)
Deps / Inject Wire *HTTPClientMetrics, build *http.Client with instrumented transport
Close Closes idle connections on the underlying transport directly — not via c.HTTPClient().CloseIdleConnections(), which cannot reach through otelhttp.Transport

No Start: constructing a *http.Client opens no connection (unlike a listening server, or a grpc.ClientConn's resolver/balancer goroutines in client-grpc), so Inject builds it directly — same reasoning client-s3 uses for building its AWS SDK client in Inject rather than a Start step.

No ProbeReady either, and deliberately not even a no-op one: ops/probe collection is structural (sdi matches candidateTyp.Implements(ProbeReadiness) over every registered resource, not a per-instance opt-in). A stub method here would silently enroll every consumer of Client into /readyz with an always-true vote, with no way to opt out. Readiness is inherently API-specific — "what does 'up' mean for this base URL?" — so it belongs on the wrapper that knows the answer (e.g. client-ollama.Client.ProbeReady calling Heartbeat), the same split client-grpc/client-s3 already use between transport and typed client.

Telemetry

On Inject, the transport chain is otelhttp.NewTransport(instrumentedTransport):

  • otelhttp — client spans to the process TracerProvider
  • instrumentedTransport — Prometheus http_client_request_duration_seconds (histogram, labels client/method/status_class) and http_client_in_flight_requests (gauge, label client)

Import the package so the metrics singleton exists; scrape via ops /metrics.

Scope

This package only builds an instrumented *http.Client bound to a base URL — it does not know request/response shapes or what "ready" means for the wrapped API. API-specific wrappers (e.g. client-ollama) consume it via Deps/Inject and add their own typed methods and, where it makes sense, their own ProbeReady.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an outbound HTTP resource: a *http.Client instrumented with otel tracing and Prometheus metrics, bound to one base URL. Catalog field: *Client (Configurable); the real http.Client is built in Inject once HTTPClientMetrics resolves — there is no separate Start: constructing a *http.Client opens no connection (unlike a listening server or a grpc.ClientConn's resolver/balancer goroutines), so there is nothing for a Start step to do.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the configured base URL (no trailing slash).

func (*Client) BuildConfig

func (*Client) BuildConfig() (app.Materializer, error)

BuildConfig returns the config spec for materialize.

func (*Client) Close

func (c *Client) Close(_ context.Context) error

Close releases idle connections. Calls instrumentedTransport directly rather than c.httpClient.CloseIdleConnections() — that would check only the outer otelhttp.Transport, which never forwards the call inward (see instrumentedTransport.CloseIdleConnections).

func (*Client) Deps

func (*Client) Deps() []any

Deps declares the shared HTTPClientMetrics singleton.

func (*Client) HTTPClient

func (c *Client) HTTPClient() *http.Client

HTTPClient returns the instrumented client (nil before Inject).

func (*Client) Inject

func (c *Client) Inject(args []any)

Inject receives HTTPClientMetrics from SDI and builds the instrumented http.Client: otel tracing outermost, wrapping the Prometheus-recording transport (mirrors otelgrpc.NewClientHandler + prometheus interceptors in client-grpc, for HTTP instead of gRPC).

func (*Client) Label

func (c *Client) Label() string

Label returns the configured resource label. No nil guard: the standard app flow (fill/materialize/sdi.Resolve) never hands a nil *Client to a consumer's Inject — only Build's output is ever registered — so a nil receiver here only happens by constructing *Client outside app.Run, which this package makes no promises about.

type Config

type Config struct {
	Label   commonv1.Label
	BaseURL httpv1.URL
}

Config is the outbound HTTP client spec (Label, BaseURL); ecfg fills before Build.

func (*Config) Build

func (cfg *Config) Build() (any, error)

type HTTPClientMetrics

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

HTTPClientMetrics is a singleton pool resource: shared Prometheus collectors for all client-http clients. RegisterMetrics matches ops metrics duck-typing (github.com/omcrgnt/ops/metrics.MetricsContributor; no ops import here).

func (*HTTPClientMetrics) RegisterMetrics

func (m *HTTPClientMetrics) RegisterMetrics(reg *prometheus.Registry) error

Jump to

Keyboard shortcuts

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