CodeSampleX

Sample

go.opentelemetry.io/otel/trace v1.47.0: SpanID

Verified sample for golang go.opentelemetry.io/otel/trace v1.47.0: SpanID. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:720e2796dd2f62269ec845b7c904145b3844014de192b68e344a154d66282cfa

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
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-10

Case

HOW
Goal
verify go.opentelemetry.io/otel/trace.SpanID in pkg:golang/go.opentelemetry.io/otel/trace@v1.47.0
Packages
Symbols
  • go.opentelemetry.io/otel/trace.SpanID
Created
2026-10-10T18:16:16Z

Contract

  1. SpanIDFromHex creates a valid SpanID from a 16-character hexadecimal string.
  2. SpanIDFromHex returns an error for invalid length or invalid hexadecimal characters.
  3. SpanID zero value is invalid and produces a 16-character zero string.
  4. SpanID preserves raw byte array values and formats correctly via String.
  5. SpanID MarshalJSON outputs a valid quoted JSON hex string.
  6. SpanID integrates with SpanContext to identify traces and spans.

Files

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

Download the source artifact (tar.gz)

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

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

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:e2780b7c921b8edd249942bdcc42fa9b3883212518909e1716813243f23c812e","contract":["SpanIDFromHex creates a valid SpanID from a 16-character hexadecimal string.","SpanIDFromHex returns an error for invalid length or invalid hexadecimal characters.","SpanID zero value is invalid and produces a 16-character zero string.","SpanID preserves raw byte array values and formats correctly via String.","SpanID MarshalJSON outputs a valid quoted JSON hex string.","SpanID integrates with SpanContext to identify traces and spans."],"goal":"verify go.opentelemetry.io/otel/trace.SpanID in pkg:golang/go.opentelemetry.io/otel/trace@v1.47.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/trace@v1.47.0"],"schemaVersion":1,"symbols":["go.opentelemetry.io/otel/trace.SpanID"]},"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.47.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/trace@v1.47.0","symbols":["go.opentelemetry.io/otel/trace.SpanID"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

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

require (
	github.com/cespare/xxhash/v2 v2.3.0 // indirect
	go.opentelemetry.io/otel v1.47.0 // indirect
)
go.sum
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
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/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
go.opentelemetry.io/otel v1.47.0 h1:j7ALJ/zgkS7Z6aeJW09p8VC9804bC+PpeTfCD4XPnOM=
go.opentelemetry.io/otel v1.47.0/go.mod h1:8wS9O2qfXrYrzp6hIF/HOYJJf/wIhFPhR2xLuP+iXQU=
go.opentelemetry.io/otel/trace v1.47.0 h1:JOjX/Oci8K94QHddo+bbfya/Ai/nf6/dt9ZfrFNWSrM=
go.opentelemetry.io/otel/trace v1.47.0/go.mod h1:jNaSLa2PZEYFG6fRjJABAu+bw4FS08uDmPg28lTghu0=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
main.go
package main

import (
	"encoding/json"
	"fmt"

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

func main() {
	// 1. Create a SpanID from hex string
	hexStr := "4bf92f3577b34da6"
	spanID, err := trace.SpanIDFromHex(hexStr)
	if err != nil {
		panic(err)
	}
	fmt.Printf("SpanID: %s, IsValid: %t\n", spanID.String(), spanID.IsValid())

	// 2. Create SpanID from raw byte array
	raw := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
	rawSpanID := trace.SpanID(raw)
	fmt.Printf("Raw SpanID: %s\n", rawSpanID.String())

	// 3. Marshal SpanID to JSON
	data, err := json.Marshal(spanID)
	if err != nil {
		panic(err)
	}
	fmt.Printf("JSON representation: %s\n", string(data))

	// 4. Integrate with SpanContext
	sc := trace.NewSpanContext(trace.SpanContextConfig{
		SpanID: spanID,
	})
	fmt.Printf("SpanContext SpanID: %s, HasSpanID: %t\n", sc.SpanID().String(), sc.HasSpanID())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.opentelemetry.io/otel/trace.SpanID in pkg:golang/go.opentelemetry.io/otel/trace@v1.47.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/otel/trace@v1.47.0"
  ],
  "symbols": [
    "go.opentelemetry.io/otel/trace.SpanID"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

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

func main() {
	// Assertion 1: SpanIDFromHex creates a valid SpanID from a 16-character hexadecimal string.
	hexStr := "4bf92f3577b34da6"
	spanID, err := trace.SpanIDFromHex(hexStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: SpanIDFromHex returned error: %v\n", err)
		os.Exit(1)
	}
	if !spanID.IsValid() {
		fmt.Fprintf(os.Stderr, "FAIL: expected spanID to be valid\n")
		os.Exit(1)
	}
	if spanID.String() != hexStr {
		fmt.Fprintf(os.Stderr, "FAIL: expected String() to be '%s', got '%s'\n", hexStr, spanID.String())
		os.Exit(1)
	}

	// Assertion 2: SpanIDFromHex returns an error for invalid length or invalid hexadecimal characters.
	if _, err := trace.SpanIDFromHex("1234"); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for short hex string\n")
		os.Exit(1)
	}
	if _, err := trace.SpanIDFromHex("4bf92f3577b34da678"); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for long hex string\n")
		os.Exit(1)
	}
	if _, err := trace.SpanIDFromHex("4bf92f3577b34dzz"); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for non-hex characters\n")
		os.Exit(1)
	}

	// Assertion 3: SpanID zero value is invalid and produces a 16-character zero string.
	var zero trace.SpanID
	if zero.IsValid() {
		fmt.Fprintf(os.Stderr, "FAIL: expected zero SpanID to be invalid\n")
		os.Exit(1)
	}
	if zero.String() != "0000000000000000" {
		fmt.Fprintf(os.Stderr, "FAIL: expected zero SpanID string '0000000000000000', got '%s'\n", zero.String())
		os.Exit(1)
	}

	// Assertion 4: SpanID preserves raw byte array values and formats correctly via String.
	raw := [8]byte{0x00, 0x01, 0x02, 0x03, 0x0a, 0x0b, 0x0e, 0x0f}
	rawSpanID := trace.SpanID(raw)
	if !rawSpanID.IsValid() {
		fmt.Fprintf(os.Stderr, "FAIL: expected rawSpanID with non-zero bytes to be valid\n")
		os.Exit(1)
	}
	if rawSpanID.String() != "000102030a0b0e0f" {
		fmt.Fprintf(os.Stderr, "FAIL: expected String() to be '000102030a0b0e0f', got '%s'\n", rawSpanID.String())
		os.Exit(1)
	}
	if [8]byte(rawSpanID) != raw {
		fmt.Fprintf(os.Stderr, "FAIL: byte representation mismatch\n")
		os.Exit(1)
	}

	// Assertion 5: SpanID MarshalJSON outputs a valid quoted JSON hex string.
	jsonData, err := spanID.MarshalJSON()
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalJSON returned error: %v\n", err)
		os.Exit(1)
	}
	expectedJSON := fmt.Sprintf("%q", hexStr)
	if string(jsonData) != expectedJSON {
		fmt.Fprintf(os.Stderr, "FAIL: expected JSON %s, got %s\n", expectedJSON, string(jsonData))
		os.Exit(1)
	}

	// Assertion 6: SpanID integrates with SpanContext to identify traces and spans.
	sc := trace.NewSpanContext(trace.SpanContextConfig{
		SpanID: spanID,
	})
	if !sc.HasSpanID() {
		fmt.Fprintf(os.Stderr, "FAIL: expected SpanContext to have SpanID\n")
		os.Exit(1)
	}
	if sc.SpanID() != spanID {
		fmt.Fprintf(os.Stderr, "FAIL: expected SpanContext SpanID '%s', got '%s'\n", spanID.String(), sc.SpanID().String())
		os.Exit(1)
	}

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

Origin Seeder

anonymous