observer/example/main.go

106 lines
2.0 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"time"
"git.ma-al.com/gora_filip/observer/pkg/tracer"
"git.ma-al.com/gora_filip/pkg/attr/layer_attr"
"git.ma-al.com/gora_filip/pkg/exporters"
tracing "git.ma-al.com/gora_filip/pkg/fiber_tracing"
"git.ma-al.com/gora_filip/pkg/level"
"github.com/gofiber/fiber/v2"
)
func main() {
main := fiber.New(fiber.Config{
StreamRequestBody: true,
})
exps := make([]exporters.TraceExporter, 0)
exps = append(exps, exporters.DevConsoleExporter())
gelfExp, err := exporters.GelfExporter()
if err == nil {
exps = append(exps, gelfExp)
}
jaegerExp, err := exporters.OtlpHTTPExporter()
if err == nil {
exps = append(exps, jaegerExp)
}
main.Use(tracing.NewMiddleware(tracing.Config{
AppName: "example",
Version: "0.0.0",
ServiceProvider: "maal",
Exporters: exps,
}))
defer tracing.ShutdownTracer()
main.Get("/", func(c *fiber.Ctx) error {
ctx, span := tracing.FStart(c, layer_attr.Handler{
Level: level.DEBUG,
}.AsOpts())
defer span.End()
span.AddEvent(
"smthing is happening",
layer_attr.Handler{
Level: level.INFO,
}.AsOpts(),
)
err := Serv(ctx)
if err != nil {
return tracer.RecordError(span, err)
}
return c.SendString("xd")
})
// handle interrupts (shutdown)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
oscall := <-c
log.Printf("system call: %+v", oscall)
main.Shutdown()
}()
if err := main.Listen(fmt.Sprintf(":%d", 3344)); err != nil {
log.Panic(err)
}
fmt.Println("Shutting down...")
}
func Serv(ctx context.Context) *fiber.Error {
ctx, span := tracer.Service(ctx, "service", "service span")
defer span.End()
for range []int{1, 2, 3} {
time.Sleep(time.Millisecond * 100)
}
err := Repo(ctx)
if err != nil {
return fiber.NewError(500, "xd")
}
return fiber.NewError(500, "x")
}
func Repo(ctx context.Context) error {
ctx, span := tracer.Repository(ctx, "repo", "repo span")
defer span.End()
for range []int{1, 2, 3} {
time.Sleep(time.Millisecond * 100)
}
return nil
}