Common Go libraries shared across Prometheus components and the broader Prometheus ecosystem
npx @tessl/cli install tessl/golang-prometheus--common@0.67.0Common Go libraries shared across Prometheus components and the broader Prometheus ecosystem. This library provides essential utilities for configuration, logging, routing, metrics formatting, data models, and more.
go get github.com/prometheus/common@v0.67.4Import specific packages as needed:
import (
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/promslog"
"github.com/prometheus/common/route"
"github.com/prometheus/common/version"
)This library is designed to be imported by Prometheus components and ecosystem tools. Here's a simple example using the config package to create an HTTP client:
package main
import (
"fmt"
"net/http"
"github.com/prometheus/common/config"
)
func main() {
// Create HTTP client configuration
cfg := config.HTTPClientConfig{
TLSConfig: config.TLSConfig{
InsecureSkipVerify: false,
},
FollowRedirects: true,
EnableHTTP2: true,
}
// Create HTTP client from configuration
client, err := config.NewClientFromConfig(cfg, "example-client")
if err != nil {
panic(err)
}
// Use the client
resp, err := client.Get("https://example.com")
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println("Response status:", resp.Status)
}The Prometheus Common library is organized into specialized packages, each serving a distinct purpose:
Utilities for working with gzip-compressed embedded file systems, providing transparent decompression.
func New(fs embed.FS) FileSystemtype FileSystem struct {
// Wraps an embed.FS to provide transparent gzip decompression
}
func (fs FileSystem) Open(path string) (fs.File, error)Comprehensive HTTP client configuration with support for various authentication methods, TLS, proxies, and custom headers.
type HTTPClientConfig struct {
BasicAuth *BasicAuth
Authorization *Authorization
OAuth2 *OAuth2
BearerToken Secret
BearerTokenFile string
TLSConfig TLSConfig
FollowRedirects bool
EnableHTTP2 bool
ProxyConfig ProxyConfig
HTTPHeaders *Headers
}
func NewClientFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HTTPClientOption) (*http.Client, error)
func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HTTPClientOption) (http.RoundTripper, error)Tools for reading and writing Prometheus metrics in various formats (text, protobuf, OpenMetrics).
type Format string
func NewDecoder(r io.Reader, format Format) Decoder
func NewEncoder(w io.Writer, format Format, options ...EncoderOption) Encoder
func Negotiate(h http.Header) FormatHelper functions for template processing, especially time-related formatting.
func HumanizeDuration(i interface{}) (string, error)
func HumanizeTimestamp(i interface{}) (string, error)
func ConvertToFloat(i interface{}) (float64, error)Helpers/Templates Package Documentation
Shared data structures for Prometheus metrics, labels, time, and values.
type Metric map[LabelName]LabelValue
type Sample struct {
Metric Metric
Value SampleValue
Timestamp Time
Histogram *SampleHistogram
}
type Time int64 // Milliseconds since Unix epoch
type LabelSet map[LabelName]LabelValue
type Alert struct {
Labels LabelSet
Annotations LabelSet
StartsAt time.Time
EndsAt time.Time
GeneratorURL string
}Standardized logging configuration for Prometheus components using Go's log/slog.
type Config struct {
Level *Level
Format *Format
Style LogStyle
Writer io.Writer
}
func New(config *Config) *slog.Logger
func NewLevel() *Level
func NewFormat() *FormatPromslog Package Documentation
Standardized flag interactions for logging configuration with Kingpin.
const LevelFlagName = "log.level"
const FormatFlagName = "log.format"
func AddFlags(a *kingpin.Application, config *promslog.Config)Promslog/Flag Package Documentation
HTTP router with support for prefixed sub-routers, context injection, and instrumentation.
type Router struct {
// HTTP router with context support
}
func New() *Router
func (r *Router) Get(path string, h http.HandlerFunc)
func (r *Router) Post(path string, h http.HandlerFunc)
func (r *Router) Put(path string, h http.HandlerFunc)
func (r *Router) Del(path string, h http.HandlerFunc)
func Param(ctx context.Context, p string) stringHTTP server utilities for serving static files.
func StaticFileServer(root http.FileSystem) http.HandlerBuild information utilities for Prometheus components.
var Version string
var Revision string
var Branch string
var BuildDate string
func Print(program string) string
func Info() string
func PrometheusUserAgent() string
func ComponentUserAgent(component string) string