CodeSampleX

샘플

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

검증된 샘플 — golang go.opentelemetry.io/proto/otlp v1.9.0: v1.AnyValue_StringValue. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다.

sha256:6a6f9ef7dcd2b6f473746432abf10015ebc5108310e23898934c83752968469b

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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-05

케이스

HOW
목표
verify go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue in pkg:golang/go.opentelemetry.io/proto/otlp@v1.9.0
패키지
심벌
  • go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue
생성일
2026-09-05T10:41:46Z

컨트랙트

  1. AnyValue_StringValue wraps a string value as an AnyValue oneof variant
  2. AnyValue.GetStringValue returns the wrapped string when AnyValue_StringValue is set and non-string getters return zero values
  3. AnyValue with AnyValue_StringValue roundtrips through protobuf binary wire format without data loss
  4. AnyValue with AnyValue_StringValue roundtrips through protojson format preserving stringValue
  5. AnyValue_StringValue preserves empty string distinction from unpopulated AnyValue
  6. KeyValue attribute with AnyValue_StringValue provides key and string value access for telemetry metadata

파일

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • 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 go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue 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:
  - go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue

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:7de223053c49195029cddffca5c9a526bf90f1093f0c5d10f6390d7c94cd7a28","contract":["AnyValue_StringValue wraps a string value as an AnyValue oneof variant","AnyValue.GetStringValue returns the wrapped string when AnyValue_StringValue is set and non-string getters return zero values","AnyValue with AnyValue_StringValue roundtrips through protobuf binary wire format without data loss","AnyValue with AnyValue_StringValue roundtrips through protojson format preserving stringValue","AnyValue_StringValue preserves empty string distinction from unpopulated AnyValue","KeyValue attribute with AnyValue_StringValue provides key and string value access for telemetry metadata"],"goal":"verify go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue 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":["go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue"]},"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":["go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require (
	go.opentelemetry.io/proto/otlp v1.9.0
	google.golang.org/protobuf v1.36.11
)
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.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
main.go
package main

import (
	"fmt"

	commonpb "go.opentelemetry.io/proto/otlp/common/v1"
)

// StringAttribute wraps a string value into an OpenTelemetry AnyValue_StringValue.
func StringAttribute(key, val string) *commonpb.KeyValue {
	return &commonpb.KeyValue{
		Key: key,
		Value: &commonpb.AnyValue{
			Value: &commonpb.AnyValue_StringValue{
				StringValue: val,
			},
		},
	}
}

// ExtractStringValue retrieves the string value from an AnyValue, or empty string if not set.
func ExtractStringValue(av *commonpb.AnyValue) string {
	if av == nil {
		return ""
	}
	return av.GetStringValue()
}

