CodeSampleX

Sample

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

Verified sample for golang github.com/prometheus/client_model v0.6.1: go.MetricType_COUNTER. The contract ran on go 1.26 · linux debian/x64 · docker and…

sha256:f1cc452d16973a2da0cb832b76833d7db808e38580f2a1d8c9214f45cd4c3857

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment go linux 24 · ubuntu · glibc 2.39 x64 go go go

Verification-run environments

Environment Contract Stages Run
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

Case

HOW
Goal
verify github.com/prometheus/client_model/go.MetricType_COUNTER in pkg:golang/github.com/prometheus/client_model@v0.6.1
Packages
Symbols
  • github.com/prometheus/client_model/go.MetricType_COUNTER
Environment
go
Created
2026-09-05T14:43:23Z

Contract

  1. dto.MetricType_COUNTER is an enum constant of type dto.MetricType with value 0 representing Prometheus counter metrics.
  2. dto.MetricType_COUNTER has String representation 'COUNTER', Enum helper returning *MetricType, Number returning 0, and maps in MetricType_name and MetricType_value.
  3. dto.MetricFamily configured with Type MetricType_COUNTER.Enum() stores counter metric values and roundtrips losslessly through protobuf serialization.
  4. dto.MetricType unmarshals JSON string 'COUNTER' and numeric 0 to MetricType_COUNTER, and rejects invalid type strings.

Files

  • NOTES.md
  • PROMPT.md
  • counter.go
  • counter_test.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json

Download the source artifact (tar.gz)

Source

NOTES.md
# prometheus client_model MetricType_COUNTER Verification Notes

## Search Results
An initial search via `search_known_solution` for `pkg:golang/github.com/prometheus/client_model@v0.6.1` with `github.com/prometheus/client_model/go.MetricType_COUNTER` returned a reference-only match for v0.2.0.

## Pinned Version
- Package: `github.com/prometheus/client_model`
- Release: `v0.6.1` (in `go.mod` / `go.sum`)
- Direct Dependency: `google.golang.org/protobuf v1.33.0`

## Observed Behavior
1. **MetricType_COUNTER Enum Constant**:
   - `dto.MetricType_COUNTER` is a typed constant of enum type `dto.MetricType` equal to `0`.
   - `dto.MetricType_COUNTER.String()` returns `"COUNTER"`.
   - `dto.MetricType_COUNTER.Enum()` returns `*dto.MetricType`.
   - `dto.MetricType_COUNTER.Number()` returns protoreflect enum number `0`.
   - `dto.MetricType_name` and `dto.MetricType_value` map between string representation and int32 representation.
2. **MetricFamily Integration & Protobuf Serialization**:
   - `dto.MetricFamily` configured with `Type: dto.MetricType_COUNTER.Enum()` contains metrics with `Counter` definitions and roundtrips through protobuf serialization without data loss.
3. **JSON Unmarshaling**:
   - `dto.MetricType` unmarshals both string `"COUNTER"` and integer `0` to `dto.MetricType_COUNTER`, and fails cleanly on invalid types.
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.MetricType_COUNTER 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.MetricType_COUNTER

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.
counter.go
package clientmodelcounter

