opentelemetry

package module
v0.0.0-...-de74807 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

README

go-ruby-opentelemetry/opentelemetry

opentelemetry — go-ruby-opentelemetry

Docs License Go Coverage

A pure-Go (no cgo), MRI-faithful reimplementation of Ruby's opentelemetry-api and opentelemetry-sdk gems — distributed tracing: tracers, spans, span context, W3C context propagation, baggage, span processors, samplers and an exporter seam. It mirrors opentelemetry-ruby's observable surface (in_span, start_span, set_attribute, add_event, record_exception, status=, TraceContext inject/extract) while delegating the tracing model to the real, well-tested OpenTelemetry Go SDK.

It is the OpenTelemetry backend for go-embedded-ruby, but is a standalone, reusable module — a sibling of go-ruby-json and go-ruby-set.

Does not reimplement the tracing model. Every type is a Ruby-faithful facade over go.opentelemetry.io/otel and go.opentelemetry.io/otel/sdk: the Go SDK does the recording, sampling, batching and W3C traceparent/tracestate/baggage serialization; this module re-expresses it with opentelemetry-ruby method names and semantics.

In-process, zero network. The span exporter is an injectable seam. An InMemorySpanExporter is provided for tests and embedding; OTLP/stdout exporters are a documented follow-up.

Install

go get github.com/go-ruby-opentelemetry/opentelemetry

Usage

package main

import (
	"errors"
	"fmt"

	rbcontext "github.com/go-ruby-opentelemetry/opentelemetry/context"
	"github.com/go-ruby-opentelemetry/opentelemetry/propagation"
	sdktrace "github.com/go-ruby-opentelemetry/opentelemetry/sdk/trace"
	"github.com/go-ruby-opentelemetry/opentelemetry/trace"
)

func main() {
	// SDK::Trace::TracerProvider with a simple processor + in-memory exporter.
	exporter := sdktrace.NewInMemorySpanExporter()
	tp := sdktrace.NewTracerProvider(
		sdktrace.WithSpanProcessor(sdktrace.NewSimpleSpanProcessor(exporter)),
		sdktrace.WithSampler(sdktrace.AlwaysOn()),
	)
	tracer := tp.Tracer("my.service", "1.0.0")

	// Tracer#in_span(name, attributes:, kind:) { |span| ... }
	tracer.InSpan(rbcontext.Background(), "handle-request",
		func(span trace.Span, ctx *rbcontext.Context) {
			span.SetAttribute("http.method", "GET")
			span.AddEvent("cache.miss", map[string]any{"key": "u:42"})
			span.RecordException(errors.New("upstream slow"), nil)
			span.SetStatus(trace.StatusError("degraded"))

			// W3C TraceContext inject → extract round-trips the ids + flags.
			carrier := propagation.NewCarrier()
			propagation.TraceContext().Inject(ctx, carrier)
			fmt.Println(carrier["traceparent"])
		},
		trace.WithKind(trace.Server),
	)

	finished := exporter.FinishedSpans()
	fmt.Println(finished[0].Name(), finished[0].Status().Code) // handle-request Error
}

Ruby surface

Ruby Go
OpenTelemetry.tracer_provider / .tracer(name, version) opentelemetry.TracerProvider() / .Tracer(name, version)
OpenTelemetry.propagation opentelemetry.Propagation() / SetPropagation
Trace::Tracer#in_span / #start_span / #start_root_span trace.Tracer.InSpan / .StartSpan / .StartRootSpan
Trace::Span#set_attribute / add_event / record_exception / status= / finish / context / name= trace.Span.SetAttribute / AddEvent / RecordException / SetStatus / Finish / Context / SetName
Trace::SpanContext (trace_id/span_id/trace_flags/remote?) trace.SpanContext
Trace::SpanKind (INTERNAL/SERVER/CLIENT/PRODUCER/CONSUMER) trace.Internal/Server/Client/Producer/Consumer
Trace::Status (unset/ok/error) trace.StatusUnset/StatusOK/StatusError
Context (create_key/value/current/attach/detach/with_current) context.Context package
Context::Propagation::TraceContext / Baggage inject/extract propagation.TraceContext() / .Baggage()
Baggage (set_value/value/values/remove_value/clear) baggage.Set/Value/Values/Remove/Clear
SDK::Trace::TracerProvider + SpanProcessor (Simple/Batch) + SpanExporter sdktrace.NewTracerProvider / NewSimpleSpanProcessor / NewBatchSpanProcessor / SpanExporter
SDK::Trace::Export::InMemorySpanExporter sdktrace.InMemorySpanExporter
SDK::Trace::Samplers (ALWAYS_ON/OFF/parent_based/ratio) sdktrace.AlwaysOn/AlwaysOff/ParentBased/TraceIDRatioBased

What it consumes

Correctness

Verified against the backing Go SDK semantics with the in-memory exporter, fully in-process (no network):

  • spans record the correct name, attributes, events, status, kind and parent/child linkage (child shares the parent's trace_id and carries the parent's span_id);
  • W3C TraceContext inject → extract round-trips trace_id, span_id and trace_flags, and marks the extracted context remote?;
  • baggage round-trips through the W3C baggage header;
  • sampling decisions are honored (AlwaysOn, AlwaysOff, ParentBased, ratio-based);
  • both the simple and batch processors flush to the exporter;
  • in_span records a panic as an exception event, sets the status to error, and re-raises.

