CodeSampleX

Beispiel

github.com/prometheus/client_golang v1.19.1: prometheus.Gauge

Verifiziertes Beispiel für golang github.com/prometheus/client_golang v1.19.1: prometheus.Gauge. Der Vertrag lief auf go 1.26 · linux alpine/x64 · docker und…

sha256:3fd1e7b659e0ae90305c6aa1e52393bd76ec2b1e001fa0667e1dfe1d6cbebfbc

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
2
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-26
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-07

Fall

HOW
Ziel
verify github.com/prometheus/client_golang/prometheus.Gauge in pkg:golang/github.com/prometheus/client_golang@v1.19.1
Pakete
Symbole
  • github.com/prometheus/client_golang/prometheus.Gauge
Erstellt
2026-08-26T13:11:29Z

Contract

  1. prometheus.NewGauge creates a Gauge metric with GaugeOpts (Name, Help, ConstLabels)
  2. Gauge.Set sets the metric to an arbitrary float64 value
  3. Gauge.Inc increments the metric value by 1.0
  4. Gauge.Dec decrements the metric value by 1.0
  5. Gauge.Add adds a float64 amount to the current metric value
  6. Gauge.Sub subtracts a float64 amount from the current metric value
  7. Gauge.SetToCurrentTime sets the metric value to the current unix epoch timestamp in seconds
  8. prometheus.Registry registers a Gauge and gathers metric families offline

Dateien

  • PROMPT.md
  • csx.json
  • gauge.go
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go

Quellartefakt herunterladen (tar.gz)

Quelltext

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/prometheus/client_golang/prometheus.Gauge in pkg:golang/github.com/prometheus/client_golang@v1.19.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/client_golang@v1.19.1
Demonstrate these symbols/APIs:
  - github.com/prometheus/client_golang/prometheus.Gauge

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:fa6f778b69d8f656035512b9cd8c895e83f100df74df978c3d69710bb4e9a04e","contract":["prometheus.NewGauge creates a Gauge metric with GaugeOpts (Name, Help, ConstLabels)","Gauge.Set sets the metric to an arbitrary float64 value","Gauge.Inc increments the metric value by 1.0","Gauge.Dec decrements the metric value by 1.0","Gauge.Add adds a float64 amount to the current metric value","Gauge.Sub subtracts a float64 amount from the current metric value","Gauge.SetToCurrentTime sets the metric value to the current unix epoch timestamp in seconds","prometheus.Registry registers a Gauge and gathers metric families offline"],"goal":"verify github.com/prometheus/client_golang/prometheus.Gauge in pkg:golang/github.com/prometheus/client_golang@v1.19.1","kind":"HOW","packages":["pkg:golang/github.com/prometheus/client_golang@v1.19.1"],"schemaVersion":1,"symbols":["github.com/prometheus/client_golang/prometheus.Gauge"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/prometheus/client_golang@v1.19.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/client_golang@v1.19.1","symbols":["github.com/prometheus/client_golang/prometheus.Gauge"],"verifierAdapter":"golang@1"}
gauge.go
package sample

import (
	"errors"

	"github.com/prometheus/client_golang/prometheus"
	dto "github.com/prometheus/client_model/go"
)

// NewTemperatureGauge creates and returns a new prometheus.Gauge metric with given options.
func NewTemperatureGauge(name, help string, constLabels prometheus.Labels) prometheus.Gauge {
	return prometheus.NewGauge(prometheus.GaugeOpts{
		Name:        name,
		Help:        help,
		ConstLabels: constLabels,
	})
}

// AdjustGauge demonstrates various Gauge manipulation methods: Set, Inc, Dec, Add, Sub.
func AdjustGauge(g prometheus.Gauge, initial float64) {
	g.Set(initial)
	g.Inc()
	g.Add(5.5)
	g.Dec()
	g.Sub(2.5)
}

// SetCurrentTime sets the Gauge value to the current unix epoch timestamp in seconds.
func SetCurrentTime(g prometheus.Gauge) {
	g.SetToCurrentTime()
}

// ReadGaugeValue extracts the float64 value directly from the Gauge metric offline via Write.
func ReadGaugeValue(g prometheus.Gauge) (float64, error) {
	var m dto.Metric
	if err := g.Write(&m); err != nil {
		return 0, err
	}
	gauge := m.GetGauge()
	if gauge == nil {
		return 0, errors.New("metric is not a gauge")
	}
	return gauge.GetValue(), nil
}

