CodeSampleX

Exemple

go.opentelemetry.io/otel/trace v1.37.0: Link

Échantillon vérifié pour golang go.opentelemetry.io/otel/trace v1.37.0: Link. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:6dc05ada903ea039767e5532e41db795d800b5a5a912c9f8db98b8cdcf7cf588

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-19

Cas

HOW
Objectif
verify go.opentelemetry.io/otel/trace.Link in pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0
Paquets
Symboles
  • go.opentelemetry.io/otel/trace.Link
Créé
2026-09-17T19:34:04Z

Contrat

  1. Link struct encapsulates a SpanContext and optional key-value Attributes identifying related spans
  2. LinkFromContext extracts the SpanContext from a valid context and attaches provided attributes to the resulting Link
  3. LinkFromContext with an empty or untraced context produces a Link with an invalid SpanContext
  4. WithLinks creates a SpanStartOption that configures links on a SpanConfig retrievable via Links()
  5. WithLinks options accumulate cumulatively when multiple WithLinks options are provided to NewSpanStartConfig

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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

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

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:c50eb674121afa41e2da060aaf9a0c66f20ae94ea8fdd3f017f01909ba07139d","contract":["Link struct encapsulates a SpanContext and optional key-value Attributes identifying related spans","LinkFromContext extracts the SpanContext from a valid context and attaches provided attributes to the resulting Link","LinkFromContext with an empty or untraced context produces a Link with an invalid SpanContext","WithLinks creates a SpanStartOption that configures links on a SpanConfig retrievable via Links()","WithLinks options accumulate cumulatively when multiple WithLinks options are provided to NewSpanStartConfig"],"goal":"verify go.opentelemetry.io/otel/trace.Link in pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0"],"schemaVersion":1,"symbols":["go.opentelemetry.io/otel/trace.Link"]},"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/trace@v1.37.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0","symbols":["go.opentelemetry.io/otel/trace.Link"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.23.0

require go.opentelemetry.io/otel/trace v1.37.0

require go.opentelemetry.io/otel v1.37.0 // indirect
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/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/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/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=
main.go
package main

import (
	"context"
	"fmt"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

func main() {
	traceID, err := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
	if err != nil {
		panic(err)
	}
	spanID, err := trace.SpanIDFromHex("00f067aa0ba902b7")
	if err != nil {
		panic(err)
	}

	sc := trace.NewSpanContext(trace.SpanContextConfig{
		TraceID:    traceID,
		SpanID:     spanID,
		TraceFlags: trace.FlagsSampled,
		Remote:     true,
	})

	// Create Link directly
	link := trace.Link{
		SpanContext: sc,
		Attributes: []attribute.KeyValue{
			attribute.String("link.role", "batch-parent"),
		},
	}
	fmt.Printf("Link: valid=%v, traceID=%s, attrs=%d\n",
		link.SpanContext.IsValid(), link.SpanContext.TraceID(), len(link.Attributes))

	// Create Link from context
	ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
	linkFromCtx := trace.LinkFromContext(ctx, attribute.String("rpc.system", "grpc"))
	fmt.Printf("LinkFromContext: valid=%v, traceID=%s, attrs=%d\n",
		linkFromCtx.SpanContext.IsValid(), linkFromCtx.SpanContext.TraceID(), len(linkFromCtx.Attributes))

	// Configure Span with links using WithLinks
	cfg := trace.NewSpanStartConfig(trace.WithLinks(link, linkFromCtx))
	fmt.Printf("SpanConfig links count: %d\n", len(cfg.Links()))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.opentelemetry.io/otel/trace.Link in pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0"
  ],
  "symbols": [
    "go.opentelemetry.io/otel/trace.Link"
  ]
}
test/contract.go
package main

