CodeSampleX

Exemple

go.opentelemetry.io/proto/otlp v1.9.0: v1.TracesData

Échantillon vérifié pour golang go.opentelemetry.io/proto/otlp v1.9.0: v1.TracesData. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a…

sha256:c5cc726388ffc7aac08d5452c2219828f063e1033a71a8c0692a5d0cedc1ab69

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-05

Cas

HOW
Objectif
verify trace/v1.TracesData in pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0
Paquets
Symboles
  • trace/v1.TracesData
Créé
2026-09-05T09:36:17Z

Contrat

  1. TracesData represents OTLP trace payloads containing nested ResourceSpans, ScopeSpans, and Spans with associated attributes and status.
  2. TracesData serializes to and deserializes from binary Protobuf wire format without data loss, satisfying proto.Equal.
  3. TracesData serializes to and deserializes from protojson format, preserving field mappings such as resourceSpans.
  4. TracesData GetResourceSpans returns nil safely when called on nil or unpopulated instances.
  5. proto.Clone produces an isolated deep copy of TracesData where mutating repeated spans or nested attributes does not affect the original.

Fichiers

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • spec.json
  • test/main.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 trace/v1.TracesData in pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0
Demonstrate these symbols/APIs:
  - trace/v1.TracesData

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:41415b53b695caac5e83d579e58375edc64ec1ac9511a1fa785c9ef1badf61a5","contract":["TracesData represents OTLP trace payloads containing nested ResourceSpans, ScopeSpans, and Spans with associated attributes and status.","TracesData serializes to and deserializes from binary Protobuf wire format without data loss, satisfying proto.Equal.","TracesData serializes to and deserializes from protojson format, preserving field mappings such as resourceSpans.","TracesData GetResourceSpans returns nil safely when called on nil or unpopulated instances.","proto.Clone produces an isolated deep copy of TracesData where mutating repeated spans or nested attributes does not affect the original."],"goal":"verify trace/v1.TracesData in pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0"],"schemaVersion":1,"symbols":["trace/v1.TracesData"]},"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/proto/otlp@v1.9.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0","symbols":["trace/v1.TracesData"],"verifierAdapter":"golang@1"}
go.mod
module example.com/otlptrace

go 1.26.6

require (
	go.opentelemetry.io/proto/otlp v1.9.0
	google.golang.org/protobuf v1.36.10
)
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
go.opentelemetry.io/proto/otlp v1.9.0 h1:l706jCMITVouPOqEnii2fIAuO3IVGBRPV5ICjceRb/A=
go.opentelemetry.io/proto/otlp v1.9.0/go.mod h1:xE+Cx5E/eEHw+ISFkwPLwCZefwVjY+pqKg1qcK03+/4=
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify trace/v1.TracesData in pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0"
  ],
  "symbols": [
    "trace/v1.TracesData"
  ]
}
test/main.go
package main

import (
	"bytes"
	"fmt"
	"os"

	commonpb "go.opentelemetry.io/proto/otlp/common/v1"
	resourcepb "go.opentelemetry.io/proto/otlp/resource/v1"
	tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
	"google.golang.org/protobuf/encoding/protojson"
	"google.golang.org/protobuf/proto"
)

func assert(condition bool, message string) {
	if !condition {
		fmt.Fprintf(os.Stderr, "assertion failed: %s\n", message)
		os.Exit(1)
	}
}

