or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assets.mdconfig.mdexpfmt.mdhelpers-templates.mdindex.mdmodel.mdpromslog-flag.mdpromslog.mdroute.mdserver.mdversion.md
tile.json

tessl/golang-prometheus--common

Common Go libraries shared across Prometheus components and the broader Prometheus ecosystem

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/github.com/prometheus/common@v0.67.4

To install, run

npx @tessl/cli install tessl/golang-prometheus--common@0.67.0

index.mddocs/

Prometheus Common

Common 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.

Package Information

  • Package Name: github.com/prometheus/common
  • Package Type: golang
  • Language: Go
  • Version: v0.67.4
  • Installation: go get github.com/prometheus/common@v0.67.4
  • License: Apache-2.0

Core Imports

Import 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"
)

Basic Usage

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)
}

Architecture

The Prometheus Common library is organized into specialized packages, each serving a distinct purpose:

  • assets: Embedded filesystem utilities with gzip decompression
  • config: HTTP client configuration with authentication, TLS, and proxy support
  • expfmt: Encoding and decoding for Prometheus exposition formats
  • helpers/templates: Template helper functions for formatting
  • model: Core data structures for metrics, labels, time, and values
  • promslog: Structured logging wrapper around Go's log/slog
  • promslog/flag: Flag integration for logging configuration
  • route: HTTP routing with context support
  • server: HTTP server utilities
  • version: Build version information and metrics

Capabilities

Assets - Embedded File System with Gzip Support

Utilities for working with gzip-compressed embedded file systems, providing transparent decompression.

func New(fs embed.FS) FileSystem
type FileSystem struct {
    // Wraps an embed.FS to provide transparent gzip decompression
}

func (fs FileSystem) Open(path string) (fs.File, error)

Assets Package Documentation

Config - HTTP Client Configuration

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)

Config Package Documentation

Expfmt - Exposition Format Encoding/Decoding

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) Format

Expfmt Package Documentation

Helpers/Templates - Template Helper Functions

Helper 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

Model - Core Data Structures

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
}

Model Package Documentation

Promslog - Structured Logging

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() *Format

Promslog Package Documentation

Promslog/Flag - Logging Flag Integration

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

Route - HTTP Routing with Context

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) string

Route Package Documentation

Server - HTTP Server Utilities

HTTP server utilities for serving static files.

func StaticFileServer(root http.FileSystem) http.Handler

Server Package Documentation

Version - Build Information

Build 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

Version Package Documentation