import (
	"context"
	"fmt"
	"os"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

func main() {
	traceID, err := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: TraceIDFromHex failed: %v\n", err)
		os.Exit(1)
	}
	spanID, err := trace.SpanIDFromHex("00f067aa0ba902b7")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: SpanIDFromHex failed: %v\n", err)
		os.Exit(1)
	}

	sc := trace.NewSpanContext(trace.SpanContextConfig{
		TraceID:    traceID,
		SpanID:     spanID,
		TraceFlags: trace.FlagsSampled,
		Remote:     true,
	})

	// Assertion 1: Link struct encapsulates a SpanContext and optional key-value Attributes identifying related spans
	attr := attribute.String("link.role", "batch-parent")
	link := trace.Link{
		SpanContext: sc,
		Attributes:  []attribute.KeyValue{attr},
	}
	if !link.SpanContext.IsValid() {
		fmt.Fprintf(os.Stderr, "FAIL: link.SpanContext must be valid\n")
		os.Exit(1)
	}
	if link.SpanContext.TraceID() != traceID {
		fmt.Fprintf(os.Stderr, "FAIL: link TraceID mismatch: expected %v, got %v\n", traceID, link.SpanContext.TraceID())
		os.Exit(1)
	}
	if link.SpanContext.SpanID() != spanID {
		fmt.Fprintf(os.Stderr, "FAIL: link SpanID mismatch: expected %v, got %v\n", spanID, link.SpanContext.SpanID())
		os.Exit(1)
	}
	if len(link.Attributes) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 1 attribute, got %d\n", len(link.Attributes))
		os.Exit(1)
	}
	if link.Attributes[0].Key != attribute.Key("link.role") || link.Attributes[0].Value.AsString() != "batch-parent" {
		fmt.Fprintf(os.Stderr, "FAIL: attribute key/value mismatch\n")
		os.Exit(1)
	}

	// Assertion 2: LinkFromContext extracts the SpanContext from a valid context and attaches provided attributes to the resulting Link
	ctx := trace.ContextWithRemoteSpanContext(context.Background(), sc)
	linkFromCtx := trace.LinkFromContext(ctx, attribute.String("rpc.system", "grpc"), attribute.Int("batch.index", 3))
	if !linkFromCtx.SpanContext.IsValid() {
		fmt.Fprintf(os.Stderr, "FAIL: linkFromCtx.SpanContext must be valid\n")
		os.Exit(1)
	}
	if !linkFromCtx.SpanContext.Equal(sc) {
		fmt.Fprintf(os.Stderr, "FAIL: linkFromCtx.SpanContext must equal injected SpanContext\n")
		os.Exit(1)
	}
	if len(linkFromCtx.Attributes) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 2 attributes on linkFromCtx, got %d\n", len(linkFromCtx.Attributes))
		os.Exit(1)
	}
	if linkFromCtx.Attributes[0].Key != attribute.Key("rpc.system") || linkFromCtx.Attributes[0].Value.AsString() != "grpc" {
		fmt.Fprintf(os.Stderr, "FAIL: first attribute mismatch\n")
		os.Exit(1)
	}
	if linkFromCtx.Attributes[1].Key != attribute.Key("batch.index") || linkFromCtx.Attributes[1].Value.AsInt64() != 3 {
		fmt.Fprintf(os.Stderr, "FAIL: second attribute mismatch\n")
		os.Exit(1)
	}

	// Assertion 3: LinkFromContext with an empty or untraced context produces a Link with an invalid SpanContext
	emptyLink := trace.LinkFromContext(context.Background())
	if emptyLink.SpanContext.IsValid() {
		fmt.Fprintf(os.Stderr, "FAIL: emptyLink SpanContext must be invalid\n")
		os.Exit(1)
	}
	if emptyLink.SpanContext.HasTraceID() || emptyLink.SpanContext.HasSpanID() {
		fmt.Fprintf(os.Stderr, "FAIL: emptyLink SpanContext must not have TraceID or SpanID\n")
		os.Exit(1)
	}
	if len(emptyLink.Attributes) != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: emptyLink must have 0 attributes\n")
		os.Exit(1)
	}

	// Assertion 4: WithLinks creates a SpanStartOption that configures links on a SpanConfig retrievable via Links()
	cfg := trace.NewSpanStartConfig(trace.WithLinks(link, linkFromCtx))
	links := cfg.Links()
	if len(links) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 2 links in SpanConfig, got %d\n", len(links))
		os.Exit(1)
	}
	if !links[0].SpanContext.Equal(link.SpanContext) {
		fmt.Fprintf(os.Stderr, "FAIL: links[0] SpanContext mismatch\n")
		os.Exit(1)
	}
	if !links[1].SpanContext.Equal(linkFromCtx.SpanContext) {
		fmt.Fprintf(os.Stderr, "FAIL: links[1] SpanContext mismatch\n")
		os.Exit(1)
	}

	// Assertion 5: WithLinks options accumulate cumulatively when multiple WithLinks options are provided to NewSpanStartConfig
	cfgAppended := trace.NewSpanStartConfig(trace.WithLinks(link), trace.WithLinks(linkFromCtx))
	appendedLinks := cfgAppended.Links()
	if len(appendedLinks) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 2 accumulated links, got %d\n", len(appendedLinks))
		os.Exit(1)
	}
	if !appendedLinks[0].SpanContext.Equal(link.SpanContext) || !appendedLinks[1].SpanContext.Equal(linkFromCtx.SpanContext) {
		fmt.Fprintf(os.Stderr, "FAIL: accumulated links order or SpanContext mismatch\n")
		os.Exit(1)
	}

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

Seeder d'origine

anonyme