Convert Figma logo to code with AI

prometheus logoclient_golang

Prometheus instrumentation library for Go applications

5,318
1,168
5,318
105

Top Related Projects

23,307

Like Prometheus, but for logs.

14,466

Agent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.

12,129

:tropical_fish: Beats - Lightweight shippers for Elasticsearch & Logstash

Main repository for Datadog Agent

OpenTelemetry Go API and SDK

VictoriaMetrics: fast, cost-effective monitoring solution and time series database

Quick Overview

prometheus/client_golang is the official Go client library for Prometheus, a popular open-source monitoring and alerting toolkit. It provides Go developers with the tools to instrument their applications for Prometheus monitoring, including metrics collection, exposition, and pushing to Pushgateway.

Pros

  • Easy integration with Go applications
  • Supports various metric types (counters, gauges, histograms, summaries)
  • Provides both pull-based and push-based metric collection
  • Well-documented and actively maintained

Cons

  • Learning curve for developers new to Prometheus concepts
  • Can introduce performance overhead if not used judiciously
  • Limited built-in visualization capabilities (requires separate Prometheus server)

Code Examples

  1. Creating and incrementing a counter:
import "github.com/prometheus/client_golang/prometheus"

counter := prometheus.NewCounter(prometheus.CounterOpts{
    Name: "my_counter",
    Help: "This is my counter",
})
prometheus.MustRegister(counter)

counter.Inc() // Increment the counter
  1. Creating a gauge and setting its value:
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "my_gauge",
    Help: "This is my gauge",
})
prometheus.MustRegister(gauge)

gauge.Set(42.0) // Set the gauge value
  1. Exposing metrics via HTTP:
import (
    "net/http"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)

Getting Started

To start using prometheus/client_golang in your Go project:

  1. Install the library:

    go get github.com/prometheus/client_golang/prometheus
    
  2. Import the necessary packages in your Go code:

    import (
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promauto"
        "github.com/prometheus/client_golang/prometheus/promhttp"
    )
    
  3. Create and register metrics in your application code (see examples above).

  4. Expose the metrics endpoint in your HTTP server:

    http.Handle("/metrics", promhttp.Handler())
    
  5. Start collecting and analyzing metrics with a Prometheus server pointed at your application's /metrics endpoint.

Competitor Comparisons

23,307

Like Prometheus, but for logs.

Pros of Loki

  • Designed for efficient log aggregation and storage, optimized for high-volume log data
  • Supports multi-tenancy, allowing multiple teams to use the same Loki instance
  • Integrates seamlessly with Grafana for visualization and querying

Cons of Loki

  • Limited to log data, while Prometheus client supports various metric types
  • Requires additional setup for metric extraction from logs
  • Less mature ecosystem compared to Prometheus

Code Comparison

Loki (LogQL query):

{app="myapp"} |= "error" | json | rate[5m]

Prometheus client (Go):

counter := promauto.NewCounter(prometheus.CounterOpts{
    Name: "myapp_processed_ops_total",
    Help: "The total number of processed operations",
})
counter.Inc()

Key Differences

  • Loki focuses on log aggregation and querying, while Prometheus client is for instrumenting applications with metrics
  • Loki uses LogQL for querying logs, Prometheus uses PromQL for querying metrics
  • Prometheus client provides direct instrumentation in code, Loki typically ingests logs from external sources

Use Cases

  • Use Loki for centralized log management and analysis
  • Use Prometheus client for application-level metrics and instrumentation
  • Consider using both in conjunction for comprehensive observability
14,466

Agent for collecting, processing, aggregating, and writing metrics, logs, and other arbitrary data.

Pros of Telegraf

  • More versatile: Supports multiple input and output plugins for various data sources and destinations
  • Easier setup: Pre-built binaries and configurations for quick deployment
  • Broader scope: Collects system-level metrics and application data out-of-the-box

Cons of Telegraf

  • Higher resource usage: May consume more system resources due to its comprehensive nature
  • Steeper learning curve: More complex configuration options for advanced use cases

Code Comparison

Telegraf configuration (TOML):