func main() {
	traceID := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10}
	spanID := []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22}

	span := &tracepb.Span{
		TraceId:           traceID,
		SpanId:            spanID,
		Name:              "execute-query",
		Kind:              tracepb.Span_SPAN_KIND_SERVER,
		StartTimeUnixNano: 1700000000000000000,
		EndTimeUnixNano:   1700000000500000000,
		Attributes: []*commonpb.KeyValue{
			{
				Key: "db.system",
				Value: &commonpb.AnyValue{
					Value: &commonpb.AnyValue_StringValue{StringValue: "postgresql"},
				},
			},
			{
				Key: "db.statement",
				Value: &commonpb.AnyValue{
					Value: &commonpb.AnyValue_StringValue{StringValue: "SELECT 1"},
				},
			},
		},
		Status: &tracepb.Status{
			Code:    tracepb.Status_STATUS_CODE_OK,
			Message: "query executed successfully",
		},
	}

	scopeSpans := &tracepb.ScopeSpans{
		Scope: &commonpb.InstrumentationScope{
			Name:    "db-instrumentation",
			Version: "1.0.0",
		},
		Spans: []*tracepb.Span{span},
	}

	resource := &resourcepb.Resource{
		Attributes: []*commonpb.KeyValue{
			{
				Key: "service.name",
				Value: &commonpb.AnyValue{
					Value: &commonpb.AnyValue_StringValue{StringValue: "query-service"},
				},
			},
		},
	}

	resourceSpans := &tracepb.ResourceSpans{
		Resource:   resource,
		ScopeSpans: []*tracepb.ScopeSpans{scopeSpans},
		SchemaUrl:  "https://opentelemetry.io/schemas/1.24.0",
	}

	tracesData := &tracepb.TracesData{
		ResourceSpans: []*tracepb.ResourceSpans{resourceSpans},
	}

	// Assertion 1: TracesData represents OTLP trace payloads containing nested ResourceSpans, ScopeSpans, and Spans with associated attributes and status.
	{
		assert(len(tracesData.GetResourceSpans()) == 1, "expected 1 ResourceSpans")
		rs := tracesData.GetResourceSpans()[0]
		assert(rs.GetSchemaUrl() == "https://opentelemetry.io/schemas/1.24.0", "schema URL mismatch")
		assert(rs.GetResource() != nil, "expected non-nil resource")
		assert(len(rs.GetScopeSpans()) == 1, "expected 1 ScopeSpans")
		ss := rs.GetScopeSpans()[0]
		assert(ss.GetScope() != nil && ss.GetScope().GetName() == "db-instrumentation", "scope name mismatch")
		assert(len(ss.GetSpans()) == 1, "expected 1 Span")
		sp := ss.GetSpans()[0]
		assert(sp.GetName() == "execute-query", "span name mismatch")
		assert(sp.GetKind() == tracepb.Span_SPAN_KIND_SERVER, "span kind mismatch")
		assert(bytes.Equal(sp.GetTraceId(), traceID), "trace ID mismatch")
		assert(bytes.Equal(sp.GetSpanId(), spanID), "span ID mismatch")
		assert(sp.GetStatus() != nil && sp.GetStatus().GetCode() == tracepb.Status_STATUS_CODE_OK, "status code mismatch")
	}

	// Assertion 2: TracesData serializes to and deserializes from binary Protobuf wire format without data loss, satisfying proto.Equal.
	{
		wire, err := proto.Marshal(tracesData)
		assert(err == nil, "proto.Marshal failed")
		assert(len(wire) > 0, "marshaled wire length is 0")

		unmarshaled := &tracepb.TracesData{}
		err = proto.Unmarshal(wire, unmarshaled)
		assert(err == nil, "proto.Unmarshal failed")
		assert(proto.Equal(tracesData, unmarshaled), "proto.Equal returned false for protobuf round-trip")
	}

	// Assertion 3: TracesData serializes to and deserializes from protojson format, preserving field mappings such as resourceSpans.
	{
		jsonBytes, err := protojson.Marshal(tracesData)
		assert(err == nil, "protojson.Marshal failed")
		assert(bytes.Contains(jsonBytes, []byte(`"resourceSpans"`)), "expected json to contain 'resourceSpans' key")

		jsonUnmarshaled := &tracepb.TracesData{}
		err = protojson.Unmarshal(jsonBytes, jsonUnmarshaled)
		assert(err == nil, "protojson.Unmarshal failed")
		assert(proto.Equal(tracesData, jsonUnmarshaled), "proto.Equal returned false for protojson round-trip")
	}

	// Assertion 4: TracesData GetResourceSpans returns nil safely when called on nil or unpopulated instances.
	{
		var nilTraces *tracepb.TracesData
		assert(nilTraces.GetResourceSpans() == nil, "expected nil from typed-nil TracesData")

		emptyTraces := &tracepb.TracesData{}
		assert(emptyTraces.GetResourceSpans() == nil, "expected nil from empty TracesData")

		emptyWire, err := proto.Marshal(emptyTraces)
		assert(err == nil, "proto.Marshal failed on empty TracesData")
		assert(len(emptyWire) == 0, "expected 0 wire bytes for empty TracesData")
	}

	// Assertion 5: proto.Clone produces an isolated deep copy of TracesData where mutating repeated spans or nested attributes does not affect the original.
	{
		cloned := proto.Clone(tracesData).(*tracepb.TracesData)
		assert(proto.Equal(tracesData, cloned), "cloned message must equal original")

		// Mutate cloned fields
		cloned.ResourceSpans[0].Resource.Attributes[0].Value = &commonpb.AnyValue{
			Value: &commonpb.AnyValue_StringValue{StringValue: "mutated-service"},
		}
		cloned.ResourceSpans[0].ScopeSpans[0].Spans[0].Name = "mutated-span"
		cloned.ResourceSpans = append(cloned.ResourceSpans, &tracepb.ResourceSpans{})

		// Verify original remains untouched
		assert(tracesData.ResourceSpans[0].Resource.Attributes[0].Value.GetStringValue() == "query-service", "original resource attribute was mutated")
		assert(tracesData.ResourceSpans[0].ScopeSpans[0].Spans[0].GetName() == "execute-query", "original span name was mutated")
		assert(len(tracesData.ResourceSpans) == 1, "original resource spans length was modified")
	}

	fmt.Println("PASS: all trace/v1.TracesData contract assertions verified")
}

Seeder d'origine

anonyme