CodeSampleX

Beispiel

github.com/prometheus/common v0.71.0: version

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

sha256:73197198b04bc5e97ba82284aa32aa272188fc8c0018a6670372ec4a1e543a5c

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 linux 24 · ubuntu · glibc 2.39 x64 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/common/version in pkg:golang/github.com/prometheus/common@v0.71.0
Pakete
Symbole
  • github.com/prometheus/common/version
Erstellt
2026-09-05T08:35:10Z

Contract

  1. version.Print, version.Info, and version.BuildContext format build and runtime version strings including program name, Go version, and platform
  2. version.ComponentUserAgent and version.PrometheusUserAgent construct formatted user-agent headers combining component names and version numbers
  3. version.Slog generates structured key-value attributes for slog logging including version, revision, branch, build date, and runtime system fields

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go
  • version_info.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/common/version in pkg:golang/github.com/prometheus/common@v0.71.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/common@v0.71.0
Demonstrate these symbols/APIs:
  - github.com/prometheus/common/version

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:6a59e262f374996609941ba7c0c3f99e6947d9ac260f0615cc3716a6fc0550d2","contract":["version.Print, version.Info, and version.BuildContext format build and runtime version strings including program name, Go version, and platform","version.ComponentUserAgent and version.PrometheusUserAgent construct formatted user-agent headers combining component names and version numbers","version.Slog generates structured key-value attributes for slog logging including version, revision, branch, build date, and runtime system fields"],"goal":"verify github.com/prometheus/common/version in pkg:golang/github.com/prometheus/common@v0.71.0","kind":"HOW","packages":["pkg:golang/github.com/prometheus/common@v0.71.0"],"schemaVersion":1,"symbols":["github.com/prometheus/common/version"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/prometheus/common@v0.71.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/common@v0.71.0","symbols":["github.com/prometheus/common/version"],"verifierAdapter":"golang@1"}
go.mod
module example.com/version-sample

go 1.25.0

require github.com/prometheus/common v0.71.0
go.sum
github.com/prometheus/common v0.71.0 h1:9KDAKb7Mj3HEVKyFCK6Dc/HIwlBzZIN2l7/lrHl3KK8=
github.com/prometheus/common v0.71.0/go.mod h1:CLJ5H8TEsGX8bl31BdMkfhIZ+QmZ9tBPPotUxUbfcmk=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/common/version in pkg:golang/github.com/prometheus/common@v0.71.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/common@v0.71.0"
  ],
  "symbols": [
    "github.com/prometheus/common/version"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	versionsample "example.com/version-sample"
	"github.com/prometheus/common/version"
)

