otelattr

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 6 Imported by: 0

README

otelattr

Usage

package main

import (
	"encoding/json"
	"os"

	"github.com/mashiike/otelattr"
)

type HTTPContext struct {
		Status int    `otelattr:"http.status_code"`
		Method string `otelattr:"http.method"`
		Path   string `otelattr:"http.path"`
}

func main() {
	httpCtx := HTTPContext{
		Status: 200,
		Method: "GET",
		Path:   "/",
	}
	attrs, err := otelattr.MarshalOtelAttributes(httpCtx)
	if err != nil {
		panic(err)
	}
	enc := json.NewEncoder(os.Stdout)
	for _, attr := range attrs {
		if err := enc.Encode(attr); err != nil {
			panic(err)
		}
	}
	// Output:
	//{"Key":"http.status_code","Value":{"Type":"INT64","Value":200}}
	//{"Key":"http.method","Value":{"Type":"STRING","Value":"GET"}}
	//{"Key":"http.path","Value":{"Type":"STRING","Value":"/"}}
}

LICENSE: MIT

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"os"

	"github.com/mashiike/otelattr"
)

func main() {
	type HTTPContext struct {
		Status int    `otelattr:"http.status_code"`
		Method string `otelattr:"http.method"`
		Path   string `otelattr:"http.path"`
	}
	httpCtx := HTTPContext{
		Status: 200,
		Method: "GET",
		Path:   "/",
	}
	attrs, err := otelattr.MarshalOtelAttributes(httpCtx)
	if err != nil {
		panic(err)
	}
	enc := json.NewEncoder(os.Stdout)
	for _, attr := range attrs {
		if err := enc.Encode(attr); err != nil {
			panic(err)
		}
	}
}
Output:

{"Key":"http.status_code","Value":{"Type":"INT64","Value":200}}
{"Key":"http.method","Value":{"Type":"STRING","Value":"GET"}}
{"Key":"http.path","Value":{"Type":"STRING","Value":"/"}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalOtelAttributes

func MarshalOtelAttributes(v interface{}) ([]attribute.KeyValue, error)

Types

type Marshaler

type Marshaler interface {
	MarshalOtelAttributes() ([]attribute.KeyValue, error)
}

Jump to

Keyboard shortcuts

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