CodeSampleX

샘플

github.com/prometheus/otlptranslator v1.0.0

검증된 샘플 — golang github.com/prometheus/otlptranslator v1.0.0. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: assert…

sha256:7851f842ba01d7d0bb3fa32fb8eed4d2590f905b992417a9720c4af45d3a3bc3

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

케이스

HOW
목표
verify pkg:golang/github.com/prometheus/otlptranslator@v1.0.0
패키지
생성일
2026-09-11T10:41:39Z

컨트랙트

  1. assert UnderscoreEscapingWithSuffixes enables escaping
  2. assert UnderscoreEscapingWithSuffixes enables suffix addition
  3. assert UnderscoreEscapingWithoutSuffixes enables escaping
  4. assert UnderscoreEscapingWithoutSuffixes disables suffix addition
  5. assert NoUTF8EscapingWithSuffixes disables escaping
  6. assert NoUTF8EscapingWithSuffixes enables suffix addition
  7. assert NoTranslation disables escaping
  8. assert NoTranslation disables suffix addition
  9. assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes counter name and appends _total
  10. assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes gauge name and translates By unit to bytes
  11. assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes histogram name and translates s unit to seconds
  12. assert MetricNamer prepends configured namespace to normalized metric name
  13. assert MetricNamer with UnderscoreEscapingWithoutSuffixes escapes characters without appending type or unit suffixes
  14. assert MetricNamer with NoTranslation preserves original metric name unmodified
  15. assert LabelNamer replaces dots with underscores when UTF8Allowed is false
  16. assert LabelNamer prepends key_ to label names starting with digit
  17. assert LabelNamer preserves reserved double underscore label names
  18. assert LabelNamer returns error when label name is empty

파일

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

소스 아티팩트 내려받기 (tar.gz)

소스

NOTES.md
# pkg:golang/github.com/prometheus/otlptranslator@v1.0.0

This code sample demonstrates translating OpenTelemetry metric and attribute names to Prometheus-compliant formats using `github.com/prometheus/otlptranslator@v1.0.0`.

## Verified Features

1. **TranslationStrategyOption**:
   - `UnderscoreEscapingWithSuffixes`: Escapes invalid characters to underscores and appends type/unit suffixes.
   - `UnderscoreEscapingWithoutSuffixes`: Escapes invalid characters to underscores without appending suffixes.
   - `NoUTF8EscapingWithSuffixes`: Preserves UTF-8 characters while appending suffixes.
   - `NoTranslation`: Preserves incoming metric and label names without translation.

2. **MetricNamer**:
   - Translates metric names according to Prometheus conventions.
   - Appends unit suffixes (e.g., `By` to `_bytes`, `s` to `_seconds`).
   - Appends type suffixes (e.g., `_total` for monotonic counters).
   - Supports namespace prefixing.

3. **LabelNamer**:
   - Replaces dots with underscores when UTF-8 escaping is required.
   - Prepends `key_` to labels starting with digits.
   - Preserves reserved double-underscore labels.
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 pkg:golang/github.com/prometheus/otlptranslator@v1.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/otlptranslator@v1.0.0

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:9558170fbe9a02cf57540af3045af22408a55622edac468e8b5f6df8f9fbc902","contract":["assert UnderscoreEscapingWithSuffixes enables escaping","assert UnderscoreEscapingWithSuffixes enables suffix addition","assert UnderscoreEscapingWithoutSuffixes enables escaping","assert UnderscoreEscapingWithoutSuffixes disables suffix addition","assert NoUTF8EscapingWithSuffixes disables escaping","assert NoUTF8EscapingWithSuffixes enables suffix addition","assert NoTranslation disables escaping","assert NoTranslation disables suffix addition","assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes counter name and appends _total","assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes gauge name and translates By unit to bytes","assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes histogram name and translates s unit to seconds","assert MetricNamer prepends configured namespace to normalized metric name","assert MetricNamer with UnderscoreEscapingWithoutSuffixes escapes characters without appending type or unit suffixes","assert MetricNamer with NoTranslation preserves original metric name unmodified","assert LabelNamer replaces dots with underscores when UTF8Allowed is false","assert LabelNamer prepends key_ to label names starting with digit","assert LabelNamer preserves reserved double underscore label names","assert LabelNamer returns error when label name is empty"],"goal":"verify pkg:golang/github.com/prometheus/otlptranslator@v1.0.0","kind":"HOW","packages":["pkg:golang/github.com/prometheus/otlptranslator@v1.0.0"],"schemaVersion":1},"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/github.com/prometheus/otlptranslator@v1.0.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/otlptranslator@v1.0.0","verifierAdapter":"golang@1"}
go.mod
module sample

go 1.23.0

require github.com/prometheus/otlptranslator v1.0.0
go.sum
github.com/prometheus/otlptranslator v1.0.0 h1:s0LJW/iN9dkIH+EnhiD3BlkkP5QVIUVEoIwkU+A6qos=
github.com/prometheus/otlptranslator v1.0.0/go.mod h1:vRYWnXvI6aWGpsdY/mOT/cbeVRBlPWtBNDb7kGR3uKM=
main.go
package main

import (
	"fmt"

	"github.com/prometheus/otlptranslator"
)

