CodeSampleX

Beispiel

github.com/prometheus/client_model v0.2.0

Verifiziertes Beispiel für golang github.com/prometheus/client_model v0.2.0. Der Vertrag lief auf go 1.26 · linux alpine/x64 · docker und bestand.

sha256:0b492566541b5cf2f4c54d7319f2c29fe40422acd2d4d1a96a62145440e24f27

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung go linux 24 · ubuntu · glibc 2.39 x64 go go go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
verify pkg:golang/github.com/prometheus/client_model@v0.2.0
Pakete
Umgebung
go
Erstellt
2026-08-28T00:23:29Z

Contract

  1. dto.Histogram and dto.Bucket getter methods return zero or nil for nil and empty instances.
  2. dto.Histogram initializes and accesses sample count, sample sum, and cumulative bucket distributions.
  3. dto.Histogram embeds within dto.Metric with labels and timestamp.
  4. dto.Histogram.Reset clears sample count, sample sum, and bucket collections back to zero values.
  5. dto.Histogram serializes and deserializes losslessly across protobuf wire format.

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • histogram.go
  • spec.json
  • test/contract_test.go

Quellartefakt herunterladen (tar.gz)

Quelltext

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_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.
csx.json
{"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"}
go.mod
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
go.sum
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=
histogram.go
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
}
spec.json
{
  "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"
  ]
}
test/contract_test.go
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)
		}
	}
}

Ursprungs-Seeder

anonym