CodeSampleX

Beispiel

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

Verifiziertes Beispiel für golang go.opentelemetry.io/otel/trace v1.37.0: SpanContextFromContext. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker…

sha256:ebee662fe41d19b13385cb710367f0bd30ec1e9ad3925c4a78a6d992f0d5644d

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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

Fall

HOW
Ziel
verify go.opentelemetry.io/otel/trace.SpanContextFromContext in pkg:golang/go.opentelemetry.io/otel/trace@v1.37.0
Pakete
Symbole
  • go.opentelemetry.io/otel/trace.SpanContextFromContext
Erstellt
2026-09-17T18:22:41Z

Contract

  1. SpanContextFromContext returns invalid SpanContext with empty TraceID and SpanID for uninstrumented context
  2. SpanContextFromContext returns invalid SpanContext for nil context without panicking
  3. SpanContextFromContext extracts exact SpanContext attributes including TraceID SpanID TraceFlags and TraceState
  4. SpanContextFromContext correctly reflects IsRemote true when populated with ContextWithRemoteSpanContext
  5. SpanContextFromContext reflects overridden child SpanContext while parent context remains unchanged
  6. SpanContextFromContext result matches SpanFromContext(ctx).SpanContext()

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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.SpanContextFromContext 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.SpanContextFromContext

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:f7a6a9d9a1818b58fc76083dd154e12b039d443e9e65ad7345f5fb8a2344ded1","contract":["SpanContextFromContext returns invalid SpanContext with empty TraceID and SpanID for uninstrumented context","SpanContextFromContext returns invalid SpanContext for nil context without panicking","SpanContextFromContext extracts exact SpanContext attributes including TraceID SpanID TraceFlags and TraceState","SpanContextFromContext correctly reflects IsRemote true when populated with ContextWithRemoteSpanContext","SpanContextFromContext reflects overridden child SpanContext while parent context remains unchanged","SpanContextFromContext result matches SpanFromContext(ctx).SpanContext()"],"goal":"verify go.opentelemetry.io/otel/trace.SpanContextFromContext 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.SpanContextFromContext"]},"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.SpanContextFromContext"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

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=
sample.go
package sample

