CodeSampleX

Exemplo

go.opentelemetry.io/otel/metric v1.37.0: MeterOption

Amostra verificada para golang go.opentelemetry.io/otel/metric v1.37.0: MeterOption. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:d49d508947486eb4d87b846ba558d26c73a74c2aca4de049687fb71f1c182bbc

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-16

Caso

HOW
Objetivo
verify go.opentelemetry.io/otel/metric.MeterOption in pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0
Pacotes
Símbolos
  • go.opentelemetry.io/otel/metric.MeterOption
Criado
2026-09-16T18:55:12Z

Contrato

  1. WithInstrumentationVersion configures the instrumentation version on MeterConfig
  2. WithSchemaURL configures the schema URL on MeterConfig
  3. WithInstrumentationAttributes configures deduplicated attributes with last-value-wins semantics on MeterConfig
  4. NewMeterConfig combines multiple MeterOption instances into a single MeterConfig
  5. MeterProvider.Meter accepts MeterOption options to create a configured Meter

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 go.opentelemetry.io/otel/metric.MeterOption in pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0
Demonstrate these symbols/APIs:
  - go.opentelemetry.io/otel/metric.MeterOption

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:537c0a190103ec546b2e1af01be7b138e0716ff5ca12092d11891dda2d274fea","contract":["WithInstrumentationVersion configures the instrumentation version on MeterConfig","WithSchemaURL configures the schema URL on MeterConfig","WithInstrumentationAttributes configures deduplicated attributes with last-value-wins semantics on MeterConfig","NewMeterConfig combines multiple MeterOption instances into a single MeterConfig","MeterProvider.Meter accepts MeterOption options to create a configured Meter"],"goal":"verify go.opentelemetry.io/otel/metric.MeterOption in pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0"],"schemaVersion":1,"symbols":["go.opentelemetry.io/otel/metric.MeterOption"]},"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/go.opentelemetry.io/otel/metric@v1.37.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0","symbols":["go.opentelemetry.io/otel/metric.MeterOption"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.24.0

require (
	go.opentelemetry.io/otel v1.37.0
	go.opentelemetry.io/otel/metric v1.37.0
)
go.sum
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ=
go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I=
go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE=
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
meter_option.go
package sample

import (
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/metric"
)

// ConfigureMeter applies the provided MeterOption slice to create a MeterConfig.
func ConfigureMeter(opts ...metric.MeterOption) metric.MeterConfig {
	return metric.NewMeterConfig(opts...)
}

// BuildMeterOptions creates a slice of MeterOption configuring version, schema URL, and attributes.
func BuildMeterOptions(version, schemaURL string, attrs ...attribute.KeyValue) []metric.MeterOption {
	var opts []metric.MeterOption
	if version != "" {
		opts = append(opts, metric.WithInstrumentationVersion(version))
	}
	if schemaURL != "" {
		opts = append(opts, metric.WithSchemaURL(schemaURL))
	}
	if len(attrs) > 0 {
		opts = append(opts, metric.WithInstrumentationAttributes(attrs...))
	}
	return opts
}

// NewConfiguredMeter uses a MeterProvider to create a named Meter with the specified MeterOptions.
func NewConfiguredMeter(mp metric.MeterProvider, name string, opts ...metric.MeterOption) metric.Meter {
	return mp.Meter(name, opts...)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.opentelemetry.io/otel/metric.MeterOption in pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/otel/metric@v1.37.0"
  ],
  "symbols": [
    "go.opentelemetry.io/otel/metric.MeterOption"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/metric"
	"go.opentelemetry.io/otel/metric/noop"
	"sample"
)

func main() {
	// 1. WithInstrumentationVersion configures the instrumentation version on MeterConfig
	versionOpt := metric.WithInstrumentationVersion("1.2.3")
	cfg1 := sample.ConfigureMeter(versionOpt)
	if got := cfg1.InstrumentationVersion(); got != "1.2.3" {
		fmt.Fprintf(os.Stderr, "WithInstrumentationVersion failed: got %q, want %q\n", got, "1.2.3")
		os.Exit(1)
	}

	// 2. WithSchemaURL configures the schema URL on MeterConfig
	const schemaURL = "https://example.com/schemas/1.0.0"
	schemaOpt := metric.WithSchemaURL(schemaURL)
	cfg2 := sample.ConfigureMeter(schemaOpt)
	if got := cfg2.SchemaURL(); got != schemaURL {
		fmt.Fprintf(os.Stderr, "WithSchemaURL failed: got %q, want %q\n", got, schemaURL)
		os.Exit(1)
	}

	// 3. WithInstrumentationAttributes configures deduplicated attributes with last-value-wins semantics on MeterConfig
	attr1 := attribute.String("component", "http-handler")
	attr2 := attribute.Int("version.major", 1)
	attrDup := attribute.String("component", "http-handler-v2")
	attrOpt := metric.WithInstrumentationAttributes(attr1, attr2, attrDup)
	cfg3 := sample.ConfigureMeter(attrOpt)
	attrs := cfg3.InstrumentationAttributes()
	if attrs.Len() != 2 {
		fmt.Fprintf(os.Stderr, "WithInstrumentationAttributes deduplication failed: expected 2 attrs, got %d\n", attrs.Len())
		os.Exit(1)
	}
	val, ok := attrs.Value(attribute.Key("component"))
	if !ok || val.AsString() != "http-handler-v2" {
		fmt.Fprintf(os.Stderr, "WithInstrumentationAttributes value mismatch: got %v, want %q\n", val, "http-handler-v2")
		os.Exit(1)
	}

	// 4. NewMeterConfig combines multiple MeterOption instances into a single MeterConfig
	allOpts := sample.BuildMeterOptions("2.0.0", schemaURL, attr1, attr2)
	cfg4 := sample.ConfigureMeter(allOpts...)
	if cfg4.InstrumentationVersion() != "2.0.0" {
		fmt.Fprintf(os.Stderr, "combined config InstrumentationVersion failed: got %q, want %q\n", cfg4.InstrumentationVersion(), "2.0.0")
		os.Exit(1)
	}
	if cfg4.SchemaURL() != schemaURL {
		fmt.Fprintf(os.Stderr, "combined config SchemaURL failed: got %q, want %q\n", cfg4.SchemaURL(), schemaURL)
		os.Exit(1)
	}
	allAttrs := cfg4.InstrumentationAttributes()
	if allAttrs.Len() != 2 {
		fmt.Fprintf(os.Stderr, "combined config InstrumentationAttributes failed: expected 2, got %d\n", allAttrs.Len())
		os.Exit(1)
	}

	// 5. MeterProvider.Meter accepts MeterOption options to create a configured Meter
	mp := noop.NewMeterProvider()
	meter := sample.NewConfiguredMeter(mp, "sample-meter", allOpts...)
	if meter == nil {
		fmt.Fprintf(os.Stderr, "MeterProvider.Meter with options returned nil\n")
		os.Exit(1)
	}

	fmt.Println("All MeterOption contract assertions passed.")
}

Seeder de origem

anônimo