CodeSampleX

Beispiel

go.opentelemetry.io/contrib/detectors/gcp v1.39.0: NewDetector

Verifiziertes Beispiel für golang go.opentelemetry.io/contrib/detectors/gcp v1.39.0: NewDetector. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker…

sha256:76a03b386e0ed12a637d1b1079651a9ee3dfd45852f558b77b3a123645b4b48f

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-10-14

Fall

HOW
Ziel
verify gcp.NewDetector in pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0
Pakete
Symbole
  • gcp.NewDetector
Erstellt
2026-10-14T17:38:37Z

Contract

  1. gcp.NewDetector() returns a non-nil resource.Detector whose Detect method extracts cloud.provider=gcp, cloud.account.id, cloud.platform, and platform-specific attributes from the GCE metadata server and environment
  2. On GCE (machine-type metadata present), Detect sets cloud.platform=gcp_compute_engine along with cloud.availability_zone, cloud.region, host.type, host.id, host.name, gcp.gce.instance.name, and gcp.gce.instance.hostname
  3. On GKE (KUBERNETES_SERVICE_HOST set and cluster-location metadata present), Detect sets cloud.platform=gcp_kubernetes_engine, k8s.cluster.name, cloud.availability_zone, and host.id
  4. On Cloud Run (K_CONFIGURATION, K_SERVICE, K_REVISION set) and Cloud Run Job (CLOUD_RUN_JOB, CLOUD_RUN_EXECUTION, CLOUD_RUN_TASK_INDEX set), Detect sets cloud.platform=gcp_cloud_run and the corresponding faas.* and gcp.cloud_run.job.* attributes
  5. On Cloud Functions (FUNCTION_TARGET set) and App Engine Standard (GAE_ENV=standard, GAE_SERVICE set), Detect sets cloud.platform=gcp_cloud_functions and gcp_app_engine respectively with their faas.* and cloud location attributes

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • 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 gcp.NewDetector in pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0
Demonstrate these symbols/APIs:
  - gcp.NewDetector

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:34e4ae7c9c30bbc41130cb5b0e9a3806d1ff54ca5803d0d420e61c6e78620f18","contract":["gcp.NewDetector() returns a non-nil resource.Detector whose Detect method extracts cloud.provider=gcp, cloud.account.id, cloud.platform, and platform-specific attributes from the GCE metadata server and environment","On GCE (machine-type metadata present), Detect sets cloud.platform=gcp_compute_engine along with cloud.availability_zone, cloud.region, host.type, host.id, host.name, gcp.gce.instance.name, and gcp.gce.instance.hostname","On GKE (KUBERNETES_SERVICE_HOST set and cluster-location metadata present), Detect sets cloud.platform=gcp_kubernetes_engine, k8s.cluster.name, cloud.availability_zone, and host.id","On Cloud Run (K_CONFIGURATION, K_SERVICE, K_REVISION set) and Cloud Run Job (CLOUD_RUN_JOB, CLOUD_RUN_EXECUTION, CLOUD_RUN_TASK_INDEX set), Detect sets cloud.platform=gcp_cloud_run and the corresponding faas.* and gcp.cloud_run.job.* attributes","On Cloud Functions (FUNCTION_TARGET set) and App Engine Standard (GAE_ENV=standard, GAE_SERVICE set), Detect sets cloud.platform=gcp_cloud_functions and gcp_app_engine respectively with their faas.* and cloud location attributes"],"goal":"verify gcp.NewDetector in pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0"],"schemaVersion":1,"symbols":["gcp.NewDetector"]},"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/contrib/detectors/gcp@v1.39.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0","symbols":["gcp.NewDetector"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.24.0

require (
	go.opentelemetry.io/contrib/detectors/gcp v1.39.0
	go.opentelemetry.io/otel v1.39.0
)

