CodeSampleX

サンプル

go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0: WithTimeout

検証済みサンプル — golang go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0: WithTimeout. go 1.26 · linux debian/x64 · docker で contract…

sha256:584f1c986216e1a1ef226f6b7f6cbde178d4aa34c93b5900913c6258e72668ab

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
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-02

ケース

HOW
ゴール
verify otlptracegrpc.WithTimeout in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0
パッケージ
シンボル
  • otlptracegrpc.WithTimeout
作成日
2026-09-02T18:22:31Z

コントラクト

  1. otlptracegrpc.WithTimeout returns non-nil Option
  2. otlptracegrpc.New accepts WithTimeout Option and returns non-nil Exporter
  3. otlptracegrpc exporter with WithTimeout connects to gRPC collector and exports spans
  4. Exported spans deliver trace ID, span ID, span name, and attributes across gRPC
  5. Exporter and TracerProvider shut down cleanly

ファイル

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

ソースアーティファクトをダウンロード (tar.gz)

ソース

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 otlptracegrpc.WithTimeout in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0
Demonstrate these symbols/APIs:
  - otlptracegrpc.WithTimeout

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:b203c9ea363d91c995ccc542d5a415d3bc9ed2556e97ee72f605f915f9da5cbc","contract":["otlptracegrpc.WithTimeout returns non-nil Option","otlptracegrpc.New accepts WithTimeout Option and returns non-nil Exporter","otlptracegrpc exporter with WithTimeout connects to gRPC collector and exports spans","Exported spans deliver trace ID, span ID, span name, and attributes across gRPC","Exporter and TracerProvider shut down cleanly"],"goal":"verify otlptracegrpc.WithTimeout in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0"],"schemaVersion":1,"symbols":["otlptracegrpc.WithTimeout"]},"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/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0","symbols":["otlptracegrpc.WithTimeout"],"verifierAdapter":"golang@1"}
exporter.go
package sample

import (
	"context"
	"fmt"
	"time"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
	"go.opentelemetry.io/otel/sdk/resource"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
	semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
	"go.opentelemetry.io/otel/trace"
	"google.golang.org/grpc"
)

// ExporterConfig holds configuration options for the OTLP trace exporter.
type ExporterConfig struct {
	Endpoint    string
	Insecure    bool
	Timeout     time.Duration
	GRPCConn    *grpc.ClientConn
	DialOptions []grpc.DialOption
}

// NewExporter creates an OTLP trace gRPC exporter with the specified configuration,
// demonstrating the use of otlptracegrpc.WithTimeout.
func NewExporter(ctx context.Context, cfg ExporterConfig) (*otlptrace.Exporter, error) {
	opts := make([]otlptracegrpc.Option, 0, 4)

	if cfg.Endpoint != "" {
		opts = append(opts, otlptracegrpc.WithEndpoint(cfg.Endpoint))
	}
	if cfg.Insecure {
		opts = append(opts, otlptracegrpc.WithInsecure())
	}
	if cfg.Timeout > 0 {
		opts = append(opts, otlptracegrpc.WithTimeout(cfg.Timeout))
	}
	if cfg.GRPCConn != nil {
		opts = append(opts, otlptracegrpc.WithGRPCConn(cfg.GRPCConn))
	}
	if len(cfg.DialOptions) > 0 {
		opts = append(opts, otlptracegrpc.WithDialOption(cfg.DialOptions...))
	}

	exp, err := otlptracegrpc.New(ctx, opts...)
	if err != nil {
		return nil, fmt.Errorf("failed to create otlptracegrpc exporter: %w", err)
	}
	return exp, nil
}

// NewTracerProvider creates a TracerProvider configured with the given OTLP trace exporter.
func NewTracerProvider(ctx context.Context, exp sdktrace.SpanExporter, serviceName string) (*sdktrace.TracerProvider, error) {
	res, err := resource.New(ctx,
		resource.WithAttributes(
			semconv.ServiceNameKey.String(serviceName),
		),
	)
	if err != nil {
		return nil, fmt.Errorf("failed to create resource: %w", err)
	}

	tp := sdktrace.NewTracerProvider(
		sdktrace.WithBatcher(exp),
		sdktrace.WithResource(res),
	)
	return tp, nil
}

// EmitSampleSpan starts and ends a sample span using the provided tracer provider.
func EmitSampleSpan(ctx context.Context, tp *sdktrace.TracerProvider, tracerName, spanName string, attrs ...attribute.KeyValue) (context.Context, trace.Span) {
	tracer := tp.Tracer(tracerName)
	ctx, span := tracer.Start(ctx, spanName, trace.WithAttributes(attrs...))
	span.End()
	return ctx, span
}
go.mod
module example.com/otlptracegrpc-timeout-sample

