CodeSampleX

示例

google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7: metric.MetricDescriptor

已验证示例 — golang google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7: metric.MetricDescriptor. contract 在 go 1.26 · linux alpine/x64 …

sha256:d353cef8adb26154d4211155e154177d515c55b8b31741c4736ef27001c3d54d

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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-28

案例

HOW
目标
verify google.golang.org/genproto/googleapis/api/metric.MetricDescriptor in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7
包
符号
  • google.golang.org/genproto/googleapis/api/metric.MetricDescriptor
创建时间
2026-08-28T09:48:29Z

契约

  1. MetricDescriptor struct fields and getter methods return configured attributes including Name, Type, MetricKind, ValueType, Unit, Description, DisplayName, Labels, and MonitoredResourceTypes
  2. Nil safety — GetName, GetType, GetUnit, GetDescription, GetDisplayName, GetMetricKind, GetValueType, GetLabels, GetMetadata, and GetMonitoredResourceTypes on nil *MetricDescriptor return zero values without panicking
  3. Protobuf binary serialization and deserialization preserves MetricDescriptor fields exactly
  4. Protobuf JSON serialization and deserialization preserves MetricDescriptor structure and values
  5. Nested LabelDescriptor within MetricDescriptor correctly specifies label schema
  6. Reset clears MetricDescriptor fields back to zero default values

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.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 google.golang.org/genproto/googleapis/api/metric.MetricDescriptor in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7
Demonstrate these symbols/APIs:
  - google.golang.org/genproto/googleapis/api/metric.MetricDescriptor

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:c088eaccc4819aa8eaf386ba587385b3525aba3896d6bb36d9ad1d988dfab1c8","contract":["MetricDescriptor struct fields and getter methods return configured attributes including Name, Type, MetricKind, ValueType, Unit, Description, DisplayName, Labels, and MonitoredResourceTypes","Nil safety — GetName, GetType, GetUnit, GetDescription, GetDisplayName, GetMetricKind, GetValueType, GetLabels, GetMetadata, and GetMonitoredResourceTypes on nil *MetricDescriptor return zero values without panicking","Protobuf binary serialization and deserialization preserves MetricDescriptor fields exactly","Protobuf JSON serialization and deserialization preserves MetricDescriptor structure and values","Nested LabelDescriptor within MetricDescriptor correctly specifies label schema","Reset clears MetricDescriptor fields back to zero default values"],"goal":"verify google.golang.org/genproto/googleapis/api/metric.MetricDescriptor in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7"],"schemaVersion":1,"symbols":["google.golang.org/genproto/googleapis/api/metric.MetricDescriptor"]},"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/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7","symbols":["google.golang.org/genproto/googleapis/api/metric.MetricDescriptor"],"verifierAdapter":"golang@1"}
go.mod
module example.com/metricdescriptor

go 1.22.0

require (
	google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7
	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=
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/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 h1:YcyjlL1PRr2Q17/I0dPk2JmYS5CDXfcdb2Z3YRioEbw=
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7/go.mod h1:OCdP9MfskevB/rbYvHTsXTtKC+3bHWajPdoKgjcYkfo=
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"

	"google.golang.org/genproto/googleapis/api/label"
	"google.golang.org/genproto/googleapis/api/metric"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
)

func main() {
	// Define a custom metric descriptor for measuring request latency
	descriptor := &metric.MetricDescriptor{
		Name:        "projects/example-project/metricDescriptors/custom.googleapis.com/http/server/latency",
		Type:        "custom.googleapis.com/http/server/latency",
		MetricKind:  metric.MetricDescriptor_GAUGE,
		ValueType:   metric.MetricDescriptor_DOUBLE,
		Unit:        "ms",
		Description: "The latency of HTTP requests processed by the server",
		DisplayName: "HTTP Request Latency",
		Labels: []*label.LabelDescriptor{
			{
				Key:         "response_code",
				ValueType:   label.LabelDescriptor_INT64,
				Description: "HTTP status code returned to the client",
			},
			{
				Key:         "environment",
				ValueType:   label.LabelDescriptor_STRING,
				Description: "Deployment environment (e.g. production, staging)",
			},
		},
		MonitoredResourceTypes: []string{
			"generic_task",
			"gce_instance",
		},
	}

	fmt.Printf("Metric Descriptor Type: %s\n", descriptor.GetType())
	fmt.Printf("Metric Kind: %s, Value Type: %s, Unit: %s\n",
		descriptor.GetMetricKind().String(),
		descriptor.GetValueType().String(),
		descriptor.GetUnit(),
	)
	fmt.Printf("Configured Labels: %d\n", len(descriptor.GetLabels()))

	// Protobuf JSON serialization
	jsonBytes, err := protojson.Marshal(descriptor)
	if err != nil {
		fmt.Printf("JSON marshal error: %v\n", err)
		return
	}
	fmt.Printf("Serialized JSON: %s\n", string(jsonBytes))

	// Protobuf binary wire serialization
	wireBytes, err := proto.Marshal(descriptor)
	if err != nil {
		fmt.Printf("Binary marshal error: %v\n", err)
		return
	}
	fmt.Printf("Serialized binary size: %d bytes\n", len(wireBytes))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify google.golang.org/genproto/googleapis/api/metric.MetricDescriptor in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20240826202546-f6391c0de4c7"
  ],
  "symbols": [
    "google.golang.org/genproto/googleapis/api/metric.MetricDescriptor"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/api/label"
	"google.golang.org/genproto/googleapis/api/metric"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
)

