CodeSampleX

サンプル

github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4: go.MetricType_COUNTER

検証済みサンプル — golang github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4: go.MetricType_COUNTER. go 1.26 · linux debian/x64 · docker で…

sha256:7a4ec6b117a4cdb68150a2cc4c4adbd8d268d28c95ac039c84bb9e14090bd027

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
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-10-13

ケース

HOW
ゴール
verify github.com/prometheus/client_model/go.MetricType_COUNTER in pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4
パッケージ
シンボル
  • github.com/prometheus/client_model/go.MetricType_COUNTER
作成日
2026-10-13T17:03:11Z

コントラクト

  1. dto.MetricType_COUNTER is an enum constant of type dto.MetricType with numeric int32 value 0 and String() representation 'COUNTER'.
  2. dto.MetricType_COUNTER.Enum() returns a non-nil *dto.MetricType equal to MetricType_COUNTER, and MetricType_name[0] / MetricType_value['COUNTER'] map bidirectionally between 0 and 'COUNTER'.
  3. dto.MetricType_COUNTER.EnumDescriptor() returns a non-empty gzipped FileDescriptorProto byte slice and enum index path.
  4. dto.MetricFamily configured with Type dto.MetricType_COUNTER.Enum() reports GetType() == dto.MetricType_COUNTER, round-trips through JSON serialization preserving type and counter metric value, and clears Type on Reset().
  5. dto.MetricType.UnmarshalJSON decodes both JSON string '"COUNTER"' and numeric '0' into dto.MetricType_COUNTER and rejects unknown enum names.

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

ソースアーティファクトをダウンロード (tar.gz)

