CodeSampleX

Sample

go.opentelemetry.io/otel/trace v1.24.0: TraceState

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

sha256:332e09a36d4ffd5a8536a87a9264b3428f533a0f71941f743afc291ed23216c0

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-09-03

Case

HOW
Goal
verify TraceState in pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0
Packages
Symbols
  • TraceState
Created
2026-09-03T01:06:06Z

Contract

  1. ParseTraceState parses valid W3C tracestate string into TraceState
  2. ParseTraceState returns an error for invalid tracestate string
  3. TraceState.Get returns value for existing member and empty string for non-existent key
  4. TraceState.Insert adds new member and moves updated member to the front
  5. TraceState.Delete removes member by key
  6. TraceState.Len returns member count and String returns serialized W3C tracestate string

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go
  • tracestate.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 TraceState in pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0
Kind: HOW

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

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:deaaea08efdfca7a58863cbba8df763e28daa20df50c95c423252056679b549c","contract":["ParseTraceState parses valid W3C tracestate string into TraceState","ParseTraceState returns an error for invalid tracestate string","TraceState.Get returns value for existing member and empty string for non-existent key","TraceState.Insert adds new member and moves updated member to the front","TraceState.Delete removes member by key","TraceState.Len returns member count and String returns serialized W3C tracestate string"],"goal":"verify TraceState in pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0"],"schemaVersion":1,"symbols":["TraceState"]},"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.24.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0","symbols":["TraceState"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

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

require go.opentelemetry.io/otel v1.24.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.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/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=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify TraceState in pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/otel/trace@v1.24.0"
  ],
  "symbols": [
    "TraceState"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"example.com/sample"
	"go.opentelemetry.io/otel/trace"
)

func main() {
	// Assertion 1: ParseTraceState parses valid W3C tracestate string into TraceState
	raw := "congo=t61rcWkgMzE,rojo=00f067aa0ba902b7"
	ts, err := trace.ParseTraceState(raw)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ParseTraceState failed on valid input: %v\n", err)
		os.Exit(1)
	}
	if ts.Len() != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected Len == 2, got %d\n", ts.Len())
		os.Exit(1)
	}

	// Assertion 2: ParseTraceState returns an error for invalid tracestate string
	invalidRaw := "invalid key with spaces=value"
	if _, err := trace.ParseTraceState(invalidRaw); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for invalid tracestate string, got nil\n")
		os.Exit(1)
	}

	// Assertion 3: TraceState.Get returns value for existing member and empty string for non-existent key
	if val := ts.Get("congo"); val != "t61rcWkgMzE" {
		fmt.Fprintf(os.Stderr, "FAIL: Get('congo') expected 't61rcWkgMzE', got %q\n", val)
		os.Exit(1)
	}
	if val := ts.Get("rojo"); val != "00f067aa0ba902b7" {
		fmt.Fprintf(os.Stderr, "FAIL: Get('rojo') expected '00f067aa0ba902b7', got %q\n", val)
		os.Exit(1)
	}
	if val := ts.Get("nonexistent"); val != "" {
		fmt.Fprintf(os.Stderr, "FAIL: Get('nonexistent') expected empty string, got %q\n", val)
		os.Exit(1)
	}

	// Assertion 4: TraceState.Insert adds new member and moves updated member to the front
	tsAdded, err := ts.Insert("vendor", "value123")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Insert new member failed: %v\n", err)
		os.Exit(1)
	}
	if tsAdded.Len() != 3 {
		fmt.Fprintf(os.Stderr, "FAIL: expected Len == 3 after insert, got %d\n", tsAdded.Len())
		os.Exit(1)
	}
	if tsAdded.String() != "vendor=value123,congo=t61rcWkgMzE,rojo=00f067aa0ba902b7" {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected String after new insert: %s\n", tsAdded.String())
		os.Exit(1)
	}

	// Update existing key moves to front
	tsUpdated, err := tsAdded.Insert("rojo", "newvalue")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Insert update member failed: %v\n", err)
		os.Exit(1)
	}
	if tsUpdated.Len() != 3 {
		fmt.Fprintf(os.Stderr, "FAIL: expected Len == 3 after update, got %d\n", tsUpdated.Len())
		os.Exit(1)
	}
	if tsUpdated.String() != "rojo=newvalue,vendor=value123,congo=t61rcWkgMzE" {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected String after update (expected front): %s\n", tsUpdated.String())
		os.Exit(1)
	}

	// Assertion 5: TraceState.Delete removes member by key
	tsDeleted := tsUpdated.Delete("vendor")
	if tsDeleted.Len() != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected Len == 2 after delete, got %d\n", tsDeleted.Len())
		os.Exit(1)
	}
	if val := tsDeleted.Get("vendor"); val != "" {
		fmt.Fprintf(os.Stderr, "FAIL: expected empty string for deleted key 'vendor', got %q\n", val)
		os.Exit(1)
	}
	if tsDeleted.String() != "rojo=newvalue,congo=t61rcWkgMzE" {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected String after delete: %s\n", tsDeleted.String())
		os.Exit(1)
	}

	// Assertion 6: TraceState.Len returns member count and String returns serialized W3C tracestate string
	mgr, err := sample.NewTraceStateManager("alpha=1,beta=2")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewTraceStateManager failed: %v\n", err)
		os.Exit(1)
	}
	if mgr.Count() != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: Count mismatch: got %d, want 2\n", mgr.Count())
		os.Exit(1)
	}
	if mgr.Header() != "alpha=1,beta=2" {
		fmt.Fprintf(os.Stderr, "FAIL: Header mismatch: got %q, want 'alpha=1,beta=2'\n", mgr.Header())
		os.Exit(1)
	}

	fmt.Println("PASS: TraceState contract passed")
}
tracestate.go
package sample

import (
	"fmt"

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

// TraceStateManager wraps trace.TraceState to demonstrate safe member manipulation.
type TraceStateManager struct {
	state trace.TraceState
}

// NewTraceStateManager parses a W3C tracestate string into a TraceStateManager.
func NewTraceStateManager(raw string) (*TraceStateManager, error) {
	ts, err := trace.ParseTraceState(raw)
	if err != nil {
		return nil, fmt.Errorf("failed to parse trace state: %w", err)
	}
	return &TraceStateManager{state: ts}, nil
}

// State returns the current trace.TraceState.
func (m *TraceStateManager) State() trace.TraceState {
	return m.state
}

// Get returns the value associated with key.
func (m *TraceStateManager) Get(key string) string {
	return m.state.Get(key)
}

// Set adds a new entry or updates an existing entry, placing it at the front.
func (m *TraceStateManager) Set(key, value string) error {
	updated, err := m.state.Insert(key, value)
	if err != nil {
		return fmt.Errorf("failed to insert trace state entry: %w", err)
	}
	m.state = updated
	return nil
}

// Delete removes an entry by key from the trace state.
func (m *TraceStateManager) Delete(key string) {
	m.state = m.state.Delete(key)
}

// Header returns the W3C serialized tracestate string.
func (m *TraceStateManager) Header() string {
	return m.state.String()
}

// Count returns the number of list-members in the trace state.
func (m *TraceStateManager) Count() int {
	return m.state.Len()
}

Origin Seeder

anonymous