CodeSampleX

Ejemplo

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

Muestra verificada para golang github.com/prometheus/client_golang v1.19.1: prometheus.GaugeOpts. El contrato se ejecutó en go 1.26 · linux alpine/x64 …

sha256:98a6a76f77a83a248427adfc3d4e547958de85a4e72dc0b57d2ad497a4773e55

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
2
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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 FAIL compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-07

Caso

HOW
Objetivo
verify github.com/prometheus/client_golang/prometheus.GaugeOpts in pkg:golang/github.com/prometheus/client_golang@v1.19.1
Paquetes
Símbolos
  • github.com/prometheus/client_golang/prometheus.GaugeOpts
Creado
2026-08-26T13:14:55Z

Contrato

  1. prometheus.NewGauge creates a Gauge metric configured with Namespace, Subsystem, Name, Help, and ConstLabels via prometheus.GaugeOpts
  2. Gauge metric created with prometheus.GaugeOpts supports Set, Inc, Dec, Add, and Sub operations
  3. prometheus.NewGaugeVec creates a GaugeVec metric using prometheus.GaugeOpts and variable label names
  4. Gauge metrics created with prometheus.GaugeOpts can be registered to a prometheus.Registry and collected via Gather

Archivos

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

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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

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:6eb2e9114908a2df6269a58ab97429c8f53ea9a37d4346c8ed372f6d08371dfe","contract":["prometheus.NewGauge creates a Gauge metric configured with Namespace, Subsystem, Name, Help, and ConstLabels via prometheus.GaugeOpts","Gauge metric created with prometheus.GaugeOpts supports Set, Inc, Dec, Add, and Sub operations","prometheus.NewGaugeVec creates a GaugeVec metric using prometheus.GaugeOpts and variable label names","Gauge metrics created with prometheus.GaugeOpts can be registered to a prometheus.Registry and collected via Gather"],"goal":"verify github.com/prometheus/client_golang/prometheus.GaugeOpts 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.GaugeOpts"]},"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.GaugeOpts"],"verifierAdapter":"golang@1"}
gauge.go
package sample

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

// CreateMemoryGauge creates a Gauge metric using GaugeOpts with namespace, subsystem, and constant labels.
func CreateMemoryGauge(namespace, subsystem string) prometheus.Gauge {
	opts := prometheus.GaugeOpts{
		Namespace: namespace,
		Subsystem: subsystem,
		Name:      "memory_bytes",
		Help:      "Current allocated memory in bytes",
		ConstLabels: prometheus.Labels{
			"env": "production",
		},
	}
	return prometheus.NewGauge(opts)
}

// CreateTaskQueueGaugeVec creates a GaugeVec metric using GaugeOpts and variable labels.
func CreateTaskQueueGaugeVec(namespace string) *prometheus.GaugeVec {
	opts := prometheus.GaugeOpts{
		Namespace: namespace,
		Name:      "queue_length",
		Help:      "Number of items in the queue",
	}
	return prometheus.NewGaugeVec(opts, []string{"queue_name", "priority"})
}
go.mod
module example.com/sample

go 1.25.0

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

require (
	github.com/beorn7/perks v1.0.1 // indirect
	github.com/cespare/xxhash/v2 v2.3.0 // indirect
	github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
	github.com/prometheus/common v0.55.0 // indirect
	github.com/prometheus/procfs v0.21.1 // indirect
	golang.org/x/sys v0.47.0 // indirect
	google.golang.org/protobuf v1.36.11 // 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.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
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.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc=
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.21.1 h1:GljZCt+zSTS+NZq88cyQ1LjZ+RCHp3uVuabBWA5+OJI=
github.com/prometheus/procfs v0.21.1/go.mod h1:aB55Cww9pdSJVHk0hUf0inxWyyjPogFIjmHKYgMKmtY=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_golang/prometheus.GaugeOpts 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.GaugeOpts"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

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

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract tests passed successfully.")
}

func runTests() error {
	// Test 1: Gauge creation via GaugeOpts with Namespace, Subsystem, Name, Help, ConstLabels
	opts := prometheus.GaugeOpts{
		Namespace: "test_ns",
		Subsystem: "test_sub",
		Name:      "test_gauge",
		Help:      "Test gauge help string",
		ConstLabels: prometheus.Labels{
			"app": "sample",
		},
	}
	gauge := prometheus.NewGauge(opts)
	if gauge == nil {
		return fmt.Errorf("expected non-nil Gauge from GaugeOpts")
	}

	// Test 2: Gauge value manipulations (Set, Inc, Dec, Add, Sub)
	gauge.Set(100.5)
	var m dto.Metric
	if err := gauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write metric: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 100.5 {
		return fmt.Errorf("expected gauge value 100.5, got %v", val)
	}

	gauge.Inc()
	m.Reset()
	if err := gauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write metric after Inc: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 101.5 {
		return fmt.Errorf("expected gauge value 101.5 after Inc, got %v", val)
	}

	gauge.Dec()
	m.Reset()
	if err := gauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write metric after Dec: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 100.5 {
		return fmt.Errorf("expected gauge value 100.5 after Dec, got %v", val)
	}

	gauge.Add(25.0)
	m.Reset()
	if err := gauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write metric after Add: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 125.5 {
		return fmt.Errorf("expected gauge value 125.5 after Add, got %v", val)
	}

	gauge.Sub(50.5)
	m.Reset()
	if err := gauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write metric after Sub: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 75.0 {
		return fmt.Errorf("expected gauge value 75.0 after Sub, got %v", val)
	}

	// Test 3: GaugeVec created using GaugeOpts
	vecOpts := prometheus.GaugeOpts{
		Namespace: "service",
		Name:      "active_workers",
		Help:      "Number of active worker goroutines",
	}
	gaugeVec := prometheus.NewGaugeVec(vecOpts, []string{"worker_type"})
	gaugeVec.WithLabelValues("io").Set(10)
	gaugeVec.WithLabelValues("compute").Set(20)

	// Test 4: Registry registration and Gather verification
	registry := prometheus.NewRegistry()
	if err := registry.Register(gauge); err != nil {
		return fmt.Errorf("failed to register gauge: %w", err)
	}
	if err := registry.Register(gaugeVec); err != nil {
		return fmt.Errorf("failed to register gaugeVec: %w", err)
	}

	metricFamilies, err := registry.Gather()
	if err != nil {
		return fmt.Errorf("registry gather failed: %w", err)
	}
	if len(metricFamilies) != 2 {
		return fmt.Errorf("expected 2 metric families, got %d", len(metricFamilies))
	}

	// Verify sample helper functions
	memGauge := sample.CreateMemoryGauge("app", "core")
	memGauge.Set(1024)
	m.Reset()
	if err := memGauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write memory gauge: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 1024 {
		return fmt.Errorf("expected memGauge value 1024, got %v", val)
	}

	queueVec := sample.CreateTaskQueueGaugeVec("app")
	queueVec.WithLabelValues("emails", "high").Set(5)
	m.Reset()
	emailGauge, err := queueVec.GetMetricWithLabelValues("emails", "high")
	if err != nil {
		return fmt.Errorf("failed to get metric from queueVec: %w", err)
	}
	if err := emailGauge.Write(&m); err != nil {
		return fmt.Errorf("failed to write queue gauge: %w", err)
	}
	if val := m.GetGauge().GetValue(); val != 5 {
		return fmt.Errorf("expected queue gauge value 5, got %v", val)
	}

	return nil
}

Seeder de origen

anónimo