Пример
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0: WithTemporalitySelector
Проверенный пример — golang go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0: WithTemporalitySelector. Контракт выполнен на go 1.26…
sha256:e70866a82ca47fb4e5a27e7c1e0d9e1119d2392145b6a4c6a7ed1001522ba008
Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла.
Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди.
MIT-0
Свидетельства выполнения
Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.
- Основа свидетельства
- Подписанный контракт пройден
- Квитанции проверки
- 2
- Ключи подписи, собравшие его
- 1
Заявленная среда
go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go
Среды запусков проверки
| Окружение | Контракт | Этапы | Запуск |
|---|---|---|---|
| go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 | FAIL | compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a… |
2026-09-03 |
| 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-03 |
Кейс
HOW- Цель
- verify WithTemporalitySelector in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0
- Символы
-
- WithTemporalitySelector
- Окружение
- go 1.26
- Создан
- 2026-09-03T20:32:06Z
Контракт
- Default exporter without WithTemporalitySelector uses cumulative temporality for all instrument kinds
- WithTemporalitySelector configures custom delta temporality selector on OTLP HTTP metric exporter
- WithTemporalitySelector configures explicit cumulative temporality selector
- Exporter configured with WithTemporalitySelector integrates with MeterProvider and flushes metrics via HTTP
- Shutdown cleanly terminates meter provider and underlying exporter configured with WithTemporalitySelector
Файлы
- PROMPT.md
- csx.json
- go.mod
- go.sum
- spec.json
- temporality.go
- test/contract_test.go
Исходный код
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 WithTemporalitySelector in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0
Demonstrate these symbols/APIs:
- WithTemporalitySelector
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:c0dddadf3937d5262d1663779e9b80e3a4a13854b1c67602f2327420c237472e","contract":["Default exporter without WithTemporalitySelector uses cumulative temporality for all instrument kinds","WithTemporalitySelector configures custom delta temporality selector on OTLP HTTP metric exporter","WithTemporalitySelector configures explicit cumulative temporality selector","Exporter configured with WithTemporalitySelector integrates with MeterProvider and flushes metrics via HTTP","Shutdown cleanly terminates meter provider and underlying exporter configured with WithTemporalitySelector"],"goal":"verify WithTemporalitySelector in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0","kind":"HOW","packages":["pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0"],"schemaVersion":1,"symbols":["WithTemporalitySelector"]},"contractCommand":["go","test","-v","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","moduleSystem":"go.mod","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","runtimeVersion":"1.26","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0"],"schemaVersion":1,"subject":"pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0","symbols":["WithTemporalitySelector"],"verifierAdapter":"golang@1"}
module sample
go 1.26.6
require (
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0
go.opentelemetry.io/otel/sdk/metric v1.46.0
)
require (
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/go-logr/logr v1.4.4 // 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.30.0 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel v1.46.0 // indirect
go.opentelemetry.io/otel/metric v1.46.0 // indirect
go.opentelemetry.io/otel/sdk v1.46.0 // indirect
go.opentelemetry.io/otel/trace v1.46.0 // indirect
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
golang.org/x/net v0.58.0 // indirect
golang.org/x/sys v0.47.0 // indirect
golang.org/x/text v0.41.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688 // indirect
google.golang.org/grpc v1.83.1 // indirect
google.golang.org/protobuf v1.36.12 // indirect
)
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
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/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
github.com/go-logr/logr v1.4.4/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.30.0 h1:/Tnpcb2E0Pz/tN9s3bfEY2Q8ePCEX9iuS+cneUwncnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0/go.mod h1:zOBXOsUaBSjKgmH4OGzV1esUpR3oUSCPYVd2cUBjKYY=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
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/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc=
go.opentelemetry.io/otel v1.46.0/go.mod h1:Gj3SEScelsNC45tp4nSxRYlS+f5iez7W8XPMCt905kE=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0 h1:AP23h/mFgb/lc7tdck1Kfn9qxsM8TAeNPCU5C3pzaps=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.46.0/go.mod h1:K4EqCe1b4kGk5WR690ntg9LaBfsPoV32FwthbyoptuA=
go.opentelemetry.io/otel/metric v1.46.0 h1:yBnkXvgV7AXFILZc5K6IZe/CBFF3OS7BJ8ov6/lj0K8=
go.opentelemetry.io/otel/metric v1.46.0/go.mod h1:iPmdWqifKUdzziPkvvzIJXITl56fQx2mGM/DHLB3/2o=
go.opentelemetry.io/otel/metric/x v0.68.0 h1:TA/cBT23D3MnxYPwHL7YFOdYGdx0A0v+s7Mzotpd1dU=
go.opentelemetry.io/otel/metric/x v0.68.0/go.mod h1:agudOmvWhwUTjgibWDzxD2PoWYnpw5Ht5jISYOD2Hd4=
go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI=
go.opentelemetry.io/otel/sdk v1.46.0/go.mod h1:GAERFXFt5SYCEB+YiKUbMBeza6UaDH7GmGOZEfh2gSM=
go.opentelemetry.io/otel/sdk/metric v1.46.0 h1:0piZ26EG4RBfebb2jhDH6ERCYHoVWduc3kLgPCwSnSE=
go.opentelemetry.io/otel/sdk/metric v1.46.0/go.mod h1:I1PbKrdVc8Qu8HYVDNtqVIwLwjNrhsV/uFuxfwg8mO4=
go.opentelemetry.io/otel/trace v1.46.0 h1:OULy7ccdJnZtJ0UDYFOIGaCmiWzJ8Vi2G/Rsu60qs1c=
go.opentelemetry.io/otel/trace v1.46.0/go.mod h1:J7GAXweO77XSFkB/rmAqk9D6ihszhFjLU+d9WuUxDLI=
go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6TbEdMIk=
go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To=
golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688 h1:ax2KzoSRIZU/M0cIxri3pKxy99vniH1PVxWC6si/eZI=
google.golang.org/genproto/googleapis/api v0.0.0-20260819154853-08b0e4226688/go.mod h1:1RJ9BQGyNdZwkGc1eTqkErfRZ6RJyYPHZo73BZ1vQqI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688 h1:cYNAzI2sUwhmCcoj9TxvihSrqsxt6uIkj3rDRhSDmW4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260819154853-08b0e4226688/go.mod h1:DjtHYE8FKJLivXcBEjGwndXfIC23G0VpXiXKqG179uA=
google.golang.org/grpc v1.83.1 h1:HIO0+BEtBP6soyqvqC8sNUjZ7bTs+0hFQuFF+RAy++Y=
google.golang.org/grpc v1.83.1/go.mod h1:kDyl6SKsiHKt0uylY5gtn5cEjkrIOhQOGDgIc4JGwzQ=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
{
"schemaVersion": 1,
"goal": "verify WithTemporalitySelector in pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0",
"kind": "HOW",
"packages": [
"pkg:golang/go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp@v1.46.0"
],
"symbols": [
"WithTemporalitySelector"
]
}
package sample
import (
"context"
"fmt"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)
// DeltaTemporalitySelector selects DeltaTemporality for cumulative-by-default instruments (counter, histogram)
// and CumulativeTemporality for gauge-like instruments.
func DeltaTemporalitySelector(kind sdkmetric.InstrumentKind) metricdata.Temporality {
switch kind {
case sdkmetric.InstrumentKindCounter,
sdkmetric.InstrumentKindUpDownCounter,
sdkmetric.InstrumentKindHistogram,
sdkmetric.InstrumentKindObservableCounter,
sdkmetric.InstrumentKindObservableUpDownCounter:
return metricdata.DeltaTemporality
default:
return metricdata.CumulativeTemporality
}
}
// CumulativeTemporalitySelector selects CumulativeTemporality for all instrument kinds.
func CumulativeTemporalitySelector(kind sdkmetric.InstrumentKind) metricdata.Temporality {
return metricdata.CumulativeTemporality
}
// NewExporterWithTemporality creates an OTLP HTTP metric exporter configured with the specified TemporalitySelector and additional options.
func NewExporterWithTemporality(ctx context.Context, selector sdkmetric.TemporalitySelector, opts ...otlpmetrichttp.Option) (*otlpmetrichttp.Exporter, error) {
allOpts := append([]otlpmetrichttp.Option{otlpmetrichttp.WithTemporalitySelector(selector)}, opts...)
exp, err := otlpmetrichttp.New(ctx, allOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create otlp metric http exporter with temporality selector: %w", err)
}
return exp, nil
}
// NewMeterProvider creates an SDK MeterProvider wrapping the given exporter with a PeriodicReader.
func NewMeterProvider(exp sdkmetric.Exporter) *sdkmetric.MeterProvider {
reader := sdkmetric.NewPeriodicReader(exp)
return sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader))
}
package test
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
sample "sample"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)
func TestWithTemporalitySelectorContract(t *testing.T) {
ctx := context.Background()
// 1. Verify default exporter uses cumulative temporality for all instrument kinds
defaultExp, err := otlpmetrichttp.New(ctx)
if err != nil {
t.Fatalf("failed to create default exporter: %v", err)
}
defer func() {
_ = defaultExp.Shutdown(ctx)
}()
if got := defaultExp.Temporality(sdkmetric.InstrumentKindCounter); got != metricdata.CumulativeTemporality {
t.Errorf("expected default counter temporality %v, got %v", metricdata.CumulativeTemporality, got)
}
if got := defaultExp.Temporality(sdkmetric.InstrumentKindHistogram); got != metricdata.CumulativeTemporality {
t.Errorf("expected default histogram temporality %v, got %v", metricdata.CumulativeTemporality, got)
}
if got := defaultExp.Temporality(sdkmetric.InstrumentKindGauge); got != metricdata.CumulativeTemporality {
t.Errorf("expected default gauge temporality %v, got %v", metricdata.CumulativeTemporality, got)
}
// 2. Verify WithTemporalitySelector configures custom delta temporality selector
deltaExp, err := sample.NewExporterWithTemporality(
ctx,
sample.DeltaTemporalitySelector,
otlpmetrichttp.WithInsecure(),
otlpmetrichttp.WithEndpoint("localhost:4318"),
)
if err != nil {
t.Fatalf("failed to create delta exporter: %v", err)
}
defer func() {
_ = deltaExp.Shutdown(ctx)
}()
if got := deltaExp.Temporality(sdkmetric.InstrumentKindCounter); got != metricdata.DeltaTemporality {
t.Errorf("expected counter temporality %v, got %v", metricdata.DeltaTemporality, got)
}
if got := deltaExp.Temporality(sdkmetric.InstrumentKindUpDownCounter); got != metricdata.DeltaTemporality {
t.Errorf("expected updown counter temporality %v, got %v", metricdata.DeltaTemporality, got)
}
if got := deltaExp.Temporality(sdkmetric.InstrumentKindHistogram); got != metricdata.DeltaTemporality {
t.Errorf("expected histogram temporality %v, got %v", metricdata.DeltaTemporality, got)
}
if got := deltaExp.Temporality(sdkmetric.InstrumentKindGauge); got != metricdata.CumulativeTemporality {
t.Errorf("expected gauge temporality %v, got %v", metricdata.CumulativeTemporality, got)
}
// 3. Direct invocation of otlpmetrichttp.WithTemporalitySelector with explicit cumulative selector
cumulExp, err := otlpmetrichttp.New(
ctx,
otlpmetrichttp.WithTemporalitySelector(sample.CumulativeTemporalitySelector),
otlpmetrichttp.WithInsecure(),
)
if err != nil {
t.Fatalf("failed to create cumulative exporter: %v", err)
}
if got := cumulExp.Temporality(sdkmetric.InstrumentKindCounter); got != metricdata.CumulativeTemporality {
t.Errorf("expected explicit cumulative counter temporality %v, got %v", metricdata.CumulativeTemporality, got)
}
_ = cumulExp.Shutdown(ctx)
// 4. Integration with httptest server: exporter with WithTemporalitySelector flushes metrics
var requestReceived atomic.Bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestReceived.Store(true)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
serverEndpoint := server.Listener.Addr().String()
exportExp, err := otlpmetrichttp.New(
ctx,
otlpmetrichttp.WithTemporalitySelector(sample.DeltaTemporalitySelector),
otlpmetrichttp.WithEndpoint(serverEndpoint),
otlpmetrichttp.WithInsecure(),
)
if err != nil {
t.Fatalf("failed to create exporter for test server: %v", err)
}
mp := sample.NewMeterProvider(exportExp)
meter := mp.Meter("test-meter")
counter, err := meter.Int64Counter("test_counter")
if err != nil {
t.Fatalf("failed to create counter: %v", err)
}
counter.Add(ctx, 10)
if err := mp.ForceFlush(ctx); err != nil {
t.Fatalf("force flush failed: %v", err)
}
if !requestReceived.Load() {
t.Fatal("expected exporter to deliver metrics HTTP request to test server")
}
// 5. Shutdown cleanly terminates meter provider and underlying exporter
if err := mp.Shutdown(ctx); err != nil {
t.Fatalf("meter provider shutdown failed: %v", err)
}
}
Исходный сидер
аноним