o11yfiber

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

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

Go to latest
Published: Feb 11, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

README

O11Y_fiber

Minimal API with fiber

Start minimal API

Use example

package main

import (
	"context"
	"errors"
	"log"

	o11yfiber "github.com/IsaacDSC/O11Y_fiber"
	"github.com/gofiber/fiber/v2"
	"go.opentelemetry.io/otel/attribute"
	oteltrace "go.opentelemetry.io/otel/trace"
)

func main() {
	tp := o11yfiber.StartTracing(o11yfiber.TracingConfig{
		EndpointCollector: "http://localhost:14268/api/traces",
		ServiceNameKey:    "minimal-api",
	})

	log.Fatal(o11yfiber.StartServerHttp(o11yfiber.SettingsHttp{
		TracerProvider: tp,
		Handlers: []o11yfiber.Handler{
			{HandlerFunc: handleUser, Path: "/users/:id", Method: o11yfiber.GET},
			{HandlerFunc: handleError, Path: "/error", Method: o11yfiber.GET},
		},
		Middleware: []func(c *fiber.Ctx) error{
			o11yfiber.MiddlewareIO,
		},
		ServerPort: 3000,
	}))

}

func handleError(ctx *fiber.Ctx) error {
	return errors.New("abc")
}

func handleUser(c *fiber.Ctx) error {
	id := c.Params("id")
	name := getUser(c.UserContext(), id)
	return c.JSON(fiber.Map{"id": id, name: name})
}
func getUser(ctx context.Context, id string) string {
	_, span := o11yfiber.Span().Start(ctx, "getUser", oteltrace.WithAttributes(attribute.String("id", id)))
	defer span.End()
	if id == "123" {
		return "otelfiber tester"
	}
	return "unknown"
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MiddlewareIO

func MiddlewareIO(ignorePaths ...string) func(c *fiber.Ctx) error

func NewLogger

func NewLogger() *slog.Logger

func Span

func Span() trace.Tracer

func StartServerHttp

func StartServerHttp(config SettingsHttp) error

func StartTracing

func StartTracing(config TracingConfig) *sdktrace.TracerProvider

Types

type Errors

type Errors = string
const (
	NOT_FOUND_SERVICE_NAME_METRICS Errors = "REQUIRED SERVICE NAME METRICS"
	SHUTTING_DOWN_TRACER_PROVIDER  Errors = "SHUTTING DOWN TRACER PROVIDER"
)

type Handler

type Handler struct {
	HandlerFunc HandlerFn
	Path        string
	Method      string
}

type HandlerFn

type HandlerFn = func(*fiber.Ctx) error

type Methods

type Methods = string
const (
	POST   Methods = "POST"
	GET    Methods = "GET"
	PATCH  Methods = "PATCH"
	PUT    Methods = "PUT"
	DELETE Methods = "DELETE"
)

type SettingsHttp

type SettingsHttp struct {
	TracerProvider     *sdktrace.TracerProvider
	Handlers           []Handler
	Middleware         []func(c *fiber.Ctx) error
	ServerPort         int
	ServiceNameMetrics string
}

type TracingConfig

type TracingConfig struct {
	EndpointCollector string
	ServiceNameKey    string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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