[[inputs.cpu]]
  percpu = true
  totalcpu = true
  collect_cpu_time = false
  report_active = false

[[outputs.prometheus_client]]
  listen = ":9273"

client_golang instrumentation (Go):

cpuTemp := prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "cpu_temperature_celsius",
    Help: "Current temperature of the CPU.",
})
prometheus.MustRegister(cpuTemp)

cpuTemp.Set(65.3)

Telegraf offers a more configuration-driven approach, while client_golang requires direct code instrumentation. Telegraf's plugin system allows for easy extension, whereas client_golang provides fine-grained control over metric collection and exposition.

12,129

:tropical_fish: Beats - Lightweight shippers for Elasticsearch & Logstash

Pros of Beats

  • Comprehensive data shipping platform for various log types and metrics
  • Integrates seamlessly with Elasticsearch and Kibana for powerful analytics
  • Supports multiple output destinations (e.g., Elasticsearch, Logstash, Kafka)

Cons of Beats

  • Steeper learning curve due to its extensive feature set
  • Requires more system resources compared to simpler monitoring solutions
  • Configuration can be complex for advanced use cases

Code Comparison

Beats (Metricbeat example):

metricbeat.modules:
- module: system
  metricsets: ["cpu", "memory"]
  period: 10s
output.elasticsearch:
  hosts: ["localhost:9200"]

client_golang:

http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)

Summary

Beats offers a comprehensive data shipping solution with deep integration into the Elastic ecosystem, while client_golang provides a lightweight Prometheus client for Go applications. Beats excels in versatility and data processing capabilities, whereas client_golang is simpler to set up and integrate for basic metric collection in Go projects.

Main repository for Datadog Agent

Pros of datadog-agent

  • Comprehensive monitoring solution with built-in integrations for various services and platforms
  • Provides advanced features like APM, log management, and real-time alerting out of the box
  • Offers a user-friendly web interface for data visualization and analysis

Cons of datadog-agent

  • Closed-source and proprietary, limiting customization options
  • Requires a paid subscription, which can be costly for large-scale deployments
  • May have a steeper learning curve due to its extensive feature set

Code Comparison

datadog-agent (Go):

import (
    "github.com/DataDog/datadog-agent/pkg/metrics"
)

sender, _ := aggregator.GetSender(aggregator.DefaultSenderName)
sender.Gauge("my.metric", 42.0, "", nil)

client_golang (Go):

import (
    "github.com/prometheus/client_golang/prometheus"
)

gauge := prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "my_metric",
    Help: "Description of my metric",
})
gauge.Set(42.0)

Both repositories provide monitoring solutions, but datadog-agent offers a more comprehensive, out-of-the-box experience with advanced features and integrations. However, it comes at the cost of being closed-source and requiring a paid subscription. client_golang, on the other hand, is open-source and focuses on providing a flexible foundation for metric collection and exposition in the Prometheus ecosystem.

OpenTelemetry Go API and SDK

Pros of OpenTelemetry Go

  • More comprehensive observability solution, covering metrics, traces, and logs
  • Vendor-agnostic, supporting multiple backends and formats
  • Actively developed with a larger community and ecosystem

Cons of OpenTelemetry Go

  • Steeper learning curve due to more complex API and concepts
  • Potentially higher overhead in some scenarios
  • Still evolving, with some features in beta or experimental stages

Code Comparison

OpenTelemetry Go:

import "go.opentelemetry.io/otel/metric"

meter := metric.NewMeterProvider().Meter("my-meter")
counter, _ := meter.Int64Counter("my_counter")
counter.Add(ctx, 1, attribute.String("key", "value"))

Prometheus Client Golang:

import "github.com/prometheus/client_golang/prometheus"

counter := prometheus.NewCounter(prometheus.CounterOpts{
    Name: "my_counter",
    Help: "This is my counter",
})
prometheus.MustRegister(counter)
counter.Inc()

Summary

OpenTelemetry Go offers a more comprehensive and flexible observability solution, supporting multiple backends and covering metrics, traces, and logs. However, it comes with a steeper learning curve and potentially higher overhead. Prometheus Client Golang is more focused on metrics and has a simpler API, making it easier to adopt for basic use cases. The choice between the two depends on specific project requirements and the desired level of observability complexity.

