Sample
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0: NewServerHandler
Verified sample for golang go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0: NewServerHandler. The contract ran on go 1.26…
sha256:ce5a3b1fdc5e84bb18849e2d01c13ddfb88e352b7f4028ac313fb0ce404bd0e0
This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured.
How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people.
MIT-0
Execution evidence
The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.
- Evidence basis
- Signed contract pass
- Verification receipts
- 1
- Signing keys that built it
- 1
Declared environment
go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1
Verification-run environments
| Environment | Contract | Stages | Run |
|---|---|---|---|
| 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-13 |
Case
HOW- Goal
- verify go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler in pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0
- Symbols
-
- go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler
- Environment
- go 1.26.6
- Created
- 2026-10-13T01:46:29Z
Contract
- otelgrpc.NewServerHandler returns a non-nil stats.Handler compatible with grpc.StatsHandler server option
- otelgrpc.NewServerHandler TagRPC starts a valid server span with RPC method attributes
- otelgrpc.NewServerHandler WithFilter skips tracing when filter returns false
- otelgrpc.NewServerHandler WithSpanAttributes adds custom attributes to recorded server spans
- otelgrpc.NewServerHandler TagConn preserves context and HandleRPC ends span on completion
Files
- PROMPT.md
- csx.json
- go.mod
- go.sum
- main.go
- spec.json
- test/contract.go
Source
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/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler in pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0
Demonstrate these symbols/APIs:
- go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler
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.
{"case":{"caseId":"case:sha256:7ed51376b841b58985ed571ceaa77cb681a4c4144b0a46e1f780dd6555c2226b","contract":["otelgrpc.NewServerHandler returns a non-nil stats.Handler compatible with grpc.StatsHandler server option","otelgrpc.NewServerHandler TagRPC starts a valid server span with RPC method attributes","otelgrpc.NewServerHandler WithFilter skips tracing when filter returns false","otelgrpc.NewServerHandler WithSpanAttributes adds custom attributes to recorded server spans","otelgrpc.NewServerHandler TagConn preserves context and HandleRPC ends span on completion"],"goal":"verify go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler in pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0"],"schemaVersion":1,"symbols":["go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0","symbols":["go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler"],"verifierAdapter":"golang@1"}
module sample
go 1.26.6
require (
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0
go.opentelemetry.io/otel v1.42.0
go.opentelemetry.io/otel/sdk v1.42.0
go.opentelemetry.io/otel/trace v1.42.0
google.golang.org/grpc v1.79.2
)
require (
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.42.0 // indirect
golang.org/x/net v0.51.0 // indirect
golang.org/x/sys v0.41.0 // indirect
golang.org/x/text v0.34.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 // indirect
google.golang.org/protobuf v1.36.11 // indirect
)
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.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.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/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/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.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/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0 h1:yI1/OhfEPy7J9eoa6Sj051C7n5dvpj0QX8g4sRchg04=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.67.0/go.mod h1:NoUCKYWK+3ecatC4HjkRktREheMeEtrXoQxrqYFeHSc=
go.opentelemetry.io/otel v1.42.0 h1:lSQGzTgVR3+sgJDAU/7/ZMjN9Z+vUip7leaqBKy4sho=
go.opentelemetry.io/otel v1.42.0/go.mod h1:lJNsdRMxCUIWuMlVJWzecSMuNjE7dOYyWlqOXWkdqCc=
go.opentelemetry.io/otel/metric v1.42.0 h1:2jXG+3oZLNXEPfNmnpxKDeZsFI5o4J+nz6xUlaFdF/4=
go.opentelemetry.io/otel/metric v1.42.0/go.mod h1:RlUN/7vTU7Ao/diDkEpQpnz3/92J9ko05BIwxYa2SSI=
go.opentelemetry.io/otel/sdk v1.42.0 h1:LyC8+jqk6UJwdrI/8VydAq/hvkFKNHZVIWuslJXYsDo=
go.opentelemetry.io/otel/sdk v1.42.0/go.mod h1:rGHCAxd9DAph0joO4W6OPwxjNTYWghRWmkHuGbayMts=
go.opentelemetry.io/otel/sdk/metric v1.42.0 h1:D/1QR46Clz6ajyZ3G8SgNlTJKBdGp84q9RKCAZ3YGuA=
go.opentelemetry.io/otel/sdk/metric v1.42.0/go.mod h1:Ua6AAlDKdZ7tdvaQKfSmnFTdHx37+J4ba8MwVCYM5hc=
go.opentelemetry.io/otel/trace v1.42.0 h1:OUCgIPt+mzOnaUTpOQcBiM/PLQ/Op7oq6g4LenLmOYY=
go.opentelemetry.io/otel/trace v1.42.0/go.mod h1:f3K9S+IFqnumBkKhRJMeaZeNk9epyhnCmQh/EysQCdc=
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.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171 h1:ggcbiqK8WWh6l1dnltU4BgWGIGo+EVYxCaAPih/zQXQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/grpc v1.79.2 h1:fRMD94s2tITpyJGtBBn7MkMseNpOZU8ZxgC3MMBaXRU=
google.golang.org/grpc v1.79.2/go.mod h1:KmT0Kjez+0dde/v2j9vzwoAScgEPx/Bw1CYChhHLrHQ=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package main
import (
"context"
"fmt"
"time"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/stats"
)
func main() {
// 1. Setup in-memory tracer provider to observe OpenTelemetry traces
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
defer func() {
_ = tp.Shutdown(context.Background())
}()
// 2. Initialize otelgrpc server handler with custom tracer provider and span attributes
serverHandler := otelgrpc.NewServerHandler(
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithSpanAttributes(
attribute.String("service.environment", "production"),
),
)
// 3. Register stats handler with gRPC server
server := grpc.NewServer(grpc.StatsHandler(serverHandler))
_ = server // gRPC server configured with OpenTelemetry stats handler
// 4. Simulate gRPC server handling an incoming RPC call
ctx := context.Background()
tagInfo := &stats.RPCTagInfo{
FullMethodName: "/helloworld.Greeter/SayHello",
}
// TagRPC extracts incoming trace context and starts server span
rpcCtx := serverHandler.TagRPC(ctx, tagInfo)
span := trace.SpanFromContext(rpcCtx)
fmt.Printf("Active server span valid: %t, span ID: %s\n",
span.SpanContext().IsValid(),
span.SpanContext().SpanID().String(),
)
// HandleRPC ends the span upon RPC completion
serverHandler.HandleRPC(rpcCtx, &stats.End{
EndTime: time.Now(),
})
// 5. Inspect recorded span in exporter
spans := exporter.GetSpans()
fmt.Printf("Recorded %d span(s)\n", len(spans))
if len(spans) > 0 {
fmt.Printf("Span name: %s\n", spans[0].Name)
}
fmt.Println("otelgrpc.NewServerHandler demonstrated successfully.")
}
{
"schemaVersion": 1,
"goal": "verify go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler in pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0",
"kind": "HOW",
"packages": [
"pkg:golang/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc@v0.67.0"
],
"symbols": [
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.NewServerHandler"
]
}
package main
import (
"context"
"fmt"
"os"
"time"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/stats"
)
func main() {
// Assertion 1: otelgrpc.NewServerHandler returns a non-nil stats.Handler compatible with grpc.StatsHandler server option
{
handler := otelgrpc.NewServerHandler()
if handler == nil {
fmt.Fprintf(os.Stderr, "assertion 1 failed: NewServerHandler returned nil\n")
os.Exit(1)
}
server := grpc.NewServer(grpc.StatsHandler(handler))
if server == nil {
fmt.Fprintf(os.Stderr, "assertion 1 failed: grpc.NewServer with StatsHandler returned nil\n")
os.Exit(1)
}
}
// Assertion 2: otelgrpc.NewServerHandler TagRPC starts a valid server span with RPC method attributes
{
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
handler := otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(tp))
ctx := context.Background()
info := &stats.RPCTagInfo{
FullMethodName: "/helloworld.Greeter/SayHello",
}
taggedCtx := handler.TagRPC(ctx, info)
span := trace.SpanFromContext(taggedCtx)
if !span.SpanContext().IsValid() {
fmt.Fprintf(os.Stderr, "assertion 2 failed: expected valid span context from TagRPC\n")
os.Exit(1)
}
if span.SpanContext().IsRemote() {
fmt.Fprintf(os.Stderr, "assertion 2 failed: expected local server span, got remote\n")
os.Exit(1)
}
}
// Assertion 3: otelgrpc.NewServerHandler WithFilter skips tracing when filter returns false
{
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
handler := otelgrpc.NewServerHandler(
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithFilter(func(info *stats.RPCTagInfo) bool {
return info.FullMethodName != "/grpc.health.v1.Health/Check"
}),
)
ctx := context.Background()
info := &stats.RPCTagInfo{
FullMethodName: "/grpc.health.v1.Health/Check",
}
taggedCtx := handler.TagRPC(ctx, info)
span := trace.SpanFromContext(taggedCtx)
if span.SpanContext().IsValid() {
fmt.Fprintf(os.Stderr, "assertion 3 failed: expected invalid (noop) span when filter returns false\n")
os.Exit(1)
}
}
// Assertion 4: otelgrpc.NewServerHandler WithSpanAttributes adds custom attributes to recorded server spans
{
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
customKey := attribute.Key("custom.service.role")
customVal := "api-gateway"
handler := otelgrpc.NewServerHandler(
otelgrpc.WithTracerProvider(tp),
otelgrpc.WithSpanAttributes(customKey.String(customVal)),
)
ctx := context.Background()
info := &stats.RPCTagInfo{
FullMethodName: "/order.OrderService/CreateOrder",
}
taggedCtx := handler.TagRPC(ctx, info)
handler.HandleRPC(taggedCtx, &stats.End{EndTime: time.Now()})
spans := exporter.GetSpans()
if len(spans) != 1 {
fmt.Fprintf(os.Stderr, "assertion 4 failed: expected 1 recorded span, got %d\n", len(spans))
os.Exit(1)
}
if spans[0].Name != "order.OrderService/CreateOrder" {
fmt.Fprintf(os.Stderr, "assertion 4 failed: expected span name 'order.OrderService/CreateOrder', got %q\n", spans[0].Name)
os.Exit(1)
}
foundCustom := false
for _, attr := range spans[0].Attributes {
if attr.Key == customKey && attr.Value.AsString() == customVal {
foundCustom = true
break
}
}
if !foundCustom {
fmt.Fprintf(os.Stderr, "assertion 4 failed: custom attribute %q not found in span attributes\n", customKey)
os.Exit(1)
}
}
// Assertion 5: otelgrpc.NewServerHandler TagConn preserves context and HandleRPC ends span on completion
{
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter))
handler := otelgrpc.NewServerHandler(otelgrpc.WithTracerProvider(tp))
type ctxKey string
baseCtx := context.WithValue(context.Background(), ctxKey("conn-id"), "conn-42")
connCtx := handler.TagConn(baseCtx, &stats.ConnTagInfo{})
if connCtx.Value(ctxKey("conn-id")) != "conn-42" {
fmt.Fprintf(os.Stderr, "assertion 5 failed: TagConn did not preserve base context value\n")
os.Exit(1)
}
handler.HandleConn(connCtx, &stats.ConnBegin{})
rpcInfo := &stats.RPCTagInfo{
FullMethodName: "/echo.EchoService/Echo",
}
taggedCtx := handler.TagRPC(connCtx, rpcInfo)
now := time.Now()
handler.HandleRPC(taggedCtx, &stats.End{EndTime: now})
spans := exporter.GetSpans()
if len(spans) != 1 {
fmt.Fprintf(os.Stderr, "assertion 5 failed: expected 1 span, got %d\n", len(spans))
os.Exit(1)
}
if spans[0].EndTime.IsZero() {
fmt.Fprintf(os.Stderr, "assertion 5 failed: span EndTime is zero\n")
os.Exit(1)
}
}
fmt.Println("All contract assertions passed successfully.")
}
Origin Seeder
anonymous