import (
	"context"

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

// ExtractSpanContext extracts a SpanContext from the provided context using trace.SpanContextFromContext.
// If the context contains no active span or is uninstrumented, it returns an invalid empty SpanContext.
func ExtractSpanContext(ctx context.Context) trace.SpanContext {
	return trace.SpanContextFromContext(ctx)
}

// InjectSpanContext wraps a context with the given SpanContext using trace.ContextWithSpanContext.
func InjectSpanContext(ctx context.Context, sc trace.SpanContext) context.Context {
	return trace.ContextWithSpanContext(ctx, sc)
}

// InjectRemoteSpanContext wraps a context with the given remote SpanContext using trace.ContextWithRemoteSpanContext.
func InjectRemoteSpanContext(ctx context.Context, sc trace.SpanContext) context.Context {
	return trace.ContextWithRemoteSpanContext(ctx, sc)
}

// HasActiveSpanContext checks whether the context contains a valid and sampled SpanContext.
func HasActiveSpanContext(ctx context.Context) bool {
	sc := trace.SpanContextFromContext(ctx)
	return sc.IsValid() && sc.IsSampled()
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.opentelemetry.io/otel/trace.SpanContextFromContext 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.SpanContextFromContext"
  ]
}
test/contract.go
package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	// 1. Uninstrumented context returns invalid empty SpanContext
	ctx := context.Background()
	sc := trace.SpanContextFromContext(ctx)
	if sc.IsValid() {
		fmt.Fprintf(os.Stderr, "expected invalid SpanContext from uninstrumented context, got valid\n")
		os.Exit(1)
	}
	if sc.HasTraceID() || sc.HasSpanID() {
		fmt.Fprintf(os.Stderr, "expected no TraceID or SpanID from uninstrumented context\n")
		os.Exit(1)
	}
	if sc.IsRemote() {
		fmt.Fprintf(os.Stderr, "expected IsRemote to be false for uninstrumented context\n")
		os.Exit(1)
	}

	// 2. Nil context returns invalid SpanContext without panicking
	scNil := trace.SpanContextFromContext(nil)
	if scNil.IsValid() {
		fmt.Fprintf(os.Stderr, "expected invalid SpanContext from nil context, got valid\n")
		os.Exit(1)
	}

	// 3. Valid SpanContext populated in context is extracted identically
	tid, err := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse TraceID: %v\n", err)
		os.Exit(1)
	}
	sid, err := trace.SpanIDFromHex("00f067aa0ba902b7")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse SpanID: %v\n", err)
		os.Exit(1)
	}

	ts, err := trace.ParseTraceState("rojo=1,congo=2")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse TraceState: %v\n", err)
		os.Exit(1)
	}

	cfg := trace.SpanContextConfig{
		TraceID:    tid,
		SpanID:     sid,
		TraceFlags: trace.FlagsSampled,
		TraceState: ts,
	}
	origSC := trace.NewSpanContext(cfg)
	if !origSC.IsValid() {
		fmt.Fprintf(os.Stderr, "expected constructed SpanContext to be valid\n")
		os.Exit(1)
	}

	ctxWithSC := sample.InjectSpanContext(ctx, origSC)
	extractedSC := trace.SpanContextFromContext(ctxWithSC)

	if !extractedSC.Equal(origSC) {
		fmt.Fprintf(os.Stderr, "extracted SpanContext does not match original SpanContext\n")
		os.Exit(1)
	}
	if extractedSC.TraceID() != tid || extractedSC.SpanID() != sid {
		fmt.Fprintf(os.Stderr, "extracted TraceID or SpanID mismatch\n")
		os.Exit(1)
	}
	if !extractedSC.IsSampled() {
		fmt.Fprintf(os.Stderr, "expected extracted SpanContext to have sampled flag set\n")
		os.Exit(1)
	}
	if extractedSC.TraceState().String() != origSC.TraceState().String() {
		fmt.Fprintf(os.Stderr, "extracted TraceState mismatch\n")
		os.Exit(1)
	}
	if extractedSC.IsRemote() {
		fmt.Fprintf(os.Stderr, "expected IsRemote to be false for local context\n")
		os.Exit(1)
	}

	// 4. Remote SpanContext via ContextWithRemoteSpanContext retains remote status
	remoteCtx := sample.InjectRemoteSpanContext(ctx, origSC)
	extractedRemoteSC := trace.SpanContextFromContext(remoteCtx)
	if !extractedRemoteSC.IsValid() {
		fmt.Fprintf(os.Stderr, "expected remote SpanContext to be valid\n")
		os.Exit(1)
	}
	if !extractedRemoteSC.IsRemote() {
		fmt.Fprintf(os.Stderr, "expected extracted remote SpanContext to have IsRemote true\n")
		os.Exit(1)
	}
	if extractedRemoteSC.TraceID() != tid || extractedRemoteSC.SpanID() != sid {
		fmt.Fprintf(os.Stderr, "extracted remote TraceID or SpanID mismatch\n")
		os.Exit(1)
	}

	// 5. Child context overrides parent SpanContext while parent remains immutable
	childSID, _ := trace.SpanIDFromHex("00f067aa0ba902b8")
	childSC := trace.NewSpanContext(trace.SpanContextConfig{
		TraceID:    tid,
		SpanID:     childSID,
		TraceFlags: trace.FlagsSampled,
	})
	childCtx := sample.InjectSpanContext(ctxWithSC, childSC)

	extractedChildSC := trace.SpanContextFromContext(childCtx)
	if extractedChildSC.SpanID() != childSID {
		fmt.Fprintf(os.Stderr, "expected child context to have child SpanID %s, got %s\n", childSID, extractedChildSC.SpanID())
		os.Exit(1)
	}
	parentAgain := trace.SpanContextFromContext(ctxWithSC)
	if parentAgain.SpanID() != sid {
		fmt.Fprintf(os.Stderr, "expected parent context to retain SpanID %s, got %s\n", sid, parentAgain.SpanID())
		os.Exit(1)
	}

	// 6. Equivalence with SpanFromContext(ctx).SpanContext() and sample helper
	spanFromCtxSC := trace.SpanFromContext(ctxWithSC).SpanContext()
	if !spanFromCtxSC.Equal(extractedSC) {
		fmt.Fprintf(os.Stderr, "SpanFromContext().SpanContext() does not match SpanContextFromContext()\n")
		os.Exit(1)
	}

	if !sample.HasActiveSpanContext(ctxWithSC) {
		fmt.Fprintf(os.Stderr, "expected HasActiveSpanContext to return true for active context\n")
		os.Exit(1)
	}
	if sample.HasActiveSpanContext(ctx) {
		fmt.Fprintf(os.Stderr, "expected HasActiveSpanContext to return false for uninstrumented context\n")
		os.Exit(1)
	}

	fmt.Println("All trace.SpanContextFromContext contract assertions passed.")
}

Ursprungs-Seeder

anonym