func main() {
	attr := StringAttribute("service.name", "billing-service")
	fmt.Printf("Attribute %s=%s\n", attr.GetKey(), ExtractStringValue(attr.GetValue()))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue 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": [
    "go.opentelemetry.io/proto/otlp/common/v1.AnyValue_StringValue"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

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

func main() {
	// 1. AnyValue_StringValue wraps a string value as an AnyValue oneof variant
	{
		strVal := &commonpb.AnyValue_StringValue{StringValue: "production"}
		av := &commonpb.AnyValue{Value: strVal}

		if av.GetValue() == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected non-nil Value\n")
			os.Exit(1)
		}
		extracted, ok := av.GetValue().(*commonpb.AnyValue_StringValue)
		if !ok {
			fmt.Fprintf(os.Stderr, "FAIL: expected *commonpb.AnyValue_StringValue\n")
			os.Exit(1)
		}
		if extracted.StringValue != "production" {
			fmt.Fprintf(os.Stderr, "FAIL: expected 'production', got %q\n", extracted.StringValue)
			os.Exit(1)
		}
	}

	// 2. AnyValue.GetStringValue returns the wrapped string when AnyValue_StringValue is set and non-string getters return zero values
	{
		av := &commonpb.AnyValue{
			Value: &commonpb.AnyValue_StringValue{StringValue: "active"},
		}
		if av.GetStringValue() != "active" {
			fmt.Fprintf(os.Stderr, "FAIL: expected 'active', got %q\n", av.GetStringValue())
			os.Exit(1)
		}
		if av.GetIntValue() != 0 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 0 for int value\n")
			os.Exit(1)
		}
		if av.GetBoolValue() != false {
			fmt.Fprintf(os.Stderr, "FAIL: expected false for bool value\n")
			os.Exit(1)
		}
		if av.GetDoubleValue() != 0.0 {
			fmt.Fprintf(os.Stderr, "FAIL: expected 0.0 for double value\n")
			os.Exit(1)
		}
		if av.GetBytesValue() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected nil for bytes value\n")
			os.Exit(1)
		}
		if av.GetArrayValue() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected nil for array value\n")
			os.Exit(1)
		}
		if av.GetKvlistValue() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected nil for kvlist value\n")
			os.Exit(1)
		}
	}

	// 3. AnyValue with AnyValue_StringValue roundtrips through protobuf binary wire format without data loss
	{
		av := &commonpb.AnyValue{
			Value: &commonpb.AnyValue_StringValue{StringValue: "eu-central-1"},
		}
		wire, err := proto.Marshal(av)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: proto.Marshal error: %v\n", err)
			os.Exit(1)
		}
		var decoded commonpb.AnyValue
		if err := proto.Unmarshal(wire, &decoded); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: proto.Unmarshal error: %v\n", err)
			os.Exit(1)
		}
		if decoded.GetStringValue() != "eu-central-1" {
			fmt.Fprintf(os.Stderr, "FAIL: wire roundtrip value mismatch: got %q\n", decoded.GetStringValue())
			os.Exit(1)
		}
		if _, ok := decoded.GetValue().(*commonpb.AnyValue_StringValue); !ok {
			fmt.Fprintf(os.Stderr, "FAIL: wire roundtrip value type is not AnyValue_StringValue\n")
			os.Exit(1)
		}
	}

	// 4. AnyValue with AnyValue_StringValue roundtrips through protojson format preserving stringValue
	{
		av := &commonpb.AnyValue{
			Value: &commonpb.AnyValue_StringValue{StringValue: "sample-cluster"},
		}
		jsonBytes, err := protojson.Marshal(av)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: protojson.Marshal error: %v\n", err)
			os.Exit(1)
		}
		var jsonDecoded commonpb.AnyValue
		if err := protojson.Unmarshal(jsonBytes, &jsonDecoded); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: protojson.Unmarshal error: %v\n", err)
			os.Exit(1)
		}
		if jsonDecoded.GetStringValue() != "sample-cluster" {
			fmt.Fprintf(os.Stderr, "FAIL: json roundtrip value mismatch: got %q\n", jsonDecoded.GetStringValue())
			os.Exit(1)
		}
	}

	// 5. AnyValue_StringValue preserves empty string distinction from unpopulated AnyValue
	{
		emptyStrAv := &commonpb.AnyValue{
			Value: &commonpb.AnyValue_StringValue{StringValue: ""},
		}
		unpopulatedAv := &commonpb.AnyValue{}

		if emptyStrAv.GetValue() == nil {
			fmt.Fprintf(os.Stderr, "FAIL: empty string AnyValue must have non-nil Value\n")
			os.Exit(1)
		}
		if unpopulatedAv.GetValue() != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unpopulated AnyValue must have nil Value\n")
			os.Exit(1)
		}
		if emptyStrAv.GetStringValue() != unpopulatedAv.GetStringValue() {
			fmt.Fprintf(os.Stderr, "FAIL: both empty string and unpopulated should return empty string from getter\n")
			os.Exit(1)
		}
	}

	// 6. KeyValue attribute with AnyValue_StringValue provides key and string value access for telemetry metadata
	{
		kv := &commonpb.KeyValue{
			Key: "service.name",
			Value: &commonpb.AnyValue{
				Value: &commonpb.AnyValue_StringValue{StringValue: "auth-service"},
			},
		}
		if kv.GetKey() != "service.name" {
			fmt.Fprintf(os.Stderr, "FAIL: KeyValue key mismatch: got %q\n", kv.GetKey())
			os.Exit(1)
		}
		if kv.GetValue().GetStringValue() != "auth-service" {
			fmt.Fprintf(os.Stderr, "FAIL: KeyValue value mismatch: got %q\n", kv.GetValue().GetStringValue())
			os.Exit(1)
		}
	}

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

오리진 시더

익명