ソース

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.0.0-20190812154241-14fe0d1b01d4
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4
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.
csx.json
{"case":{"caseId":"case:sha256:f86b576b488669825fd255410010e680404ed8de80a359c7f63c3820c0a6925f","contract":["dto.MetricType_COUNTER is an enum constant of type dto.MetricType with numeric int32 value 0 and String() representation 'COUNTER'.","dto.MetricType_COUNTER.Enum() returns a non-nil *dto.MetricType equal to MetricType_COUNTER, and MetricType_name[0] / MetricType_value['COUNTER'] map bidirectionally between 0 and 'COUNTER'.","dto.MetricType_COUNTER.EnumDescriptor() returns a non-empty gzipped FileDescriptorProto byte slice and enum index path.","dto.MetricFamily configured with Type dto.MetricType_COUNTER.Enum() reports GetType() == dto.MetricType_COUNTER, round-trips through JSON serialization preserving type and counter metric value, and clears Type on Reset().","dto.MetricType.UnmarshalJSON decodes both JSON string '\"COUNTER\"' and numeric '0' into dto.MetricType_COUNTER and rejects unknown enum names."],"goal":"verify github.com/prometheus/client_model/go.MetricType_COUNTER in pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4","kind":"HOW","packages":["pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4"],"schemaVersion":1,"symbols":["github.com/prometheus/client_model/go.MetricType_COUNTER"]},"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/client_model@v0.0.0-20190812154241-14fe0d1b01d4"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4","symbols":["github.com/prometheus/client_model/go.MetricType_COUNTER"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.21

require github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4

require github.com/golang/protobuf v1.2.0 // indirect
go.sum
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
sample.go
package sample

import (
	"encoding/json"

	dto "github.com/prometheus/client_model/go"
)

// CounterMetricType returns the MetricType_COUNTER enum constant.
func CounterMetricType() dto.MetricType {
	return dto.MetricType_COUNTER
}

// BuildCounterFamily constructs a MetricFamily typed as MetricType_COUNTER
// containing a single counter metric with the given value.
func BuildCounterFamily(name, help string, value float64) *dto.MetricFamily {
	n := name
	h := help
	v := value
	return &dto.MetricFamily{
		Name: &n,
		Help: &h,
		Type: dto.MetricType_COUNTER.Enum(),
		Metric: []*dto.Metric{
			{
				Counter: &dto.Counter{
					Value: &v,
				},
			},
		},
	}
}

// IsCounterFamily reports whether the given MetricFamily has an explicit Type of MetricType_COUNTER.
func IsCounterFamily(mf *dto.MetricFamily) bool {
	return mf != nil && mf.Type != nil && mf.GetType() == dto.MetricType_COUNTER
}

// MarshalCounterFamilyJSON serializes a MetricFamily to JSON bytes.
func MarshalCounterFamilyJSON(mf *dto.MetricFamily) ([]byte, error) {
	return json.Marshal(mf)
}

// UnmarshalCounterFamilyJSON deserializes JSON bytes into a MetricFamily.
func UnmarshalCounterFamilyJSON(data []byte) (*dto.MetricFamily, error) {
	var mf dto.MetricFamily
	if err := json.Unmarshal(data, &mf); err != nil {
		return nil, err
	}
	return &mf, nil
}

// ParseMetricTypeJSON unmarshals a JSON-encoded metric type into a dto.MetricType.
func ParseMetricTypeJSON(data []byte) (dto.MetricType, error) {
	var mt dto.MetricType
	if err := json.Unmarshal(data, &mt); err != nil {
		return 0, err
	}
	return mt, nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/prometheus/client_model/go.MetricType_COUNTER in pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/client_model@v0.0.0-20190812154241-14fe0d1b01d4"
  ],
  "symbols": [
    "github.com/prometheus/client_model/go.MetricType_COUNTER"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"example.com/sample"
	dto "github.com/prometheus/client_model/go"
)

func failf(format string, args ...interface{}) {
	fmt.Fprintf(os.Stderr, "contract failure: "+format+"\n", args...)
	os.Exit(1)
}

func main() {
	// 1. Verify enum constant numeric value and string representation.
	ct := sample.CounterMetricType()
	if ct != dto.MetricType_COUNTER {
		failf("expected CounterMetricType() == MetricType_COUNTER, got %v", ct)
	}
	if int32(dto.MetricType_COUNTER) != 0 {
		failf("expected int32(MetricType_COUNTER) == 0, got %d", int32(dto.MetricType_COUNTER))
	}
	if dto.MetricType_COUNTER.String() != "COUNTER" {
		failf("expected MetricType_COUNTER.String() == COUNTER, got %q", dto.MetricType_COUNTER.String())
	}

	// 2. Verify Enum() pointer helper and lookup maps MetricType_name / MetricType_value.
	ptr := dto.MetricType_COUNTER.Enum()
	if ptr == nil || *ptr != dto.MetricType_COUNTER {
		failf("expected MetricType_COUNTER.Enum() to return non-nil pointer to MetricType_COUNTER")
	}
	if name, ok := dto.MetricType_name[int32(dto.MetricType_COUNTER)]; !ok || name != "COUNTER" {
		failf("expected MetricType_name[0] == COUNTER, got %q (ok=%v)", name, ok)
	}
	if val, ok := dto.MetricType_value["COUNTER"]; !ok || dto.MetricType(val) != dto.MetricType_COUNTER {
		failf("expected MetricType_value[COUNTER] == MetricType_COUNTER, got %d (ok=%v)", val, ok)
	}

	// 3. Verify EnumDescriptor returns non-empty descriptor metadata.
	descBytes, descPath := dto.MetricType_COUNTER.EnumDescriptor()
	if len(descBytes) == 0 || len(descPath) == 0 {
		failf("expected non-empty EnumDescriptor(), got %d bytes and path len %d", len(descBytes), len(descPath))
	}

	// 4. Verify MetricFamily construction, getters, JSON round-trip, and Reset with MetricType_COUNTER.
	mf := sample.BuildCounterFamily("requests_total", "Total processed requests.", 42.5)
	if !sample.IsCounterFamily(mf) {
		failf("expected IsCounterFamily(mf) to be true")
	}
	if mf.GetType() != dto.MetricType_COUNTER {
		failf("expected mf.GetType() == MetricType_COUNTER, got %v", mf.GetType())
	}
	encoded, err := sample.MarshalCounterFamilyJSON(mf)
	if err != nil || len(encoded) == 0 {
		failf("MarshalCounterFamilyJSON failed: err=%v, len=%d", err, len(encoded))
	}
	decoded, err := sample.UnmarshalCounterFamilyJSON(encoded)
	if err != nil {
		failf("UnmarshalCounterFamilyJSON failed: %v", err)
	}
	if !sample.IsCounterFamily(decoded) || decoded.GetType() != dto.MetricType_COUNTER {
		failf("expected unmarshaled MetricFamily type MetricType_COUNTER, got %v", decoded.GetType())
	}
	if decoded.GetName() != "requests_total" || decoded.GetHelp() != "Total processed requests." || len(decoded.GetMetric()) != 1 || decoded.GetMetric()[0].GetCounter().GetValue() != 42.5 {
		failf("unexpected unmarshaled MetricFamily contents: %+v", decoded)
	}
	decoded.Reset()
	if sample.IsCounterFamily(decoded) {
		failf("expected IsCounterFamily(decoded) to be false after Reset()")
	}

	// 5. Verify JSON unmarshaling into MetricType_COUNTER by name and numeric value, and rejection of unknown names.
	fromName, err := sample.ParseMetricTypeJSON([]byte(`"COUNTER"`))
	if err != nil || fromName != dto.MetricType_COUNTER {
		failf("expected JSON \"COUNTER\" to unmarshal to MetricType_COUNTER, got %v (err=%v)", fromName, err)
	}
	fromNum, err := sample.ParseMetricTypeJSON([]byte(`0`))
	if err != nil || fromNum != dto.MetricType_COUNTER {
		failf("expected JSON 0 to unmarshal to MetricType_COUNTER, got %v (err=%v)", fromNum, err)
	}
	if _, err := sample.ParseMetricTypeJSON([]byte(`"INVALID_METRIC_TYPE"`)); err == nil {
		failf("expected JSON \"INVALID_METRIC_TYPE\" to fail unmarshaling")
	}
}

オリジンシーダー

匿名