CodeSampleX

Ejemplo

go.opentelemetry.io/otel/metric v1.24.0: Int64UpDownCounter

Muestra verificada para golang go.opentelemetry.io/otel/metric v1.24.0: Int64UpDownCounter. El contrato se ejecutó en go 1.26 · linux debian/x64 · docker y…

sha256:c9b76c4d7be6683aabb52e7bf5c71d2462ebcf238ba678819e394077b225db54

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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-03

Caso

HOW
Objetivo
verify go.opentelemetry.io/otel/metric.Int64UpDownCounter in pkg:golang/go.opentelemetry.io/otel/metric@v1.24.0
Paquetes
Símbolos
  • go.opentelemetry.io/otel/metric.Int64UpDownCounter
Creado
2026-09-03T13:16:58Z

Contrato

  1. NewInt64UpDownCounterConfig applies description and unit options
  2. Meter creates Int64UpDownCounter with configured name and options
  3. Int64UpDownCounter records positive and negative increments
  4. Int64UpDownCounter accepts measurement attributes with context

Archivos

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

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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

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

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:ca97e96d818b5bbae05367d5c194348ddeacc207e9e0fbb353edc116d40f9de8","contract":["NewInt64UpDownCounterConfig applies description and unit options","Meter creates Int64UpDownCounter with configured name and options","Int64UpDownCounter records positive and negative increments","Int64UpDownCounter accepts measurement attributes with context"],"goal":"verify go.opentelemetry.io/otel/metric.Int64UpDownCounter in pkg:golang/go.opentelemetry.io/otel/metric@v1.24.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/metric@v1.24.0"],"schemaVersion":1,"symbols":["go.opentelemetry.io/otel/metric.Int64UpDownCounter"]},"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.24.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/metric@v1.24.0","symbols":["go.opentelemetry.io/otel/metric.Int64UpDownCounter"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22

require (
	go.opentelemetry.io/otel v1.24.0
	go.opentelemetry.io/otel/metric v1.24.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.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/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.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
main.go
package main

import (
	"context"
	"fmt"

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

// ActiveJobTracker tracks the number of concurrently running jobs using Int64UpDownCounter.
type ActiveJobTracker struct {
	counter metric.Int64UpDownCounter
}

// NewActiveJobTracker creates a new ActiveJobTracker with an Int64UpDownCounter instrument.
func NewActiveJobTracker(meter metric.Meter) (*ActiveJobTracker, error) {
	counter, err := meter.Int64UpDownCounter(
		"worker.active_jobs",
		metric.WithDescription("Number of active worker jobs currently executing"),
		metric.WithUnit("{job}"),
	)
	if err != nil {
		return nil, fmt.Errorf("failed to create Int64UpDownCounter: %w", err)
	}

	return &ActiveJobTracker{
		counter: counter,
	}, nil
}

// JobStarted increments the active job count for a queue.
func (t *ActiveJobTracker) JobStarted(ctx context.Context, queueName string) {
	t.counter.Add(ctx, 1, metric.WithAttributes(
		attribute.String("queue", queueName),
	))
}

// JobCompleted decrements the active job count for a queue.
func (t *ActiveJobTracker) JobCompleted(ctx context.Context, queueName string) {
	t.counter.Add(ctx, -1, metric.WithAttributes(
		attribute.String("queue", queueName),
	))
}

func main() {
	mp := noop.NewMeterProvider()
	meter := mp.Meter("example.com/worker-tracker")

	tracker, err := NewActiveJobTracker(meter)
	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	tracker.JobStarted(ctx, "tasks")
	tracker.JobCompleted(ctx, "tasks")

	fmt.Println("Int64UpDownCounter operations recorded successfully")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.opentelemetry.io/otel/metric.Int64UpDownCounter in pkg:golang/go.opentelemetry.io/otel/metric@v1.24.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/otel/metric@v1.24.0"
  ],
  "symbols": [
    "go.opentelemetry.io/otel/metric.Int64UpDownCounter"
  ]
}
test/contract.go
package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	// 1. NewInt64UpDownCounterConfig applies description and unit options
	{
		desc := "active worker jobs count"
		unit := "{job}"
		cfg := metric.NewInt64UpDownCounterConfig(
			metric.WithDescription(desc),
			metric.WithUnit(unit),
		)
		if got := cfg.Description(); got != desc {
			fmt.Fprintf(os.Stderr, "FAIL: expected description %q, got %q\n", desc, got)
			os.Exit(1)
		}
		if got := cfg.Unit(); got != unit {
			fmt.Fprintf(os.Stderr, "FAIL: expected unit %q, got %q\n", unit, got)
			os.Exit(1)
		}
	}

	// 2. Meter creates Int64UpDownCounter with configured name and options
	mp := noop.NewMeterProvider()
	meter := mp.Meter("example.com/test-meter")
	counter, err := meter.Int64UpDownCounter(
		"worker.active_jobs",
		metric.WithDescription("active worker jobs count"),
		metric.WithUnit("{job}"),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: meter.Int64UpDownCounter returned unexpected error: %v\n", err)
		os.Exit(1)
	}
	if counter == nil {
		fmt.Fprintf(os.Stderr, "FAIL: meter.Int64UpDownCounter returned nil instrument\n")
		os.Exit(1)
	}

	// 3. Int64UpDownCounter records positive and negative increments
	ctx := context.Background()
	counter.Add(ctx, 10)
	counter.Add(ctx, -3)
	counter.Add(ctx, -7)

	// 4. Int64UpDownCounter accepts measurement attributes with context
	counter.Add(ctx, 5, metric.WithAttributes(
		attribute.String("queue", "high-priority"),
		attribute.Int("attempt", 1),
	))
	counter.Add(ctx, -5, metric.WithAttributes(
		attribute.String("queue", "high-priority"),
		attribute.Int("attempt", 1),
	))

	fmt.Println("PASS: all contract assertions passed")
}

Seeder de origen

anónimo