Tests & coverage

COVERPKG=$(go list ./... | paste -sd, -)
go test -race -coverpkg="$COVERPKG" -coverprofile=cover.out ./...
go tool cover -func=cover.out | tail -1   # 100.0%

CGO-free, gofmt + go vet clean, race-clean, and green across the six 64-bit Go targets (amd64, arm64, riscv64, loong64, ppc64le, s390x big-endian) and three OSes (Linux, macOS, Windows). Span timestamps are the SDK's monotonic UTC clocks — no timezone assumptions.

Follow-ups

  • Metrics (OpenTelemetry::Metrics — meters/instruments) — a documented follow-up; this module does the trace API + SDK faithfully.
  • OTLP / stdout exporters wired onto the existing SpanExporter seam.
  • rbgo binding into go-embedded-ruby.
  • Full org conformance (Hugo landing, MkDocs/mike docs, brand assets).

License

BSD-3-Clause — see LICENSE. Copyright the go-ruby-opentelemetry/opentelemetry authors.

WebAssembly

Being pure Go (CGO=0), this library also compiles to WebAssembly — both GOOS=js GOARCH=wasm (browser / Node.js) and GOOS=wasip1 GOARCH=wasm (WASI). CI builds both targets on every push, alongside the six 64-bit native/qemu arches.

GOOS=js     GOARCH=wasm go build ./...   # browser / Node
GOOS=wasip1 GOARCH=wasm go build ./...   # WASI (wasmtime, wasmer, wasmedge, …)

Documentation

Overview

Package opentelemetry is the root of a pure-Go (CGO=0), MRI-faithful reimplementation of Ruby's opentelemetry-api and opentelemetry-sdk gems, focused on distributed tracing.

It mirrors the top-level OpenTelemetry Ruby module — the global tracer provider and text-map propagator — while the sub-packages mirror the rest of the surface:

  • trace: OpenTelemetry::Trace — tracers, spans, span context, kind, status.
  • context: OpenTelemetry::Context — the immutable execution context.
  • baggage: OpenTelemetry::Baggage — propagated key/value pairs.
  • propagation: OpenTelemetry::Context::Propagation — W3C TraceContext and Baggage inject/extract.
  • sdk/trace: OpenTelemetry::SDK::Trace — the configurable TracerProvider, span processors (simple + batch), samplers and the in-memory exporter seam.

The tracing model itself is not reimplemented: every type is a Ruby-faithful facade over the OpenTelemetry Go SDK (go.opentelemetry.io/otel and go.opentelemetry.io/otel/sdk), so the real, well-tested Go implementation does the recording, sampling, batching and W3C serialization while callers use the method names and semantics of opentelemetry-ruby. Everything runs in-process with no network: the exporter is an injectable seam and an InMemorySpanExporter is provided for tests and embedding.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Propagation

func Propagation() propagation.TextMapPropagator

Propagation mirrors OpenTelemetry.propagation — the registered global text-map propagator.

func SetPropagation

func SetPropagation(p propagation.TextMapPropagator)

SetPropagation mirrors OpenTelemetry.propagation= — it installs the global propagator.

func SetTracerProvider

func SetTracerProvider(tp trace.TracerProvider)

SetTracerProvider mirrors OpenTelemetry.tracer_provider= — it installs the global provider (typically an SDK TracerProvider).

func Tracer

func Tracer(name, version string) *trace.Tracer

Tracer mirrors OpenTelemetry.tracer_provider.tracer(name, version) — a convenience that fetches a tracer from the global provider.

func TracerProvider

func TracerProvider() trace.TracerProvider

TracerProvider mirrors OpenTelemetry.tracer_provider — the registered global provider, defaulting to a no-op provider until the SDK is installed.

Types

This section is empty.

Directories

Path Synopsis
Package baggage mirrors Ruby's OpenTelemetry::Baggage — the propagated set of key/value pairs that ride alongside a trace.
Package baggage mirrors Ruby's OpenTelemetry::Baggage — the propagated set of key/value pairs that ride alongside a trace.
Package context mirrors Ruby's OpenTelemetry::Context — the immutable, key/value execution context that carries the current span, baggage and any user values across API boundaries.
Package context mirrors Ruby's OpenTelemetry::Context — the immutable, key/value execution context that carries the current span, baggage and any user values across API boundaries.
Package propagation mirrors Ruby's OpenTelemetry::Context::Propagation — the injection and extraction of cross-process context over carriers such as HTTP headers.
Package propagation mirrors Ruby's OpenTelemetry::Context::Propagation — the injection and extraction of cross-process context over carriers such as HTTP headers.
sdk
trace
Package sdktrace mirrors Ruby's OpenTelemetry::SDK::Trace — the configurable tracing implementation from the opentelemetry-sdk gem.
Package sdktrace mirrors Ruby's OpenTelemetry::SDK::Trace — the configurable tracing implementation from the opentelemetry-sdk gem.
Package trace mirrors Ruby's OpenTelemetry::Trace API — the tracing surface (tracers, spans, span context, kind, status) exposed by the opentelemetry-api gem.
Package trace mirrors Ruby's OpenTelemetry::Trace API — the tracing surface (tracers, spans, span context, kind, status) exposed by the opentelemetry-api gem.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL