CodeSampleX

示例

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

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

sha256:2a009002386b334e47b046ad31ec3cd794d786f10c0fa0c7010439f85302a227

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-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

案例

HOW
目标
verify github.com/prometheus/client_golang/prometheus.NewRegistry in pkg:golang/github.com/prometheus/client_golang@v1.19.1
包
符号
  • github.com/prometheus/client_golang/prometheus.NewRegistry
创建时间
2026-08-26T13:38:15Z

契约

  1. prometheus.NewRegistry creates an isolated non-nil metric registry
  2. prometheus.NewRegistry allows registering and unregistering collectors independently
  3. prometheus.NewRegistry returns AlreadyRegisteredError on duplicate collector registration
  4. prometheus.NewRegistry gathers registered metrics without global registry pollution

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • registry.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 github.com/prometheus/client_golang/prometheus.NewRegistry 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.NewRegistry

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:32068ccc4a2cff6954d6f0dfd8cfee3392cac4c175e3a53bc3618a7e55951f51","contract":["prometheus.NewRegistry creates an isolated non-nil metric registry","prometheus.NewRegistry allows registering and unregistering collectors independently","prometheus.NewRegistry returns AlreadyRegisteredError on duplicate collector registration","prometheus.NewRegistry gathers registered metrics without global registry pollution"],"goal":"verify github.com/prometheus/client_golang/prometheus.NewRegistry 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.NewRegistry"]},"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.NewRegistry"],"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=
registry.go
package sample

import (
	"errors"

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

// RegistryManager encapsulates a dedicated Prometheus registry for isolated metrics collection.
type RegistryManager struct {
	reg *prometheus.Registry
}

// NewRegistryManager initializes a new isolated Prometheus registry.
func NewRegistryManager() *RegistryManager {
	return &RegistryManager{
		reg: prometheus.NewRegistry(),
	}
}

// Registry returns the underlying prometheus.Registry instance.
func (m *RegistryManager) Registry() *prometheus.Registry {
	return m.reg
}

// RegisterCollector registers a collector into the isolated registry.
func (m *RegistryManager) RegisterCollector(c prometheus.Collector) error {
	return m.reg.Register(c)
}

// UnregisterCollector removes a collector from the isolated registry.
func (m *RegistryManager) UnregisterCollector(c prometheus.Collector) bool {
	return m.reg.Unregister(c)
}

// IsAlreadyRegistered checks if an error indicates a duplicate metric registration.
func IsAlreadyRegistered(err error) bool {
	var are prometheus.AlreadyRegisteredError
	return errors.As(err, &are)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_golang/prometheus.NewRegistry 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.NewRegistry"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

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

func main() {
	// 1. Verify prometheus.NewRegistry creates an isolated non-nil metric registry
	mgr := sample.NewRegistryManager()
	if mgr == nil || mgr.Registry() == nil {
		fmt.Fprintf(os.Stderr, "failed: prometheus.NewRegistry returned nil\n")
		os.Exit(1)
	}

	// Create a second isolated registry to prove instance separation
	mgr2 := sample.NewRegistryManager()
	if mgr2.Registry() == mgr.Registry() {
		fmt.Fprintf(os.Stderr, "failed: NewRegistry returned identical pointers\n")
		os.Exit(1)
	}

	counter1 := prometheus.NewCounter(prometheus.CounterOpts{
		Name: "sample_ops_total",
		Help: "Total number of operations.",
	})
	counter2 := prometheus.NewCounter(prometheus.CounterOpts{
		Name: "sample_ops_total",
		Help: "Total number of operations.",
	})

	// 2. Verify registering collectors independently
	if err := mgr.RegisterCollector(counter1); err != nil {
		fmt.Fprintf(os.Stderr, "failed to register counter1 in mgr: %v\n", err)
		os.Exit(1)
	}

	if err := mgr2.RegisterCollector(counter2); err != nil {
		fmt.Fprintf(os.Stderr, "failed to register counter2 in mgr2: %v\n", err)
		os.Exit(1)
	}

	// 3. Verify duplicate registration returns AlreadyRegisteredError
	dupErr := mgr.RegisterCollector(counter1)
	if dupErr == nil {
		fmt.Fprintf(os.Stderr, "expected error on duplicate registration, got nil\n")
		os.Exit(1)
	}
	if !sample.IsAlreadyRegistered(dupErr) {
		fmt.Fprintf(os.Stderr, "expected AlreadyRegisteredError, got %T: %v\n", dupErr, dupErr)
		os.Exit(1)
	}

	// 4. Verify unregistering collector
	if !mgr.UnregisterCollector(counter1) {
		fmt.Fprintf(os.Stderr, "expected Unregister to return true\n")
		os.Exit(1)
	}
	if mgr.UnregisterCollector(counter1) {
		fmt.Fprintf(os.Stderr, "expected second Unregister to return false\n")
		os.Exit(1)
	}

	// Re-register counter1
	if err := mgr.RegisterCollector(counter1); err != nil {
		fmt.Fprintf(os.Stderr, "failed to re-register counter1: %v\n", err)
		os.Exit(1)
	}

	counter1.Inc()

	// 5. Verify Gather collects metrics without pollution
	mfs1, err := mgr.Registry().Gather()
	if err != nil {
		fmt.Fprintf(os.Stderr, "mgr.Gather failed: %v\n", err)
		os.Exit(1)
	}
	if len(mfs1) != 1 {
		fmt.Fprintf(os.Stderr, "expected 1 metric family in mgr, got %d\n", len(mfs1))
		os.Exit(1)
	}
	if mfs1[0].GetName() != "sample_ops_total" || mfs1[0].GetType() != dto.MetricType_COUNTER {
		fmt.Fprintf(os.Stderr, "unexpected metric family in mgr: %v\n", mfs1[0])
		os.Exit(1)
	}
	if len(mfs1[0].GetMetric()) != 1 || mfs1[0].GetMetric()[0].GetCounter().GetValue() != 1 {
		fmt.Fprintf(os.Stderr, "unexpected counter value in mgr\n")
		os.Exit(1)
	}

	// Verify mgr2 gather has counter2 value 0
	mfs2, err := mgr2.Registry().Gather()
	if err != nil {
		fmt.Fprintf(os.Stderr, "mgr2.Gather failed: %v\n", err)
		os.Exit(1)
	}
	if len(mfs2) != 1 || mfs2[0].GetMetric()[0].GetCounter().GetValue() != 0 {
		fmt.Fprintf(os.Stderr, "unexpected counter value in mgr2\n")
		os.Exit(1)
	}

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

原始种子者

匿名