CodeSampleX

Beispiel

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

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

sha256:70fe550e51f669b2bc8ecde054f3dfcd4d77132710c1e45a78b65c44b4bef1a3

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 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

Fall

HOW
Ziel
verify github.com/prometheus/client_model/go.Metric in pkg:golang/github.com/prometheus/client_model@v0.6.1
Pakete
Symbole
  • github.com/prometheus/client_model/go.Metric
Umgebung
go
Erstellt
2026-09-05T10:30:58Z

Contract

  1. dto.Metric getter methods (GetLabel, GetGauge, GetCounter, GetSummary, GetUntyped, GetHistogram, GetTimestampMs) return nil or zero for nil and empty instances.
  2. dto.Metric stores and retrieves labels, gauge metric value, and timestamp in milliseconds.
  3. dto.Metric stores and retrieves counter metric value.
  4. dto.Metric.Reset clears all fields back to zero values.

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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 github.com/prometheus/client_model/go.Metric 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.Metric

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:aea026c1bb42221c894415996ca6398176257eda48b8c0420a4e5e14d5c0b51d","contract":["dto.Metric getter methods (GetLabel, GetGauge, GetCounter, GetSummary, GetUntyped, GetHistogram, GetTimestampMs) return nil or zero for nil and empty instances.","dto.Metric stores and retrieves labels, gauge metric value, and timestamp in milliseconds.","dto.Metric stores and retrieves counter metric value.","dto.Metric.Reset clears all fields back to zero values."],"goal":"verify github.com/prometheus/client_model/go.Metric 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.Metric"]},"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.6.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/client_model@v0.6.1","symbols":["github.com/prometheus/client_model/go.Metric"],"verifierAdapter":"golang@1"}
go.mod
module example.com/metric

go 1.22

require (
	github.com/prometheus/client_model v0.6.1
	google.golang.org/protobuf v1.34.2
)
go.sum
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.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
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.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
main.go
package main

import (
	"fmt"

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

func main() {
	labelName := "method"
	labelVal := "GET"
	gaugeVal := 123.45
	tsMs := int64(1700000000000)

	metric := &dto.Metric{
		Label: []*dto.LabelPair{
			{
				Name:  proto.String(labelName),
				Value: proto.String(labelVal),
			},
		},
		Gauge: &dto.Gauge{
			Value: proto.Float64(gaugeVal),
		},
		TimestampMs: proto.Int64(tsMs),
	}

	fmt.Printf("Metric label count: %d\n", len(metric.GetLabel()))
	if len(metric.GetLabel()) > 0 {
		fmt.Printf("Metric label: %s=%s\n", metric.GetLabel()[0].GetName(), metric.GetLabel()[0].GetValue())
	}
	if g := metric.GetGauge(); g != nil {
		fmt.Printf("Metric gauge value: %f\n", g.GetValue())
	}
	fmt.Printf("Metric timestamp ms: %d\n", metric.GetTimestampMs())

	metric.Reset()
	fmt.Printf("After reset, labels count: %d\n", len(metric.GetLabel()))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_model/go.Metric 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.Metric"
  ]
}
test/contract_test.go
package test

import (
	"testing"

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

func TestMetricNilAndDefaults(t *testing.T) {
	var nilMetric *dto.Metric
	if got := nilMetric.GetLabel(); got != nil {
		t.Errorf("nilMetric.GetLabel() = %v, want nil", got)
	}
	if got := nilMetric.GetGauge(); got != nil {
		t.Errorf("nilMetric.GetGauge() = %v, want nil", got)
	}
	if got := nilMetric.GetCounter(); got != nil {
		t.Errorf("nilMetric.GetCounter() = %v, want nil", got)
	}
	if got := nilMetric.GetSummary(); got != nil {
		t.Errorf("nilMetric.GetSummary() = %v, want nil", got)
	}
	if got := nilMetric.GetUntyped(); got != nil {
		t.Errorf("nilMetric.GetUntyped() = %v, want nil", got)
	}
	if got := nilMetric.GetHistogram(); got != nil {
		t.Errorf("nilMetric.GetHistogram() = %v, want nil", got)
	}
	if got := nilMetric.GetTimestampMs(); got != 0 {
		t.Errorf("nilMetric.GetTimestampMs() = %v, want 0", got)
	}

	emptyMetric := &dto.Metric{}
	if got := emptyMetric.GetLabel(); got != nil {
		t.Errorf("emptyMetric.GetLabel() = %v, want nil", got)
	}
	if got := emptyMetric.GetGauge(); got != nil {
		t.Errorf("emptyMetric.GetGauge() = %v, want nil", got)
	}
	if got := emptyMetric.GetTimestampMs(); got != 0 {
		t.Errorf("emptyMetric.GetTimestampMs() = %v, want 0", got)
	}
}

func TestMetricWithLabelsAndGauge(t *testing.T) {
	labelName := "handler"
	labelVal := "prometheus"
	gaugeVal := 99.5
	ts := int64(1690000000123)

	metric := &dto.Metric{
		Label: []*dto.LabelPair{
			{
				Name:  proto.String(labelName),
				Value: proto.String(labelVal),
			},
		},
		Gauge: &dto.Gauge{
			Value: proto.Float64(gaugeVal),
		},
		TimestampMs: proto.Int64(ts),
	}

	labels := metric.GetLabel()
	if len(labels) != 1 {
		t.Fatalf("len(metric.GetLabel()) = %d, want 1", len(labels))
	}
	if labels[0].GetName() != labelName || labels[0].GetValue() != labelVal {
		t.Errorf("label mismatch: got name=%s val=%s, want name=%s val=%s",
			labels[0].GetName(), labels[0].GetValue(), labelName, labelVal)
	}

	gauge := metric.GetGauge()
	if gauge == nil {
		t.Fatal("metric.GetGauge() returned nil")
	}
	if gauge.GetValue() != gaugeVal {
		t.Errorf("gauge.GetValue() = %v, want %v", gauge.GetValue(), gaugeVal)
	}

	if metric.GetTimestampMs() != ts {
		t.Errorf("metric.GetTimestampMs() = %v, want %v", metric.GetTimestampMs(), ts)
	}
}

func TestMetricWithCounter(t *testing.T) {
	counterVal := 1500.0
	metric := &dto.Metric{
		Counter: &dto.Counter{
			Value: proto.Float64(counterVal),
		},
	}

	counter := metric.GetCounter()
	if counter == nil {
		t.Fatal("metric.GetCounter() returned nil")
	}
	if counter.GetValue() != counterVal {
		t.Errorf("counter.GetValue() = %v, want %v", counter.GetValue(), counterVal)
	}
}

func TestMetricReset(t *testing.T) {
	metric := &dto.Metric{
		Label: []*dto.LabelPair{
			{
				Name:  proto.String("service"),
				Value: proto.String("web"),
			},
		},
		Gauge: &dto.Gauge{
			Value: proto.Float64(10.0),
		},
		TimestampMs: proto.Int64(123456789),
	}

	metric.Reset()

	if got := metric.GetLabel(); got != nil {
		t.Errorf("after Reset(), metric.GetLabel() = %v, want nil", got)
	}
	if got := metric.GetGauge(); got != nil {
		t.Errorf("after Reset(), metric.GetGauge() = %v, want nil", got)
	}
	if got := metric.GetTimestampMs(); got != 0 {
		t.Errorf("after Reset(), metric.GetTimestampMs() = %v, want 0", got)
	}
}

Ursprungs-Seeder

anonym