import (
	"sort"

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

// NewCounterMetricFamily constructs a MetricFamily with MetricType_COUNTER.
func NewCounterMetricFamily(name, help string, labels map[string]string, value float64) *dto.MetricFamily {
	var labelPairs []*dto.LabelPair
	if len(labels) > 0 {
		keys := make([]string, 0, len(labels))
		for k := range labels {
			keys = append(keys, k)
		}
		sort.Strings(keys)
		labelPairs = make([]*dto.LabelPair, 0, len(keys))
		for _, k := range keys {
			labelPairs = append(labelPairs, &dto.LabelPair{
				Name:  proto.String(k),
				Value: proto.String(labels[k]),
			})
		}
	}

	metric := &dto.Metric{
		Label: labelPairs,
		Counter: &dto.Counter{
			Value: proto.Float64(value),
		},
	}

	return &dto.MetricFamily{
		Name:   proto.String(name),
		Help:   proto.String(help),
		Type:   dto.MetricType_COUNTER.Enum(),
		Metric: []*dto.Metric{metric},
	}
}

// IsCounterMetricFamily returns true if the MetricFamily type is MetricType_COUNTER.
func IsCounterMetricFamily(mf *dto.MetricFamily) bool {
	if mf == nil {
		return false
	}
	return mf.GetType() == dto.MetricType_COUNTER
}

// RoundTripMetricFamily marshals and unmarshals a MetricFamily using protobuf.
func RoundTripMetricFamily(mf *dto.MetricFamily) (*dto.MetricFamily, error) {
	data, err := proto.Marshal(mf)
	if err != nil {
		return nil, err
	}
	out := &dto.MetricFamily{}
	if err := proto.Unmarshal(data, out); err != nil {
		return nil, err
	}
	return out, nil
}
counter_test.go
package clientmodelcounter_test

import (
	"encoding/json"
	"testing"

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

	"example.com/clientmodelcounter"
)

func TestMetricTypeCounterConstantsAndMethods(t *testing.T) {
	// Verify enum numeric value
	if dto.MetricType_COUNTER != 0 {
		t.Fatalf("expected MetricType_COUNTER == 0, got %d", dto.MetricType_COUNTER)
	}

	// Verify String() representation
	if str := dto.MetricType_COUNTER.String(); str != "COUNTER" {
		t.Fatalf("expected MetricType_COUNTER.String() == %q, got %q", "COUNTER", str)
	}

	// Verify Enum() returns pointer to MetricType_COUNTER
	enumPtr := dto.MetricType_COUNTER.Enum()
	if enumPtr == nil || *enumPtr != dto.MetricType_COUNTER {
		t.Fatalf("expected Enum() to return pointer to MetricType_COUNTER, got %v", enumPtr)
	}

	// Verify Number() returns protoreflect.EnumNumber(0)
	if num := dto.MetricType_COUNTER.Number(); num != 0 {
		t.Fatalf("expected Number() == 0, got %d", num)
	}

	// Verify Descriptor() has full name and value name
	desc := dto.MetricType_COUNTER.Descriptor()
	if desc == nil {
		t.Fatal("expected non-nil Descriptor()")
	}
	if string(desc.FullName()) != "io.prometheus.client.MetricType" {
		t.Errorf("expected descriptor full name 'io.prometheus.client.MetricType', got %q", desc.FullName())
	}
	valDesc := desc.Values().ByNumber(0)
	if valDesc == nil || string(valDesc.Name()) != "COUNTER" {
		t.Errorf("expected value descriptor 0 to be 'COUNTER', got %v", valDesc)
	}

	// Verify name and value maps
	if name, ok := dto.MetricType_name[int32(dto.MetricType_COUNTER)]; !ok || name != "COUNTER" {
		t.Fatalf("expected MetricType_name mapping to COUNTER, got %q (ok=%v)", name, ok)
	}
	if val, ok := dto.MetricType_value["COUNTER"]; !ok || val != int32(dto.MetricType_COUNTER) {
		t.Fatalf("expected MetricType_value mapping for COUNTER to 0, got %d (ok=%v)", val, ok)
	}
}

func TestMetricTypeCounterJSONUnmarshal(t *testing.T) {
	// Unmarshal string representation "COUNTER"
	var mtFromString dto.MetricType
	if err := json.Unmarshal([]byte(`"COUNTER"`), &mtFromString); err != nil {
		t.Fatalf("failed to unmarshal JSON string \"COUNTER\": %v", err)
	}
	if mtFromString != dto.MetricType_COUNTER {
		t.Fatalf("expected MetricType_COUNTER, got %v", mtFromString)
	}

	// Unmarshal numeric representation 0
	var mtFromInt dto.MetricType
	if err := json.Unmarshal([]byte(`0`), &mtFromInt); err != nil {
		t.Fatalf("failed to unmarshal numeric JSON 0: %v", err)
	}
	if mtFromInt != dto.MetricType_COUNTER {
		t.Fatalf("expected MetricType_COUNTER, got %v", mtFromInt)
	}

	// Unmarshal invalid string should fail
	var mtInvalid dto.MetricType
	if err := json.Unmarshal([]byte(`"INVALID_TYPE"`), &mtInvalid); err == nil {
		t.Fatal("expected error when unmarshaling invalid MetricType string, got nil")
	}
}

func TestMetricFamilyWithCounter(t *testing.T) {
	labels := map[string]string{
		"handler": "/metrics",
		"method":  "GET",
	}
	mf := clientmodelcounter.NewCounterMetricFamily(
		"http_requests_total",
		"Total number of HTTP requests processed",
		labels,
		150.0,
	)

	if !clientmodelcounter.IsCounterMetricFamily(mf) {
		t.Fatal("expected IsCounterMetricFamily(mf) to be true")
	}
	if clientmodelcounter.IsCounterMetricFamily(nil) {
		t.Fatal("expected IsCounterMetricFamily(nil) to be false")
	}

	if mf.GetName() != "http_requests_total" {
		t.Errorf("expected name 'http_requests_total', got %q", mf.GetName())
	}
	if mf.GetHelp() != "Total number of HTTP requests processed" {
		t.Errorf("expected matching help, got %q", mf.GetHelp())
	}
	if mf.GetType() != dto.MetricType_COUNTER {
		t.Fatalf("expected type MetricType_COUNTER, got %v", mf.GetType())
	}

	metrics := mf.GetMetric()
	if len(metrics) != 1 {
		t.Fatalf("expected 1 metric, got %d", len(metrics))
	}
	counter := metrics[0].GetCounter()
	if counter == nil {
		t.Fatal("expected non-nil Counter in metric")
	}
	if counter.GetValue() != 150.0 {
		t.Errorf("expected counter value 150.0, got %f", counter.GetValue())
	}

	// Verify labels sorted order
	metricLabels := metrics[0].GetLabel()
	if len(metricLabels) != 2 {
		t.Fatalf("expected 2 labels, got %d", len(metricLabels))
	}
	if metricLabels[0].GetName() != "handler" || metricLabels[0].GetValue() != "/metrics" {
		t.Errorf("unexpected label 0: %v", metricLabels[0])
	}
	if metricLabels[1].GetName() != "method" || metricLabels[1].GetValue() != "GET" {
		t.Errorf("unexpected label 1: %v", metricLabels[1])
	}
}

func TestMetricFamilyProtobufRoundTrip(t *testing.T) {
	original := clientmodelcounter.NewCounterMetricFamily(
		"api_errors_total",
		"Total number of API errors encountered",
		map[string]string{"code": "500"},
		42.0,
	)

	deserialized, err := clientmodelcounter.RoundTripMetricFamily(original)
	if err != nil {
		t.Fatalf("RoundTripMetricFamily failed: %v", err)
	}

	if !proto.Equal(original, deserialized) {
		t.Fatal("deserialized MetricFamily does not equal original via proto.Equal")
	}

	if deserialized.GetType() != dto.MetricType_COUNTER {
		t.Fatalf("expected deserialized type MetricType_COUNTER, got %v", deserialized.GetType())
	}
	if deserialized.GetName() != original.GetName() {
		t.Errorf("expected name %q, got %q", original.GetName(), deserialized.GetName())
	}
	if deserialized.GetMetric()[0].GetCounter().GetValue() != 42.0 {
		t.Errorf("expected counter value 42.0, got %f", deserialized.GetMetric()[0].GetCounter().GetValue())
	}
}
csx.json
{"case":{"caseId":"case:sha256:1b57e0ff743cb23692d4f5473c4d45d7c71cc1d8810d0eaebf53bc9eb2422d96","contract":["dto.MetricType_COUNTER is an enum constant of type dto.MetricType with value 0 representing Prometheus counter metrics.","dto.MetricType_COUNTER has String representation 'COUNTER', Enum helper returning *MetricType, Number returning 0, and maps in MetricType_name and MetricType_value.","dto.MetricFamily configured with Type MetricType_COUNTER.Enum() stores counter metric values and roundtrips losslessly through protobuf serialization.","dto.MetricType unmarshals JSON string 'COUNTER' and numeric 0 to MetricType_COUNTER, and rejects invalid type strings."],"goal":"verify github.com/prometheus/client_model/go.MetricType_COUNTER 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.MetricType_COUNTER"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","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.MetricType_COUNTER"],"verifierAdapter":"golang@1"}
go.mod
module example.com/clientmodelcounter

go 1.22.0

require (
	github.com/prometheus/client_model v0.6.1
	google.golang.org/protobuf v1.33.0
)
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.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_model/go.MetricType_COUNTER 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.MetricType_COUNTER"
  ]
}

Origin Seeder

anonymous