oteltag

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

otel-tag

Simple package that extracts OpenTelemetry span attributes and baggage members from a struct based on tags.

[!WARNING] Please bear in mind that this package does not intend to cater to all use cases and please everyone. The code could also do with some refactoring here and there, but it's doing the job.

Shortcomings

  • Does not support basic type pointers (only struct pointers).

Usage

package main

import (
	"context"

	"go.opentelemetry.io/otel/baggage"
	"go.opentelemetry.io/otel/trace"

	oteltag "github.com/remychantenay/otel-tag"
)

type User struct {
	ID        string `otel:"app.user.id"`
	Username  string `otel:"app.user.username"`
	IsPremium bool   `otel:"app.user.premium"`

	UserDetails UserDetails
}

type UserDetails struct {
	Website string `otel:"app.user.website,omitempty"`
	Bio     string // Not tagged, will be ignored.
}

func main() {
	// Span attributes.
	_, span := tracer.Start(context.Background(), "someOperation",
		trace.WithAttributes(oteltag.SpanAttributes(m)...),
	)
	defer span.End()

	// Baggage Members.
	members := oteltag.BaggageMembers(m)
	bag, _ := baggage.New(members...)
	ctx := baggage.ContextWithBaggage(context.Background(), bag)
}

License

Apache License Version 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BaggageMembers

func BaggageMembers(res any) []baggage.Member

BaggageMembers takes in a struct and spits out OpenTelemetry baggage members based on the struct tags.

func SpanAttributes

func SpanAttributes(res any) []attribute.KeyValue

SpanAttributes takes in a struct and spits out OpenTelemetry span attributes (attribute.KeyValue) based on the struct tags.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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