CodeSampleX

示例

github.com/prometheus/client_golang v1.19.1

已验证示例 — golang github.com/prometheus/client_golang v1.19.1. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: prometheus.NewRegistry creates a distinct…

sha256:0b22cb395ef23743a8650b3be580f82aef4d7cd6e9b2a22781363736619b0078

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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-23
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

案例

HOW
目标
verify pkg:golang/github.com/prometheus/client_golang@v1.19.1
包
创建时间
2026-08-23T17:00:36Z

契约

  1. prometheus.NewRegistry creates a distinct non-nil metric registry
  2. prometheus.NewCounterVec increments labeled counter metrics and gathers them
  3. prometheus.NewGauge records and updates gauge values in the registry
  4. prometheus.NewHistogram records observations with bucket counts and sum

文件

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

下载源代码构件 (tar.gz)

源代码

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 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
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:2e08c96909016ff89cb8a562437d0dca52a1be6972513646c71e5a4730575336","contract":["prometheus.NewRegistry creates a distinct non-nil metric registry","prometheus.NewCounterVec increments labeled counter metrics and gathers them","prometheus.NewGauge records and updates gauge values in the registry","prometheus.NewHistogram records observations with bucket counts and sum"],"goal":"verify 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},"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","verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

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=
metrics.go
package sample

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

// Metrics holds prometheus collectors registered to a custom registry.
type Metrics struct {
	Registry       *prometheus.Registry
	RequestCounter *prometheus.CounterVec
	ActiveGauge    prometheus.Gauge
	DurationHist   prometheus.Histogram
}

// NewMetrics creates and registers custom Prometheus metrics with a dedicated registry.
func NewMetrics() *Metrics {
	reg := prometheus.NewRegistry()

	counter := prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "http_requests_total",
			Help: "Total number of HTTP requests processed.",
		},
		[]string{"method", "status"},
	)

	gauge := prometheus.NewGauge(
		prometheus.GaugeOpts{
			Name: "current_active_workers",
			Help: "Current number of active workers.",
		},
	)

	hist := prometheus.NewHistogram(
		prometheus.HistogramOpts{
			Name:    "task_duration_seconds",
			Help:    "Duration of tasks in seconds.",
			Buckets: []float64{0.1, 0.5, 1.0, 5.0},
		},
	)

	reg.MustRegister(counter, gauge, hist)

	return &Metrics{
		Registry:       reg,
		RequestCounter: counter,
		ActiveGauge:    gauge,
		DurationHist:   hist,
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/prometheus/client_golang@v1.19.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/client_golang@v1.19.1"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"

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

func main() {
	m := sample.NewMetrics()
	if m == nil || m.Registry == nil {
		fmt.Fprintf(os.Stderr, "failed to initialize metrics registry\n")
		os.Exit(1)
	}

	// 1. Record metric values
	m.RequestCounter.WithLabelValues("GET", "200").Inc()
	m.RequestCounter.WithLabelValues("POST", "201").Add(2)
	m.ActiveGauge.Set(42)
	m.DurationHist.Observe(0.35)

	// 2. Gather metrics from the registry
	mfs, err := m.Registry.Gather()
	if err != nil {
		fmt.Fprintf(os.Stderr, "registry.Gather failed: %v\n", err)
		os.Exit(1)
	}

	if len(mfs) != 3 {
		fmt.Fprintf(os.Stderr, "expected 3 metric families, got %d\n", len(mfs))
		os.Exit(1)
	}

	foundMap := make(map[string]*dto.MetricFamily)
	for _, mf := range mfs {
		foundMap[mf.GetName()] = mf
	}

	// 3. Verify counter family
	counterMF, ok := foundMap["http_requests_total"]
	if !ok {
		fmt.Fprintf(os.Stderr, "missing http_requests_total metric\n")
		os.Exit(1)
	}
	if counterMF.GetType() != dto.MetricType_COUNTER {
		fmt.Fprintf(os.Stderr, "expected COUNTER type, got %v\n", counterMF.GetType())
		os.Exit(1)
	}
	if len(counterMF.GetMetric()) != 2 {
		fmt.Fprintf(os.Stderr, "expected 2 counter metric instances, got %d\n", len(counterMF.GetMetric()))
		os.Exit(1)
	}

	// 4. Verify gauge family
	gaugeMF, ok := foundMap["current_active_workers"]
	if !ok {
		fmt.Fprintf(os.Stderr, "missing current_active_workers metric\n")
		os.Exit(1)
	}
	if gaugeMF.GetType() != dto.MetricType_GAUGE {
		fmt.Fprintf(os.Stderr, "expected GAUGE type, got %v\n", gaugeMF.GetType())
		os.Exit(1)
	}
	if len(gaugeMF.GetMetric()) != 1 || gaugeMF.GetMetric()[0].GetGauge().GetValue() != 42 {
		fmt.Fprintf(os.Stderr, "expected gauge value 42\n")
		os.Exit(1)
	}

	// 5. Verify histogram family
	histMF, ok := foundMap["task_duration_seconds"]
	if !ok {
		fmt.Fprintf(os.Stderr, "missing task_duration_seconds metric\n")
		os.Exit(1)
	}
	if histMF.GetType() != dto.MetricType_HISTOGRAM {
		fmt.Fprintf(os.Stderr, "expected HISTOGRAM type, got %v\n", histMF.GetType())
		os.Exit(1)
	}
	hist := histMF.GetMetric()[0].GetHistogram()
	if hist.GetSampleCount() != 1 || hist.GetSampleSum() != 0.35 {
		fmt.Fprintf(os.Stderr, "expected sample count 1 and sum 0.35, got %d, %f\n", hist.GetSampleCount(), hist.GetSampleSum())
		os.Exit(1)
	}

	fmt.Println("All prometheus client_golang contract assertions passed.")
}

原始种子者

匿名