// RegisterAndGather registers the gauge to a custom registry and gathers the metric value.
func RegisterAndGather(registry *prometheus.Registry, g prometheus.Gauge, metricName string) (float64, error) {
	if err := registry.Register(g); err != nil {
		return 0, err
	}
	mfs, err := registry.Gather()
	if err != nil {
		return 0, err
	}
	for _, mf := range mfs {
		if mf.GetName() == metricName {
			for _, m := range mf.GetMetric() {
				if gauge := m.GetGauge(); gauge != nil {
					return gauge.GetValue(), nil
				}
			}
		}
	}
	return 0, errors.New("metric not found in gathered families")
}
go.mod
module example.com/prometheus-gauge-sample

go 1.21

require (
	github.com/prometheus/client_golang v1.19.1
	github.com/prometheus/client_model v0.5.0
)

require (
	github.com/beorn7/perks v1.0.1 // indirect
	github.com/cespare/xxhash/v2 v2.2.0 // indirect
	github.com/prometheus/common v0.48.0 // indirect
	github.com/prometheus/procfs v0.12.0 // indirect
	golang.org/x/sys v0.17.0 // indirect
	google.golang.org/protobuf v1.33.0 // indirect
)
go.sum
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_golang/prometheus.Gauge in pkg:golang/github.com/prometheus/client_golang@v1.19.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/client_golang@v1.19.1"
  ],
  "symbols": [
    "github.com/prometheus/client_golang/prometheus.Gauge"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"math"
	"os"
	"time"

	"example.com/prometheus-gauge-sample"
	"github.com/prometheus/client_golang/prometheus"
)

func main() {
	fmt.Println("Running prometheus.Gauge contract verification...")

	// 1. Creation with GaugeOpts
	labels := prometheus.Labels{"sensor": "temp_1", "unit": "celsius"}
	gauge := sample.NewTemperatureGauge("room_temperature_celsius", "Current room temperature in Celsius", labels)
	if gauge == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected non-nil Gauge instance\n")
		os.Exit(1)
	}

	// 2. Set
	gauge.Set(25.0)
	val, err := sample.ReadGaugeValue(gauge)
	if err != nil || math.Abs(val-25.0) > 1e-6 {
		fmt.Fprintf(os.Stderr, "FAIL: Gauge.Set(25.0) got %v (err: %v)\n", val, err)
		os.Exit(1)
	}

	// 3. Inc
	gauge.Inc()
	val, err = sample.ReadGaugeValue(gauge)
	if err != nil || math.Abs(val-26.0) > 1e-6 {
		fmt.Fprintf(os.Stderr, "FAIL: Gauge.Inc() got %v (err: %v)\n", val, err)
		os.Exit(1)
	}

	// 4. Dec
	gauge.Dec()
	val, err = sample.ReadGaugeValue(gauge)
	if err != nil || math.Abs(val-25.0) > 1e-6 {
		fmt.Fprintf(os.Stderr, "FAIL: Gauge.Dec() got %v (err: %v)\n", val, err)
		os.Exit(1)
	}

	// 5. Add
	gauge.Add(10.5)
	val, err = sample.ReadGaugeValue(gauge)
	if err != nil || math.Abs(val-35.5) > 1e-6 {
		fmt.Fprintf(os.Stderr, "FAIL: Gauge.Add(10.5) got %v (err: %v)\n", val, err)
		os.Exit(1)
	}

	// 6. Sub
	gauge.Sub(5.5)
	val, err = sample.ReadGaugeValue(gauge)
	if err != nil || math.Abs(val-30.0) > 1e-6 {
		fmt.Fprintf(os.Stderr, "FAIL: Gauge.Sub(5.5) got %v (err: %v)\n", val, err)
		os.Exit(1)
	}

	// 7. SetToCurrentTime
	before := float64(time.Now().Unix())
	sample.SetCurrentTime(gauge)
	after := float64(time.Now().Unix())
	val, err = sample.ReadGaugeValue(gauge)
	if err != nil || val < before || val > after+1 {
		fmt.Fprintf(os.Stderr, "FAIL: Gauge.SetToCurrentTime() got %v (expected [%v, %v])\n", val, before, after)
		os.Exit(1)
	}

	// 8. Register and Gather
	reg := prometheus.NewRegistry()
	freshGauge := prometheus.NewGauge(prometheus.GaugeOpts{
		Name: "system_cpu_usage_ratio",
		Help: "Current CPU usage ratio",
	})
	freshGauge.Set(0.75)
	gatheredVal, err := sample.RegisterAndGather(reg, freshGauge, "system_cpu_usage_ratio")
	if err != nil || math.Abs(gatheredVal-0.75) > 1e-6 {
		fmt.Fprintf(os.Stderr, "FAIL: RegisterAndGather got %v (err: %v)\n", gatheredVal, err)
		os.Exit(1)
	}

	fmt.Println("PASS: all prometheus.Gauge contract assertions passed successfully.")
}

Ursprungs-Seeder

anonym