func main() {
	// Assertion 1: Verify default runtime information
	if version.GoVersion == "" {
		fmt.Fprintf(os.Stderr, "FAIL: expected non-empty version.GoVersion\n")
		os.Exit(1)
	}
	if version.GoOS == "" || version.GoArch == "" {
		fmt.Fprintf(os.Stderr, "FAIL: expected non-empty GoOS and GoArch\n")
		os.Exit(1)
	}
	defaultCtx := version.BuildContext()
	if !strings.Contains(defaultCtx, "go=") || !strings.Contains(defaultCtx, "platform=") {
		fmt.Fprintf(os.Stderr, "FAIL: BuildContext missing expected default fields: %s\n", defaultCtx)
		os.Exit(1)
	}

	// Assertion 2: Configure build variables and verify Print, Info, BuildContext
	version.Version = "2.1.0"
	version.Revision = "1a2b3c4d"
	version.Branch = "release-2.1"
	version.BuildUser = "ci-builder"
	version.BuildDate = "2026-09-05T00:00:00Z"

	expectedInfo := "(version=2.1.0, branch=release-2.1, revision=1a2b3c4d)"
	if info := version.Info(); info != expectedInfo {
		fmt.Fprintf(os.Stderr, "FAIL: Info() mismatch: got %q, want %q\n", info, expectedInfo)
		os.Exit(1)
	}

	expectedCtx := fmt.Sprintf("(go=%s, platform=%s/%s, user=ci-builder, date=2026-09-05T00:00:00Z, tags=%s)",
		version.GoVersion, version.GoOS, version.GoArch, version.GetTags())
	if ctx := version.BuildContext(); ctx != expectedCtx {
		fmt.Fprintf(os.Stderr, "FAIL: BuildContext() mismatch: got %q, want %q\n", ctx, expectedCtx)
		os.Exit(1)
	}

	printOut := version.Print("test-service")
	if !strings.Contains(printOut, "test-service, version 2.1.0 (branch: release-2.1, revision: 1a2b3c4d)") {
		fmt.Fprintf(os.Stderr, "FAIL: Print() missing header line: %s\n", printOut)
		os.Exit(1)
	}
	if !strings.Contains(printOut, "build user:       ci-builder") || !strings.Contains(printOut, "build date:       2026-09-05T00:00:00Z") {
		fmt.Fprintf(os.Stderr, "FAIL: Print() missing build user or build date: %s\n", printOut)
		os.Exit(1)
	}

	// Assertion 3: Verify UserAgent generation
	if ua := version.PrometheusUserAgent(); ua != "Prometheus/2.1.0" {
		fmt.Fprintf(os.Stderr, "FAIL: PrometheusUserAgent mismatch: got %q, want %q\n", ua, "Prometheus/2.1.0")
		os.Exit(1)
	}
	if ua := version.ComponentUserAgent("Alertmanager"); ua != "Alertmanager/2.1.0" {
		fmt.Fprintf(os.Stderr, "FAIL: ComponentUserAgent mismatch: got %q, want %q\n", ua, "Alertmanager/2.1.0")
		os.Exit(1)
	}
	if ua := versionsample.FormatUserAgent("Pushgateway"); ua != "Pushgateway/2.1.0" {
		fmt.Fprintf(os.Stderr, "FAIL: versionsample.FormatUserAgent mismatch: got %q, want %q\n", ua, "Pushgateway/2.1.0")
		os.Exit(1)
	}

	// Assertion 4: Verify Slog structured logging key-values
	attrs := version.Slog()
	if len(attrs)%2 != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected even number of slog key-value pairs, got %d\n", len(attrs))
		os.Exit(1)
	}
	attrMap := make(map[string]any)
	for i := 0; i < len(attrs); i += 2 {
		key, ok := attrs[i].(string)
		if !ok {
			fmt.Fprintf(os.Stderr, "FAIL: slog attribute key at index %d is not a string\n", i)
			os.Exit(1)
		}
		attrMap[key] = attrs[i+1]
	}
	if attrMap["version"] != "2.1.0" || attrMap["revision"] != "1a2b3c4d" || attrMap["branch"] != "release-2.1" {
		fmt.Fprintf(os.Stderr, "FAIL: slog attributes mismatch on version/revision/branch: %+v\n", attrMap)
		os.Exit(1)
	}
	if attrMap["builduser"] != "ci-builder" || attrMap["builddate"] != "2026-09-05T00:00:00Z" {
		fmt.Fprintf(os.Stderr, "FAIL: slog attributes mismatch on builduser/builddate: %+v\n", attrMap)
		os.Exit(1)
	}
	if attrMap["goversion"] != version.GoVersion || attrMap["goos"] != version.GoOS || attrMap["goarch"] != version.GoArch {
		fmt.Fprintf(os.Stderr, "FAIL: slog attributes mismatch on runtime fields: %+v\n", attrMap)
		os.Exit(1)
	}

	// Assertion 5: Verify BuildSummary wrapper
	summary := versionsample.GetBuildSummary("test-daemon")
	if summary.Program != "test-daemon" || summary.UserAgent != "Prometheus/2.1.0" || summary.Info != expectedInfo {
		fmt.Fprintf(os.Stderr, "FAIL: BuildSummary mismatch: %+v\n", summary)
		os.Exit(1)
	}

	fmt.Println("PASS: version contract passed")
}
version_info.go
package versionsample

import (
	"github.com/prometheus/common/version"
)

// BuildSummary aggregates application identity and version context.
type BuildSummary struct {
	Program   string
	Info      string
	Context   string
	UserAgent string
	SlogAttrs []any
}

// GetBuildSummary returns formatted version details for the given program name.
func GetBuildSummary(program string) BuildSummary {
	return BuildSummary{
		Program:   program,
		Info:      version.Info(),
		Context:   version.BuildContext(),
		UserAgent: version.PrometheusUserAgent(),
		SlogAttrs: version.Slog(),
	}
}

// FormatUserAgent returns a component-specific user agent string.
func FormatUserAgent(component string) string {
	return version.ComponentUserAgent(component)
}

Ursprungs-Seeder

anonym