func main() {
	namer := otlptranslator.NewMetricNamer("myapp", otlptranslator.UnderscoreEscapingWithSuffixes)
	metric := otlptranslator.Metric{
		Name: "http.server.requests",
		Unit: "1",
		Type: otlptranslator.MetricTypeMonotonicCounter,
	}
	promMetric, err := namer.Build(metric)
	if err != nil {
		panic(err)
	}

	labelNamer := otlptranslator.LabelNamer{UTF8Allowed: false}
	promLabel, err := labelNamer.Build("http.status_code")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s{%s=\"200\"}\n", promMetric, promLabel)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/prometheus/otlptranslator@v1.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/otlptranslator@v1.0.0"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"github.com/prometheus/otlptranslator"
)

func assert(cond bool, msg string) {
	if !cond {
		fmt.Fprintf(os.Stderr, "FAIL: %s\n", msg)
		os.Exit(1)
	}
	fmt.Printf("PASS: %s\n", msg)
}

func main() {
	// 1. TranslationStrategyOption behavior
	assert(otlptranslator.UnderscoreEscapingWithSuffixes.ShouldEscape() == true, "assert UnderscoreEscapingWithSuffixes enables escaping")
	assert(otlptranslator.UnderscoreEscapingWithSuffixes.ShouldAddSuffixes() == true, "assert UnderscoreEscapingWithSuffixes enables suffix addition")
	assert(otlptranslator.UnderscoreEscapingWithoutSuffixes.ShouldEscape() == true, "assert UnderscoreEscapingWithoutSuffixes enables escaping")
	assert(otlptranslator.UnderscoreEscapingWithoutSuffixes.ShouldAddSuffixes() == false, "assert UnderscoreEscapingWithoutSuffixes disables suffix addition")
	assert(otlptranslator.NoUTF8EscapingWithSuffixes.ShouldEscape() == false, "assert NoUTF8EscapingWithSuffixes disables escaping")
	assert(otlptranslator.NoUTF8EscapingWithSuffixes.ShouldAddSuffixes() == true, "assert NoUTF8EscapingWithSuffixes enables suffix addition")
	assert(otlptranslator.NoTranslation.ShouldEscape() == false, "assert NoTranslation disables escaping")
	assert(otlptranslator.NoTranslation.ShouldAddSuffixes() == false, "assert NoTranslation disables suffix addition")

	// 2. MetricNamer with UnderscoreEscapingWithSuffixes
	namerDefault := otlptranslator.NewMetricNamer("", otlptranslator.UnderscoreEscapingWithSuffixes)

	// Counter gets _total
	counterMetric := otlptranslator.Metric{
		Name: "http.server.requests",
		Unit: "1",
		Type: otlptranslator.MetricTypeMonotonicCounter,
	}
	counterName, err := namerDefault.Build(counterMetric)
	assert(err == nil && counterName == "http_server_requests_total", "assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes counter name and appends _total")

	// Gauge with byte unit
	gaugeMetric := otlptranslator.Metric{
		Name: "system.memory.usage",
		Unit: "By",
		Type: otlptranslator.MetricTypeGauge,
	}
	gaugeName, err := namerDefault.Build(gaugeMetric)
	assert(err == nil && gaugeName == "system_memory_usage_bytes", "assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes gauge name and translates By unit to bytes")

	// Histogram with seconds unit
	histMetric := otlptranslator.Metric{
		Name: "http.server.request.duration",
		Unit: "s",
		Type: otlptranslator.MetricTypeHistogram,
	}
	histName, err := namerDefault.Build(histMetric)
	assert(err == nil && histName == "http_server_request_duration_seconds", "assert MetricNamer with UnderscoreEscapingWithSuffixes normalizes histogram name and translates s unit to seconds")

	// MetricNamer with custom namespace
	namerNamespace := otlptranslator.NewMetricNamer("app", otlptranslator.UnderscoreEscapingWithSuffixes)
	nsName, err := namerNamespace.Build(gaugeMetric)
	assert(err == nil && nsName == "app_system_memory_usage_bytes", "assert MetricNamer prepends configured namespace to normalized metric name")

	// 3. MetricNamer with UnderscoreEscapingWithoutSuffixes
	namerNoSuffixes := otlptranslator.NewMetricNamer("", otlptranslator.UnderscoreEscapingWithoutSuffixes)
	noSuffixName, err := namerNoSuffixes.Build(counterMetric)
	assert(err == nil && noSuffixName == "http_server_requests", "assert MetricNamer with UnderscoreEscapingWithoutSuffixes escapes characters without appending type or unit suffixes")

	// 4. MetricNamer with NoTranslation
	namerNoTrans := otlptranslator.NewMetricNamer("", otlptranslator.NoTranslation)
	noTransName, err := namerNoTrans.Build(histMetric)
	assert(err == nil && noTransName == "http.server.request.duration", "assert MetricNamer with NoTranslation preserves original metric name unmodified")

	// 5. LabelNamer normalization
	labelNamerLegacy := otlptranslator.LabelNamer{UTF8Allowed: false}
	lbl1, err := labelNamerLegacy.Build("http.status_code")
	assert(err == nil && lbl1 == "http_status_code", "assert LabelNamer replaces dots with underscores when UTF8Allowed is false")

	lblDigit, err := labelNamerLegacy.Build("99_bottles")
	assert(err == nil && lblDigit == "key_99_bottles", "assert LabelNamer prepends key_ to label names starting with digit")

	lblReserved, err := labelNamerLegacy.Build("__reserved_label__")
	assert(err == nil && lblReserved == "__reserved_label__", "assert LabelNamer preserves reserved double underscore label names")

	_, errEmpty := labelNamerLegacy.Build("")
	assert(errEmpty != nil, "assert LabelNamer returns error when label name is empty")

	fmt.Println("ALL CONTRACT TESTS PASSED")
}

오리진 시더

익명