CodeSampleX

示例

github.com/prometheus/client_model v0.6.1: go.Histogram

已验证示例 — golang github.com/prometheus/client_model v0.6.1: go.Histogram. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:4d8914a1da812d3dcf42ecc8c568464f4741f6e00f3a0b9a87de33f121e10cff

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify github.com/prometheus/client_model/go.Histogram in pkg:golang/github.com/prometheus/client_model@v0.6.1
包
符号
  • github.com/prometheus/client_model/go.Histogram
环境
go 1.26.6
创建时间
2026-09-05T14:10:19Z

契约

  1. dto.Histogram getters (GetSampleCount, GetSampleSum, GetBucket) return zero/nil values for nil and uninitialized instances.
  2. dto.Histogram stores and retrieves sample count, sample sum, and bucket slices.
  3. dto.Bucket getters (GetCumulativeCount, GetUpperBound, GetExemplar) return zero/nil values for nil instances and retrieve configured values.
  4. dto.Histogram.Reset clears all fields back to zero values.

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract_test.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_model/go.Histogram in pkg:golang/github.com/prometheus/client_model@v0.6.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/client_model@v0.6.1
Demonstrate these symbols/APIs:
  - github.com/prometheus/client_model/go.Histogram
Required runtime conditions:
  - ecosystem: golang
  - language: go
  - packageManager: go@1.26.6
  - runtime: go@1.26.6

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:2384306a0711c581c5347a5aeba6191533e6010e5de51e522db7deb5bb6bcb9d","contract":["dto.Histogram getters (GetSampleCount, GetSampleSum, GetBucket) return zero/nil values for nil and uninitialized instances.","dto.Histogram stores and retrieves sample count, sample sum, and bucket slices.","dto.Bucket getters (GetCumulativeCount, GetUpperBound, GetExemplar) return zero/nil values for nil instances and retrieve configured values.","dto.Histogram.Reset clears all fields back to zero values."],"goal":"verify github.com/prometheus/client_model/go.Histogram in pkg:golang/github.com/prometheus/client_model@v0.6.1","kind":"HOW","packages":["pkg:golang/github.com/prometheus/client_model@v0.6.1"],"schemaVersion":1,"symbols":["github.com/prometheus/client_model/go.Histogram"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/prometheus/client_model@v0.6.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/client_model@v0.6.1","symbols":["github.com/prometheus/client_model/go.Histogram"],"verifierAdapter":"golang@1"}
go.mod
module example.com/histogram

go 1.26.6

require (
	github.com/prometheus/client_model v0.6.1
	google.golang.org/protobuf v1.36.12
)
go.sum
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/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
main.go
package main

import (
	"fmt"

	dto "github.com/prometheus/client_model/go"
	"google.golang.org/protobuf/proto"
)

func main() {
	h := &dto.Histogram{
		SampleCount: proto.Uint64(10),
		SampleSum:   proto.Float64(25.5),
		Bucket: []*dto.Bucket{
			{
				CumulativeCount: proto.Uint64(4),
				UpperBound:      proto.Float64(2.5),
			},
			{
				CumulativeCount: proto.Uint64(10),
				UpperBound:      proto.Float64(10.0),
			},
		},
	}

	fmt.Printf("Sample count: %d\n", h.GetSampleCount())
	fmt.Printf("Sample sum: %f\n", h.GetSampleSum())
	fmt.Printf("Buckets count: %d\n", len(h.GetBucket()))
	for _, b := range h.GetBucket() {
		fmt.Printf("  le %f: %d\n", b.GetUpperBound(), b.GetCumulativeCount())
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_model/go.Histogram in pkg:golang/github.com/prometheus/client_model@v0.6.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/client_model@v0.6.1"
  ],
  "symbols": [
    "github.com/prometheus/client_model/go.Histogram"
  ],
  "runtimeConditions": {
    "ecosystem": "golang",
    "language": "go",
    "packageManager": "go@1.26.6",
    "runtime": "go@1.26.6"
  }
}
test/contract_test.go
package test

import (
	"testing"

	dto "github.com/prometheus/client_model/go"
	"google.golang.org/protobuf/proto"
)