func main() {
	// Assertion 1: MetricDescriptor field initialization and getter methods return configured attributes
	{
		desc := &metric.MetricDescriptor{
			Name:        "projects/example-project/metricDescriptors/custom.googleapis.com/invoice/processing_time",
			Type:        "custom.googleapis.com/invoice/processing_time",
			MetricKind:  metric.MetricDescriptor_GAUGE,
			ValueType:   metric.MetricDescriptor_DOUBLE,
			Unit:        "ms",
			Description: "Processing time for invoice operations",
			DisplayName: "Invoice Processing Time",
			Labels: []*label.LabelDescriptor{
				{
					Key:         "environment",
					ValueType:   label.LabelDescriptor_STRING,
					Description: "Deployment environment",
				},
			},
			MonitoredResourceTypes: []string{
				"generic_task",
			},
		}

		if desc.GetName() != "projects/example-project/metricDescriptors/custom.googleapis.com/invoice/processing_time" {
			fmt.Fprintf(os.Stderr, "FAIL: GetName() mismatch: got %q\n", desc.GetName())
			os.Exit(1)
		}
		if desc.GetType() != "custom.googleapis.com/invoice/processing_time" {
			fmt.Fprintf(os.Stderr, "FAIL: GetType() mismatch: got %q\n", desc.GetType())
			os.Exit(1)
		}
		if desc.GetMetricKind() != metric.MetricDescriptor_GAUGE {
			fmt.Fprintf(os.Stderr, "FAIL: GetMetricKind() mismatch: got %v\n", desc.GetMetricKind())
			os.Exit(1)
		}
		if desc.GetValueType() != metric.MetricDescriptor_DOUBLE {
			fmt.Fprintf(os.Stderr, "FAIL: GetValueType() mismatch: got %v\n", desc.GetValueType())
			os.Exit(1)
		}
		if desc.GetUnit() != "ms" {
			fmt.Fprintf(os.Stderr, "FAIL: GetUnit() mismatch: got %q\n", desc.GetUnit())
			os.Exit(1)
		}
		if desc.GetDescription() != "Processing time for invoice operations" {
			fmt.Fprintf(os.Stderr, "FAIL: GetDescription() mismatch: got %q\n", desc.GetDescription())
			os.Exit(1)
		}
		if desc.GetDisplayName() != "Invoice Processing Time" {
			fmt.Fprintf(os.Stderr, "FAIL: GetDisplayName() mismatch: got %q\n", desc.GetDisplayName())
			os.Exit(1)
		}
		if len(desc.GetLabels()) != 1 || desc.GetLabels()[0].GetKey() != "environment" {
			fmt.Fprintf(os.Stderr, "FAIL: GetLabels() mismatch\n")
			os.Exit(1)
		}
		if len(desc.GetMonitoredResourceTypes()) != 1 || desc.GetMonitoredResourceTypes()[0] != "generic_task" {
			fmt.Fprintf(os.Stderr, "FAIL: GetMonitoredResourceTypes() mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 2: Nil safety — getters on nil *MetricDescriptor return zero values without panicking
	{
		var nilDesc *metric.MetricDescriptor
		if nilDesc.GetName() != "" {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetName() expected empty string, got %q\n", nilDesc.GetName())
			os.Exit(1)
		}
		if nilDesc.GetType() != "" {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetType() expected empty string, got %q\n", nilDesc.GetType())
			os.Exit(1)
		}
		if nilDesc.GetUnit() != "" {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetUnit() expected empty string, got %q\n", nilDesc.GetUnit())
			os.Exit(1)
		}
		if nilDesc.GetDescription() != "" {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetDescription() expected empty string, got %q\n", nilDesc.GetDescription())
			os.Exit(1)
		}
		if nilDesc.GetDisplayName() != "" {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetDisplayName() expected empty string, got %q\n", nilDesc.GetDisplayName())
			os.Exit(1)
		}
		if nilDesc.GetMetricKind() != metric.MetricDescriptor_METRIC_KIND_UNSPECIFIED {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetMetricKind() expected METRIC_KIND_UNSPECIFIED, got %v\n", nilDesc.GetMetricKind())
			os.Exit(1)
		}
		if nilDesc.GetValueType() != metric.MetricDescriptor_VALUE_TYPE_UNSPECIFIED {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetValueType() expected VALUE_TYPE_UNSPECIFIED, got %v\n", nilDesc.GetValueType())
			os.Exit(1)
		}
		if nilDesc.GetLabels() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetLabels() expected nil, got %v\n", nilDesc.GetLabels())
			os.Exit(1)
		}
		if nilDesc.GetMetadata() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetMetadata() expected nil, got %v\n", nilDesc.GetMetadata())
			os.Exit(1)
		}
		if nilDesc.GetMonitoredResourceTypes() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: nil GetMonitoredResourceTypes() expected nil, got %v\n", nilDesc.GetMonitoredResourceTypes())
			os.Exit(1)
		}
	}

	// Assertion 3: Protobuf binary serialization and deserialization preserves MetricDescriptor fields exactly
	{
		orig := &metric.MetricDescriptor{
			Name:        "projects/example-project/metricDescriptors/custom.googleapis.com/network/bandwidth",
			Type:        "custom.googleapis.com/network/bandwidth",
			MetricKind:  metric.MetricDescriptor_CUMULATIVE,
			ValueType:   metric.MetricDescriptor_INT64,
			Unit:        "By",
			Description: "Total network egress bytes",
			DisplayName: "Network Egress Bytes",
			Labels: []*label.LabelDescriptor{
				{
					Key:         "interface",
					ValueType:   label.LabelDescriptor_STRING,
					Description: "Network interface identifier",
				},
			},
			MonitoredResourceTypes: []string{"gce_instance"},
		}

		wire, err := proto.Marshal(orig)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal failed: %v\n", err)
			os.Exit(1)
		}

		decoded := &metric.MetricDescriptor{}
		if err := proto.Unmarshal(wire, decoded); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: proto.Unmarshal failed: %v\n", err)
			os.Exit(1)
		}

		if !proto.Equal(orig, decoded) {
			fmt.Fprintf(os.Stderr, "FAIL: decoded proto message does not equal original\n")
			os.Exit(1)
		}
		if decoded.GetType() != orig.GetType() || decoded.GetMetricKind() != orig.GetMetricKind() || decoded.GetValueType() != orig.GetValueType() {
			fmt.Fprintf(os.Stderr, "FAIL: decoded fields mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 4: Protobuf JSON serialization and deserialization preserves MetricDescriptor structure and values
	{
		orig := &metric.MetricDescriptor{
			Name:        "projects/example-project/metricDescriptors/custom.googleapis.com/tasks/queue_size",
			Type:        "custom.googleapis.com/tasks/queue_size",
			MetricKind:  metric.MetricDescriptor_GAUGE,
			ValueType:   metric.MetricDescriptor_INT64,
			Unit:        "1",
			Description: "Current number of items in the queue",
			DisplayName: "Queue Size",
		}

		jsonBytes, err := protojson.Marshal(orig)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: protojson.Marshal failed: %v\n", err)
			os.Exit(1)
		}

		unmarshaled := &metric.MetricDescriptor{}
		if err := protojson.Unmarshal(jsonBytes, unmarshaled); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: protojson.Unmarshal failed: %v\n", err)
			os.Exit(1)
		}

		if unmarshaled.GetType() != "custom.googleapis.com/tasks/queue_size" ||
			unmarshaled.GetMetricKind() != metric.MetricDescriptor_GAUGE ||
			unmarshaled.GetValueType() != metric.MetricDescriptor_INT64 ||
			unmarshaled.GetUnit() != "1" {
			fmt.Fprintf(os.Stderr, "FAIL: protojson unmarshaled fields mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 5: Nested LabelDescriptor within MetricDescriptor correctly specifies label schema
	{
		labelDesc := &label.LabelDescriptor{
			Key:         "region",
			ValueType:   label.LabelDescriptor_STRING,
			Description: "Geographic cloud region",
		}

		desc := &metric.MetricDescriptor{
			Type:   "custom.googleapis.com/api/errors",
			Labels: []*label.LabelDescriptor{labelDesc},
		}

		if len(desc.GetLabels()) != 1 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 1 label, got %d\n", len(desc.GetLabels()))
			os.Exit(1)
		}
		if desc.GetLabels()[0].GetKey() != "region" || desc.GetLabels()[0].GetValueType() != label.LabelDescriptor_STRING {
			fmt.Fprintf(os.Stderr, "FAIL: label descriptor content mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 6: Reset clears MetricDescriptor fields back to zero default values
	{
		desc := &metric.MetricDescriptor{
			Name:        "projects/example/metricDescriptors/test",
			Type:        "custom.googleapis.com/test",
			MetricKind:  metric.MetricDescriptor_GAUGE,
			ValueType:   metric.MetricDescriptor_DOUBLE,
			Unit:        "s",
			Description: "Test metric",
			DisplayName: "Test",
		}

		desc.Reset()
		if desc.GetName() != "" || desc.GetType() != "" || desc.GetUnit() != "" || desc.GetDescription() != "" || desc.GetDisplayName() != "" {
			fmt.Fprintf(os.Stderr, "FAIL: expected empty fields after Reset()\n")
			os.Exit(1)
		}
		if desc.GetMetricKind() != metric.MetricDescriptor_METRIC_KIND_UNSPECIFIED || desc.GetValueType() != metric.MetricDescriptor_VALUE_TYPE_UNSPECIFIED {
			fmt.Fprintf(os.Stderr, "FAIL: expected unspecified enum values after Reset()\n")
			os.Exit(1)
		}
	}

	fmt.Println("PASS: google.golang.org/genproto/googleapis/api/metric.MetricDescriptor contract passed")
}

原始种子者

匿名