go 1.22.0

require (
	go.opentelemetry.io/otel v1.35.0
	go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0
	go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0
	go.opentelemetry.io/otel/sdk v1.35.0
	go.opentelemetry.io/otel/trace v1.35.0
	go.opentelemetry.io/proto/otlp v1.5.0
	google.golang.org/grpc v1.71.0
)

require (
	github.com/cenkalti/backoff/v4 v4.3.0 // indirect
	github.com/go-logr/logr v1.4.2 // indirect
	github.com/go-logr/stdr v1.2.2 // indirect
	github.com/google/uuid v1.6.0 // indirect
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
	go.opentelemetry.io/auto/sdk v1.1.0 // indirect
	go.opentelemetry.io/otel/metric v1.35.0 // indirect
	golang.org/x/net v0.35.0 // indirect
	golang.org/x/sys v0.30.0 // indirect
	golang.org/x/text v0.22.0 // indirect
	google.golang.org/genproto/googleapis/api v0.0.0-20250218202821-56aae31c358a // indirect
	google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
	google.golang.org/protobuf v1.36.5 // indirect
)
go.sum
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
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/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 h1:e9Rjr40Z98/clHv5Yg79Is0NtosR5LXRvdr7o/6NwbA=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1/go.mod h1:tIxuGz/9mpox++sgp9fJjHO0+q1X9/UOWd798aAm22M=
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/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 h1:1fTNlAIJZGWLP5FVu0fikVry1IsiUnXjf7QFvoNN3Xw=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0/go.mod h1:zjPK58DtkqQFn+YUMbx0M2XV3QgKU0gS9LeGohREyK4=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0 h1:m639+BofXTvcY1q8CGs4ItwQarYtJPOWmVobfM1HpVI=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0/go.mod h1:LjReUci/F4BUyv+y4dwnq3h/26iNOeC3wAIqgvTIZVo=
go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M=
go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE=
go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY=
go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg=
go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk=
go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w=
go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs=
go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J8o6xRXLrIkyc=
go.opentelemetry.io/proto/otlp v1.5.0 h1:xJvq7gMzB31/d406fB8U5CBdyQGw4P399D1aQWU/3i4=
go.opentelemetry.io/proto/otlp v1.5.0/go.mod h1:keN8WnHxOy8PG0rQZjJJ5A2ebUoafqWp0eVQ4yIXvJ4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
google.golang.org/genproto/googleapis/api v0.0.0-20250218202821-56aae31c358a h1:nwKuGPlUAt+aR+pcrkfFRrTU1BVrSmYyYMxYbUIVHr0=
google.golang.org/genproto/googleapis/api v0.0.0-20250218202821-56aae31c358a/go.mod h1:3kWAYMk1I75K4vykHtKt2ycnOgpA6974V7bREqbsenU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a h1:51aaUVRocpvUOSQKM6Q7VuoaktNIaMCLuhZB6DKksq4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a/go.mod h1:uRxBH1mhmO8PGhU89cMcHaXKZqO+OfakD8QQO0oYwlQ=
google.golang.org/grpc v1.71.0 h1:kF77BGdPTQ4/JZWMlb9VpJ5pa25aqvVqogsxNHHdeBg=
google.golang.org/grpc v1.71.0/go.mod h1:H0GRtasmQOh9LkFoCPDu3ZrwUtD1YGE+b2vYBYd/8Ec=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
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 otlptracegrpc.WithTimeout in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc@v1.35.0"
  ],
  "symbols": [
    "otlptracegrpc.WithTimeout"
  ]
}
test/contract.go
package main

import (
	"context"
	"fmt"
	"net"
	"os"
	"sync"
	"time"

	sample "example.com/otlptracegrpc-timeout-sample"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
	coltracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
	tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

type mockTraceServer struct {
	coltracepb.UnimplementedTraceServiceServer
	mu       sync.Mutex
	requests []*coltracepb.ExportTraceServiceRequest
}

func (s *mockTraceServer) Export(ctx context.Context, req *coltracepb.ExportTraceServiceRequest) (*coltracepb.ExportTraceServiceResponse, error) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.requests = append(s.requests, req)
	return &coltracepb.ExportTraceServiceResponse{}, nil
}

func (s *mockTraceServer) getRequests() []*coltracepb.ExportTraceServiceRequest {
	s.mu.Lock()
	defer s.mu.Unlock()
	copied := make([]*coltracepb.ExportTraceServiceRequest, len(s.requests))
	copy(copied, s.requests)
	return copied
}