require (
	cloud.google.com/go/compute/metadata v0.9.0 // indirect
	github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.30.0 // indirect
	github.com/cespare/xxhash/v2 v2.3.0 // indirect
	github.com/go-logr/logr v1.4.3 // indirect
	github.com/go-logr/stdr v1.2.2 // indirect
	github.com/google/uuid v1.6.0 // indirect
	go.opentelemetry.io/auto/sdk v1.2.1 // indirect
	go.opentelemetry.io/otel/metric v1.39.0 // indirect
	go.opentelemetry.io/otel/sdk v1.39.0 // indirect
	go.opentelemetry.io/otel/trace v1.39.0 // indirect
	golang.org/x/sys v0.39.0 // indirect
)
go.sum
cloud.google.com/go/compute/metadata v0.9.0 h1:pDUj4QMoPejqq20dK0Pg2N4yG9zIkYGdBtwLoEkH9Zs=
cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.30.0 h1:sBEjpZlNHzK1voKq9695PJSX2o5NEXl7/OL3coiIY0c=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.30.0/go.mod h1:P4WPRUkOhJC13W//jWpyfJNDAIpvRbAUIYLX/4jtlE0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/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.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/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/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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/contrib/detectors/gcp v1.39.0 h1:kWRNZMsfBHZ+uHjiH4y7Etn2FK26LAGkNFw7RHv1DhE=
go.opentelemetry.io/contrib/detectors/gcp v1.39.0/go.mod h1:t/OGqzHBa5v6RHZwrDBJ2OirWc+4q/w2fTbLZwAKjTk=
go.opentelemetry.io/otel v1.39.0 h1:8yPrr/S0ND9QEfTfdP9V+SiwT4E0G7Y5MO7p85nis48=
go.opentelemetry.io/otel v1.39.0/go.mod h1:kLlFTywNWrFyEdH0oj2xK0bFYZtHRYUdv1NklR/tgc8=
go.opentelemetry.io/otel/metric v1.39.0 h1:d1UzonvEZriVfpNKEVmHXbdf909uGTOQjA0HF0Ls5Q0=
go.opentelemetry.io/otel/metric v1.39.0/go.mod h1:jrZSWL33sD7bBxg1xjrqyDjnuzTUB0x1nBERXd7Ftcs=
go.opentelemetry.io/otel/sdk v1.39.0 h1:nMLYcjVsvdui1B/4FRkwjzoRVsMK8uL/cj0OyhKzt18=
go.opentelemetry.io/otel/sdk v1.39.0/go.mod h1:vDojkC4/jsTJsE+kh+LXYQlbL8CgrEcwmt1ENZszdJE=
go.opentelemetry.io/otel/trace v1.39.0 h1:2d2vfpEDmCJ5zVYz7ijaJdOF59xLomrvj7bjt6/qCJI=
go.opentelemetry.io/otel/trace v1.39.0/go.mod h1:88w4/PnZSazkGzz/w84VHpQafiU4EtqqlVdxWy+rNOA=
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
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 gcp.NewDetector in pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.opentelemetry.io/contrib/detectors/gcp@v1.39.0"
  ],
  "symbols": [
    "gcp.NewDetector"
  ]
}
test/contract.go
package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"os"
	"strings"

	"go.opentelemetry.io/contrib/detectors/gcp"
	"go.opentelemetry.io/otel/attribute"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/computeMetadata/v1/project/project-id", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "demo-project")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/id", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "instance-101")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/region", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "projects/100/regions/us-central1")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/zone", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "projects/100/zones/us-central1-a")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/machine-type", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "e2-medium")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/name", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "vm-node-1")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/hostname", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "vm-node-1.internal")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/attributes/cluster-name", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "cluster-alpha")
	})
	mux.HandleFunc("/computeMetadata/v1/instance/attributes/cluster-location", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "us-central1-a")
	})

	srv := httptest.NewServer(mux)
	defer srv.Close()

	host := strings.TrimPrefix(srv.URL, "http://")
	os.Setenv("GCE_METADATA_HOST", host)

	clearEnv := func() {
		for _, k := range []string{
			"BMS_PROJECT_ID", "BMS_REGION", "BMS_INSTANCE_ID",
			"KUBERNETES_SERVICE_HOST",
			"FUNCTION_TARGET",
			"K_CONFIGURATION", "K_SERVICE", "K_REVISION",
			"CLOUD_RUN_JOB", "CLOUD_RUN_EXECUTION", "CLOUD_RUN_TASK_INDEX",
			"GAE_ENV", "GAE_SERVICE", "GAE_VERSION", "GAE_INSTANCE",
		} {
			os.Unsetenv(k)
		}
	}

	attrMap := func(kvs []attribute.KeyValue) map[string]string {
		m := make(map[string]string, len(kvs))
		for _, kv := range kvs {
			m[string(kv.Key)] = kv.Value.Emit()
		}
		return m
	}

	assertEq := func(name, got, want string) {
		if got != want {
			panic(fmt.Sprintf("%s: got %q, want %q", name, got, want))
		}
	}

	d := gcp.NewDetector()
	if d == nil {
		panic("gcp.NewDetector() returned nil")
	}

	// 1. GCE detection (no platform env vars set, metadata server responds to machine-type)
	clearEnv()
	resGCE, err := d.Detect(context.Background())
	if err != nil {
		panic(fmt.Sprintf("GCE Detect failed: %v", err))
	}
	mGCE := attrMap(resGCE.Attributes())
	assertEq("GCE cloud.provider", mGCE["cloud.provider"], "gcp")
	assertEq("GCE cloud.account.id", mGCE["cloud.account.id"], "demo-project")
	assertEq("GCE cloud.platform", mGCE["cloud.platform"], "gcp_compute_engine")
	assertEq("GCE cloud.availability_zone", mGCE["cloud.availability_zone"], "us-central1-a")
	assertEq("GCE cloud.region", mGCE["cloud.region"], "us-central1")
	assertEq("GCE host.type", mGCE["host.type"], "e2-medium")
	assertEq("GCE host.id", mGCE["host.id"], "instance-101")
	assertEq("GCE host.name", mGCE["host.name"], "vm-node-1")
	assertEq("GCE gcp.gce.instance.name", mGCE["gcp.gce.instance.name"], "vm-node-1")
	assertEq("GCE gcp.gce.instance.hostname", mGCE["gcp.gce.instance.hostname"], "vm-node-1.internal")

	// 2. GKE detection (KUBERNETES_SERVICE_HOST set + cluster-location metadata attribute)
	clearEnv()
	os.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1")
	resGKE, err := d.Detect(context.Background())
	if err != nil {
		panic(fmt.Sprintf("GKE Detect failed: %v", err))
	}
	mGKE := attrMap(resGKE.Attributes())
	assertEq("GKE cloud.provider", mGKE["cloud.provider"], "gcp")
	assertEq("GKE cloud.platform", mGKE["cloud.platform"], "gcp_kubernetes_engine")
	assertEq("GKE k8s.cluster.name", mGKE["k8s.cluster.name"], "cluster-alpha")
	assertEq("GKE cloud.availability_zone", mGKE["cloud.availability_zone"], "us-central1-a")
	assertEq("GKE host.id", mGKE["host.id"], "instance-101")

	// 3. Cloud Run service detection (K_CONFIGURATION, K_SERVICE, K_REVISION set)
	clearEnv()
	os.Setenv("K_CONFIGURATION", "cfg-1")
	os.Setenv("K_SERVICE", "svc-run")
	os.Setenv("K_REVISION", "rev-1")
	resRun, err := d.Detect(context.Background())
	if err != nil {
		panic(fmt.Sprintf("CloudRun Detect failed: %v", err))
	}
	mRun := attrMap(resRun.Attributes())
	assertEq("CloudRun cloud.platform", mRun["cloud.platform"], "gcp_cloud_run")
	assertEq("CloudRun faas.name", mRun["faas.name"], "svc-run")
	assertEq("CloudRun faas.version", mRun["faas.version"], "rev-1")
	assertEq("CloudRun faas.instance", mRun["faas.instance"], "instance-101")
	assertEq("CloudRun cloud.region", mRun["cloud.region"], "us-central1")

	// 4. Cloud Run Job detection (CLOUD_RUN_JOB, CLOUD_RUN_EXECUTION, CLOUD_RUN_TASK_INDEX set)
	clearEnv()
	os.Setenv("CLOUD_RUN_JOB", "job-batch")
	os.Setenv("CLOUD_RUN_EXECUTION", "exec-7")
	os.Setenv("CLOUD_RUN_TASK_INDEX", "2")
	resJob, err := d.Detect(context.Background())
	if err != nil {
		panic(fmt.Sprintf("CloudRunJob Detect failed: %v", err))
	}
	mJob := attrMap(resJob.Attributes())
	assertEq("CloudRunJob cloud.platform", mJob["cloud.platform"], "gcp_cloud_run")
	assertEq("CloudRunJob faas.name", mJob["faas.name"], "job-batch")
	assertEq("CloudRunJob gcp.cloud_run.job.execution", mJob["gcp.cloud_run.job.execution"], "exec-7")
	assertEq("CloudRunJob gcp.cloud_run.job.task_index", mJob["gcp.cloud_run.job.task_index"], "2")

	// 5. Cloud Functions detection (FUNCTION_TARGET, K_SERVICE, K_REVISION set)
	clearEnv()
	os.Setenv("FUNCTION_TARGET", "HandleEvent")
	os.Setenv("K_SERVICE", "fn-handler")
	os.Setenv("K_REVISION", "v3")
	resFn, err := d.Detect(context.Background())
	if err != nil {
		panic(fmt.Sprintf("CloudFunctions Detect failed: %v", err))
	}
	mFn := attrMap(resFn.Attributes())
	assertEq("CloudFunctions cloud.platform", mFn["cloud.platform"], "gcp_cloud_functions")
	assertEq("CloudFunctions faas.name", mFn["faas.name"], "fn-handler")
	assertEq("CloudFunctions faas.version", mFn["faas.version"], "v3")

	// 6. App Engine Standard detection (GAE_ENV=standard, GAE_SERVICE, GAE_VERSION, GAE_INSTANCE)
	clearEnv()
	os.Setenv("GAE_ENV", "standard")
	os.Setenv("GAE_SERVICE", "default-app")
	os.Setenv("GAE_VERSION", "20260101t000000")
	os.Setenv("GAE_INSTANCE", "gae-inst-9")
	resGAE, err := d.Detect(context.Background())
	if err != nil {
		panic(fmt.Sprintf("GAE Standard Detect failed: %v", err))
	}
	mGAE := attrMap(resGAE.Attributes())
	assertEq("GAE cloud.platform", mGAE["cloud.platform"], "gcp_app_engine")
	assertEq("GAE faas.name", mGAE["faas.name"], "default-app")
	assertEq("GAE faas.version", mGAE["faas.version"], "20260101t000000")
	assertEq("GAE faas.instance", mGAE["faas.instance"], "gae-inst-9")
	assertEq("GAE cloud.availability_zone", mGAE["cloud.availability_zone"], "us-central1-a")
	assertEq("GAE cloud.region", mGAE["cloud.region"], "us-central1")
}

Ursprungs-Seeder

anonym