示例
github.com/prometheus/client_model v0.2.0
已验证示例 — golang github.com/prometheus/client_model v0.2.0. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: dto.Histogram and dto.Bucket getter methods…
sha256:0b492566541b5cf2f4c54d7319f2c29fe40422acd2d4d1a96a62145440e24f27
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
go linux 24 · ubuntu · glibc 2.39 x64 go go 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-28 |
案例
HOW- 目标
- verify pkg:golang/github.com/prometheus/client_model@v0.2.0
- 环境
- go
- 创建时间
- 2026-08-28T00:23:29Z
契约
- dto.Histogram and dto.Bucket getter methods return zero or nil for nil and empty instances.
- dto.Histogram initializes and accesses sample count, sample sum, and cumulative bucket distributions.
- dto.Histogram embeds within dto.Metric with labels and timestamp.
- dto.Histogram.Reset clears sample count, sample sum, and bucket collections back to zero values.
- dto.Histogram serializes and deserializes losslessly across protobuf wire format.
文件
- PROMPT.md
- csx.json
- go.mod
- go.sum
- histogram.go
- spec.json
- test/contract_test.go
源代码
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_model@v0.2.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/prometheus/client_model@v0.2.0
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.
{"case":{"caseId":"case:sha256:60877eb8b78431c832bc8e8e99be47832addcb7971483ab102381212a7fa8b03","contract":["dto.Histogram and dto.Bucket getter methods return zero or nil for nil and empty instances.","dto.Histogram initializes and accesses sample count, sample sum, and cumulative bucket distributions.","dto.Histogram embeds within dto.Metric with labels and timestamp.","dto.Histogram.Reset clears sample count, sample sum, and bucket collections back to zero values.","dto.Histogram serializes and deserializes losslessly across protobuf wire format."],"goal":"verify pkg:golang/github.com/prometheus/client_model@v0.2.0","kind":"HOW","packages":["pkg:golang/github.com/prometheus/client_model@v0.2.0"],"schemaVersion":1},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","executionContext":"go","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/prometheus/client_model@v0.2.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/client_model@v0.2.0","verifierAdapter":"golang@1"}
module example.com/histogram
go 1.22
require (
github.com/golang/protobuf v1.5.0
github.com/prometheus/client_model v0.2.0
)
require google.golang.org/protobuf v1.26.0-rc.1 // indirect
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1 h1:7QnIQpGRHE5RnLKnESfDoxm2dTapTZua5a0kS0A+VXQ=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
package histogram
import (
"github.com/golang/protobuf/proto"
dto "github.com/prometheus/client_model/go"
)
// BucketConfig holds cumulative count and upper bound configuration for a bucket.
type BucketConfig struct {
CumulativeCount uint64
UpperBound float64
}
// CreateHistogram creates a dto.Histogram with the specified sample count, sample sum, and buckets.
func CreateHistogram(sampleCount uint64, sampleSum float64, buckets []BucketConfig) *dto.Histogram {
bucketList := make([]*dto.Bucket, 0, len(buckets))
for _, b := range buckets {
bucketList = append(bucketList, &dto.Bucket{
CumulativeCount: proto.Uint64(b.CumulativeCount),
UpperBound: proto.Float64(b.UpperBound),
})
}
return &dto.Histogram{
SampleCount: proto.Uint64(sampleCount),
SampleSum: proto.Float64(sampleSum),
Bucket: bucketList,
}
}
// CreateHistogramMetric creates a dto.Metric wrapping a Histogram with labels and optional timestamp.
func CreateHistogramMetric(labels map[string]string, h *dto.Histogram, timestampMs int64) *dto.Metric {
var labelPairs []*dto.LabelPair
for k, v := range labels {
labelPairs = append(labelPairs, &dto.LabelPair{
Name: proto.String(k),
Value: proto.String(v),
})
}
m := &dto.Metric{
Label: labelPairs,
Histogram: h,
}
if timestampMs > 0 {
m.TimestampMs = proto.Int64(timestampMs)
}
return m
}
// RoundTripHistogram marshals and unmarshals a Histogram using protobuf.
func RoundTripHistogram(h *dto.Histogram) (*dto.Histogram, error) {
data, err := proto.Marshal(h)
if err != nil {
return nil, err
}
out := &dto.Histogram{}
if err := proto.Unmarshal(data, out); err != nil {
return nil, err
}
return out, nil
}
{
"schemaVersion": 1,
"goal": "verify pkg:golang/github.com/prometheus/client_model@v0.2.0",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/prometheus/client_model@v0.2.0"
]
}
package test
import (
"math"
"testing"
"github.com/golang/protobuf/proto"
dto "github.com/prometheus/client_model/go"
"example.com/histogram"
)
func TestHistogramNilAndDefaults(t *testing.T) {
var nilH *dto.Histogram
if got := nilH.GetSampleCount(); got != 0 {
t.Errorf("nilH.GetSampleCount() = %d, want 0", got)
}
if got := nilH.GetSampleSum(); got != 0 {
t.Errorf("nilH.GetSampleSum() = %f, want 0", got)
}
if got := nilH.GetBucket(); got != nil {
t.Errorf("nilH.GetBucket() = %v, want nil", got)
}
var nilB *dto.Bucket
if got := nilB.GetCumulativeCount(); got != 0 {
t.Errorf("nilB.GetCumulativeCount() = %d, want 0", got)
}
if got := nilB.GetUpperBound(); got != 0 {
t.Errorf("nilB.GetUpperBound() = %f, want 0", got)
}
emptyH := &dto.Histogram{}
if got := emptyH.GetSampleCount(); got != 0 {
t.Errorf("emptyH.GetSampleCount() = %d, want 0", got)
}
if got := emptyH.GetSampleSum(); got != 0 {
t.Errorf("emptyH.GetSampleSum() = %f, want 0", got)
}
if got := emptyH.GetBucket(); got != nil {
t.Errorf("emptyH.GetBucket() = %v, want nil", got)
}
}
func TestCreateHistogramAndGetters(t *testing.T) {
buckets := []histogram.BucketConfig{
{CumulativeCount: 10, UpperBound: 0.1},
{CumulativeCount: 25, UpperBound: 0.5},
{CumulativeCount: 30, UpperBound: 1.0},
{CumulativeCount: 30, UpperBound: math.Inf(1)},
}
sampleCount := uint64(30)
sampleSum := 12.345
h := histogram.CreateHistogram(sampleCount, sampleSum, buckets)
if got := h.GetSampleCount(); got != sampleCount {
t.Errorf("h.GetSampleCount() = %d, want %d", got, sampleCount)
}
if got := h.GetSampleSum(); got != sampleSum {
t.Errorf("h.GetSampleSum() = %f, want %f", got, sampleSum)
}
bList := h.GetBucket()
if len(bList) != len(buckets) {
t.Fatalf("len(h.GetBucket()) = %d, want %d", len(bList), len(buckets))
}
for i, want := range buckets {
if got := bList[i].GetCumulativeCount(); got != want.CumulativeCount {
t.Errorf("bucket[%d].GetCumulativeCount() = %d, want %d", i, got, want.CumulativeCount)
}
if got := bList[i].GetUpperBound(); got != want.UpperBound {
t.Errorf("bucket[%d].GetUpperBound() = %f, want %f", i, got, want.UpperBound)
}
}
}
func TestHistogramMetricEmbedding(t *testing.T) {
buckets := []histogram.BucketConfig{
{CumulativeCount: 5, UpperBound: 100.0},
{CumulativeCount: 10, UpperBound: 500.0},
}
h := histogram.CreateHistogram(10, 2500.0, buckets)
labels := map[string]string{"le": "100", "path": "/api/v1"}
ts := int64(1700000000000)
metric := histogram.CreateHistogramMetric(labels, h, ts)
if metric.GetHistogram() == nil {
t.Fatal("metric.GetHistogram() returned nil")
}
if metric.GetHistogram().GetSampleCount() != 10 {
t.Errorf("sample count = %d, want 10", metric.GetHistogram().GetSampleCount())
}
if metric.GetTimestampMs() != ts {
t.Errorf("timestamp = %d, want %d", metric.GetTimestampMs(), ts)
}
}
func TestHistogramReset(t *testing.T) {
h := &dto.Histogram{
SampleCount: proto.Uint64(100),
SampleSum: proto.Float64(500.5),
Bucket: []*dto.Bucket{
{
CumulativeCount: proto.Uint64(100),
UpperBound: proto.Float64(1.0),
},
},
}
h.Reset()
if got := h.GetSampleCount(); got != 0 {
t.Errorf("after Reset(), h.GetSampleCount() = %d, want 0", got)
}
if got := h.GetSampleSum(); got != 0 {
t.Errorf("after Reset(), h.GetSampleSum() = %f, want 0", got)
}
if got := h.GetBucket(); got != nil {
t.Errorf("after Reset(), h.GetBucket() = %v, want nil", got)
}
}
func TestHistogramProtobufRoundTrip(t *testing.T) {
buckets := []histogram.BucketConfig{
{CumulativeCount: 3, UpperBound: 0.05},
{CumulativeCount: 8, UpperBound: 0.25},
{CumulativeCount: 15, UpperBound: 1.0},
}
original := histogram.CreateHistogram(15, 7.89, buckets)
deserialized, err := histogram.RoundTripHistogram(original)
if err != nil {
t.Fatalf("RoundTripHistogram failed: %v", err)
}
if deserialized.GetSampleCount() != original.GetSampleCount() {
t.Errorf("deserialized SampleCount = %d, want %d", deserialized.GetSampleCount(), original.GetSampleCount())
}
if deserialized.GetSampleSum() != original.GetSampleSum() {
t.Errorf("deserialized SampleSum = %f, want %f", deserialized.GetSampleSum(), original.GetSampleSum())
}
if len(deserialized.GetBucket()) != len(original.GetBucket()) {
t.Fatalf("deserialized Bucket length = %d, want %d", len(deserialized.GetBucket()), len(original.GetBucket()))
}
for i := range original.GetBucket() {
if deserialized.GetBucket()[i].GetCumulativeCount() != original.GetBucket()[i].GetCumulativeCount() {
t.Errorf("bucket[%d] count mismatch", i)
}
if deserialized.GetBucket()[i].GetUpperBound() != original.GetBucket()[i].GetUpperBound() {
t.Errorf("bucket[%d] upper bound mismatch", i)
}
}
}
原始种子者
匿名