func TestHistogramNilAndDefaultGetters(t *testing.T) {
	var nilHistogram *dto.Histogram
	if got := nilHistogram.GetSampleCount(); got != 0 {
		t.Errorf("nilHistogram.GetSampleCount() = %v, want 0", got)
	}
	if got := nilHistogram.GetSampleSum(); got != 0 {
		t.Errorf("nilHistogram.GetSampleSum() = %v, want 0", got)
	}
	if got := nilHistogram.GetBucket(); got != nil {
		t.Errorf("nilHistogram.GetBucket() = %v, want nil", got)
	}

	emptyHistogram := &dto.Histogram{}
	if got := emptyHistogram.GetSampleCount(); got != 0 {
		t.Errorf("emptyHistogram.GetSampleCount() = %v, want 0", got)
	}
	if got := emptyHistogram.GetSampleSum(); got != 0 {
		t.Errorf("emptyHistogram.GetSampleSum() = %v, want 0", got)
	}
	if got := emptyHistogram.GetBucket(); got != nil {
		t.Errorf("emptyHistogram.GetBucket() = %v, want nil", got)
	}
}

func TestHistogramValuesAndBuckets(t *testing.T) {
	b1 := &dto.Bucket{
		CumulativeCount: proto.Uint64(5),
		UpperBound:      proto.Float64(1.0),
	}
	b2 := &dto.Bucket{
		CumulativeCount: proto.Uint64(12),
		UpperBound:      proto.Float64(5.0),
	}

	h := &dto.Histogram{
		SampleCount: proto.Uint64(12),
		SampleSum:   proto.Float64(34.5),
		Bucket:      []*dto.Bucket{b1, b2},
	}

	if got := h.GetSampleCount(); got != 12 {
		t.Errorf("h.GetSampleCount() = %v, want 12", got)
	}
	if got := h.GetSampleSum(); got != 34.5 {
		t.Errorf("h.GetSampleSum() = %v, want 34.5", got)
	}
	buckets := h.GetBucket()
	if len(buckets) != 2 {
		t.Fatalf("len(h.GetBucket()) = %v, want 2", len(buckets))
	}
	if buckets[0].GetCumulativeCount() != 5 || buckets[0].GetUpperBound() != 1.0 {
		t.Errorf("bucket 0 mismatch: count=%v, upper=%v", buckets[0].GetCumulativeCount(), buckets[0].GetUpperBound())
	}
	if buckets[1].GetCumulativeCount() != 12 || buckets[1].GetUpperBound() != 5.0 {
		t.Errorf("bucket 1 mismatch: count=%v, upper=%v", buckets[1].GetCumulativeCount(), buckets[1].GetUpperBound())
	}
}

func TestBucketGetters(t *testing.T) {
	var nilBucket *dto.Bucket
	if got := nilBucket.GetCumulativeCount(); got != 0 {
		t.Errorf("nilBucket.GetCumulativeCount() = %v, want 0", got)
	}
	if got := nilBucket.GetUpperBound(); got != 0 {
		t.Errorf("nilBucket.GetUpperBound() = %v, want 0", got)
	}
	if got := nilBucket.GetExemplar(); got != nil {
		t.Errorf("nilBucket.GetExemplar() = %v, want nil", got)
	}

	b := &dto.Bucket{
		CumulativeCount: proto.Uint64(42),
		UpperBound:      proto.Float64(100.5),
	}
	if got := b.GetCumulativeCount(); got != 42 {
		t.Errorf("b.GetCumulativeCount() = %v, want 42", got)
	}
	if got := b.GetUpperBound(); got != 100.5 {
		t.Errorf("b.GetUpperBound() = %v, want 100.5", got)
	}
}

func TestHistogramReset(t *testing.T) {
	h := &dto.Histogram{
		SampleCount: proto.Uint64(10),
		SampleSum:   proto.Float64(50.0),
		Bucket: []*dto.Bucket{
			{CumulativeCount: proto.Uint64(10), UpperBound: proto.Float64(5.0)},
		},
	}
	h.Reset()
	if got := h.GetSampleCount(); got != 0 {
		t.Errorf("h.GetSampleCount() after Reset() = %v, want 0", got)
	}
	if got := h.GetSampleSum(); got != 0 {
		t.Errorf("h.GetSampleSum() after Reset() = %v, want 0", got)
	}
	if got := h.GetBucket(); got != nil {
		t.Errorf("h.GetBucket() after Reset() = %v, want nil", got)
	}
}

原始种子者

匿名