VictoriaMetrics: fast, cost-effective monitoring solution and time series database

Pros of VictoriaMetrics

  • Higher performance and scalability for time series data storage and querying
  • Built-in support for multi-tenancy and high cardinality data
  • More efficient data compression, resulting in lower storage costs

Cons of VictoriaMetrics

  • Less mature ecosystem compared to Prometheus
  • Potential learning curve for teams already familiar with Prometheus
  • Some advanced Prometheus features may not be fully supported

Code Comparison

VictoriaMetrics client example:

import "github.com/VictoriaMetrics/metrics"

var counter = metrics.NewCounter("http_requests_total")

func handleRequest() {
    counter.Inc()
}

Prometheus client example:

import "github.com/prometheus/client_golang/prometheus"

var counter = prometheus.NewCounter(prometheus.CounterOpts{
    Name: "http_requests_total",
})

func handleRequest() {
    counter.Inc()
}

Both clients offer similar functionality for basic metric collection, but VictoriaMetrics provides a simpler API with less boilerplate code. However, Prometheus client_golang offers more advanced features and customization options out of the box.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Prometheus Go client library

CI Go Report Card Go Reference Slack

This is the Go client library for Prometheus. It has two separate parts, one for instrumenting application code, and one for creating clients that talk to the Prometheus HTTP API.

This library requires Go1.21 or later.

The library mandates the use of Go1.21 or subsequent versions. While it has demonstrated functionality with versions as old as Go 1.17, our commitment remains to offer support and rectifications for only the most recent three major releases.

Important note about releases and stability

This repository generally follows Semantic Versioning. However, the API client in prometheus/client_golang/api/… is still considered experimental. Breaking changes of the API client will not trigger a new major release. The same is true for selected other new features explicitly marked as EXPERIMENTAL in CHANGELOG.md.

Features that require breaking changes in the stable parts of the repository are being batched up and tracked in the v2 milestone, but plans for further development of v2 at the moment.

NOTE: The initial v2 attempt is in a separate branch. We also started experimenting on a new prometheus.V2.* APIs in the 1.x's V2 struct. Help wanted!

Instrumenting applications

Go Reference

The prometheus directory contains the instrumentation library. See the guide on the Prometheus website to learn more about instrumenting applications.

The examples directory contains simple examples of instrumented code.

Client for the Prometheus HTTP API

Go Reference

The api/prometheus directory contains the client for the Prometheus HTTP API. It allows you to write Go applications that query time series data from a Prometheus server. It is still in alpha stage.

Where is model, extraction, and text?

The model packages has been moved to prometheus/common/model.

The extraction and text packages are now contained in prometheus/common/expfmt.

Contributing and community

See the contributing guidelines and the Community section of the homepage.

client_golang community is also present on the CNCF Slack #prometheus-client_golang.

For Maintainers: Release Process

To cut a minor version:

  1. Create a new branch release-<major>.<minor> on top of the main commit you want to cut the version from and push it.
  2. Create a new branch on top of the release branch, e.g. <yourname>/cut-<major>.<minor>.<patch>,
  3. Change the VERSION file.
  4. Update CHANGELOG (only user-impacting changes to mention).
  5. Create PR, and get it reviewed.
  6. Once merged, create a release with the release-<major>.<minor> tag on GitHub with the <version> title.
  7. Announce on the prometheus-announce mailing list, slack and Twitter.
  8. Merge the release branch back to the main using the "merge without squashing" approach (!).

NOTE: In case of merge conflicts, you can checkout the release branch in a new branch, e.g. <yourname>/resolve-conflicts, fix the merge problems there, and then do a PR into main from the new branch. In that way, you still get all the commits in the release branch back into main, but leave the release branch alone.

To cut the patch version:

  1. Create a branch on top of the release branch you want to use.
  2. Cherry-pick the fixes from the main branch (or add new commits) to fix critical bugs for that patch release.
  3. Follow steps 3-8 as above.