CodeSampleX

Beispiel

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

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

sha256:4f0fc2fd7c90db644eb131b2ab5d47b108aa4eb0d205bd307b3a7ca62460fdd3

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

Fall

HOW
Ziel
verify github.com/prometheus/client_model/go.Metric in pkg:golang/github.com/prometheus/client_model@v0.2.0
Pakete
Symbole
  • github.com/prometheus/client_model/go.Metric
Umgebung
go
Erstellt
2026-08-27T15:55:05Z

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.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/client_model@v0.2.0
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:771ed2be6f63fe49ce58114d1e85590dcee1a39869eba1e4345a9cbf46e978d9","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.2.0","kind":"HOW","packages":["pkg:golang/github.com/prometheus/client_model@v0.2.0"],"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.2.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/client_model@v0.2.0","symbols":["github.com/prometheus/client_model/go.Metric"],"verifierAdapter":"golang@1"}
go.mod
module example.com/metric

go 1.22.0

require (
	github.com/golang/protobuf v1.5.4
	github.com/prometheus/client_model v0.2.0
)

require google.golang.org/protobuf v1.36.6 // indirect
go.sum
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
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=
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
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.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
main.go
package main

import (
	"fmt"

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

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.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/client_model@v0.2.0"
  ],
  "symbols": [
    "github.com/prometheus/client_model/go.Metric"
  ]
}
test/contract_test.go
package test

import (
	"testing"

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

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