func main() {
	ctx := context.Background()

	// Assertion 1: otlptracegrpc.WithTimeout returns non-nil Option
	opt := otlptracegrpc.WithTimeout(5 * time.Second)
	if opt == nil {
		fmt.Fprintf(os.Stderr, "FAIL: otlptracegrpc.WithTimeout returned nil option\n")
		os.Exit(1)
	}

	// Assertion 2: otlptracegrpc.New accepts WithTimeout Option and returns non-nil Exporter
	exp1, err := sample.NewExporter(ctx, sample.ExporterConfig{
		Endpoint: "127.0.0.1:4317",
		Insecure: true,
		Timeout:  3 * time.Second,
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewExporter with WithTimeout failed: %v\n", err)
		os.Exit(1)
	}
	if exp1 == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected non-nil exporter\n")
		os.Exit(1)
	}
	_ = exp1.Shutdown(ctx)

	// Start local mock gRPC collector server for offline verification
	lis, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: net.Listen failed: %v\n", err)
		os.Exit(1)
	}
	defer lis.Close()

	srv := grpc.NewServer()
	mockServer := &mockTraceServer{}
	coltracepb.RegisterTraceServiceServer(srv, mockServer)

	go func() {
		_ = srv.Serve(lis)
	}()
	defer srv.Stop()

	// Connect to local mock gRPC server
	conn, err := grpc.DialContext(ctx, lis.Addr().String(),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: grpc.DialContext failed: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close()

	// Assertion 3: otlptracegrpc exporter with WithTimeout connects to gRPC collector and exports spans
	exp, err := sample.NewExporter(ctx, sample.ExporterConfig{
		GRPCConn: conn,
		Timeout:  5 * time.Second,
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewExporter with GRPCConn and Timeout failed: %v\n", err)
		os.Exit(1)
	}

	tp, err := sample.NewTracerProvider(ctx, exp, "contract-test-service")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewTracerProvider failed: %v\n", err)
		os.Exit(1)
	}

	spanName := "contract-span"
	_, span := sample.EmitSampleSpan(ctx, tp, "contract-tracer", spanName,
		attribute.String("contract.key", "contract-value"),
		attribute.Int("contract.number", 100),
	)
	spanCtx := span.SpanContext()
	wantTraceID := spanCtx.TraceID()
	wantSpanID := spanCtx.SpanID()

	if err := tp.ForceFlush(ctx); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: tp.ForceFlush failed: %v\n", err)
		os.Exit(1)
	}

	// Assertion 4: Exported spans deliver trace ID, span ID, span name, and attributes across gRPC
	reqs := mockServer.getRequests()
	if len(reqs) == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected at least 1 gRPC export request, got 0\n")
		os.Exit(1)
	}

	var foundSpan *tracepb.Span
	for _, req := range reqs {
		for _, rs := range req.GetResourceSpans() {
			for _, ss := range rs.GetScopeSpans() {
				for _, s := range ss.GetSpans() {
					if s.GetName() == spanName {
						foundSpan = s
						break
					}
				}
			}
		}
	}
	if foundSpan == nil {
		fmt.Fprintf(os.Stderr, "FAIL: span %q not found in export requests\n", spanName)
		os.Exit(1)
	}

	if [16]byte(foundSpan.GetTraceId()) != wantTraceID {
		fmt.Fprintf(os.Stderr, "FAIL: trace ID mismatch\n")
		os.Exit(1)
	}
	if [8]byte(foundSpan.GetSpanId()) != wantSpanID {
		fmt.Fprintf(os.Stderr, "FAIL: span ID mismatch\n")
		os.Exit(1)
	}

	attrMap := make(map[string]any)
	for _, kv := range foundSpan.GetAttributes() {
		if kv.GetValue().GetStringValue() != "" {
			attrMap[kv.GetKey()] = kv.GetValue().GetStringValue()
		} else if kv.GetValue().GetIntValue() != 0 {
			attrMap[kv.GetKey()] = kv.GetValue().GetIntValue()
		}
	}
	if attrMap["contract.key"] != "contract-value" || attrMap["contract.number"] != int64(100) {
		fmt.Fprintf(os.Stderr, "FAIL: attributes mismatch\n")
		os.Exit(1)
	}

	// Assertion 5: Exporter and TracerProvider shut down cleanly
	if err := tp.Shutdown(ctx); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: tp.Shutdown failed: %v\n", err)
		os.Exit(1)
	}
	if err := exp.Shutdown(ctx); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: exp.Shutdown failed: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("PASS")
}

オリジンシーダー

匿名