observer/example/main.go

130 lines
3.0 KiB
Go
Raw Normal View History

2024-04-26 13:09:54 +00:00
package main
import (
2024-04-30 10:41:05 +00:00
"context"
2024-04-26 13:09:54 +00:00
"fmt"
"log"
"os"
"os/signal"
"time"
"git.ma-al.com/maal-libraries/observer/pkg/attr/layer_attr"
"git.ma-al.com/maal-libraries/observer/pkg/event"
"git.ma-al.com/maal-libraries/observer/pkg/exporters"
"git.ma-al.com/maal-libraries/observer/pkg/exporters/console_exporter"
tracing "git.ma-al.com/maal-libraries/observer/pkg/fiber_tracing"
"git.ma-al.com/maal-libraries/observer/pkg/level"
2024-04-26 13:09:54 +00:00
"github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/trace"
2024-04-26 13:09:54 +00:00
)
func main() {
main := fiber.New()
2024-04-26 13:09:54 +00:00
exps := make([]exporters.TraceExporter, 0)
envFilter := "OBSERVER_CONSOLE"
singlelineFmt := console_exporter.NewSimpleSinglelineFormatter()
exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{
FilterFromEnvVar: &envFilter,
TraceFormatter: &singlelineFmt,
}))
gelfExp, err := exporters.GelfExporter()
if err == nil {
exps = append(exps, gelfExp)
}
jaegerExp, err := exporters.OtlpHTTPExporter(otlptracehttp.WithEndpointURL("http://localhost:4318/v1/traces"))
if err == nil {
exps = append(exps, jaegerExp)
}
main.Use(tracing.NewMiddleware(tracing.Config{
AppName: "example",
Version: "0.0.0",
ServiceProvider: "maal",
Exporters: exps,
2024-04-26 13:09:54 +00:00
}))
defer tracing.ShutdownTracer()
2024-04-26 13:09:54 +00:00
2024-05-20 09:04:36 +00:00
main.Use(func(c *fiber.Ctx) error {
span := tracing.SpanFromContext(c)
span.AddEvent("pushed into a span an event from middleware")
span = trace.SpanFromContext(c.UserContext())
span.AddEvent("span also available from c.UserContext()")
2024-05-20 09:04:36 +00:00
return c.Next()
})
main.Get("/", Handler)
main.Get("/just/some/more/complex/path/:with/params", Handler)
2024-04-26 13:09:54 +00:00
// 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...")
}
2024-04-30 10:41:05 +00:00
func Handler(c *fiber.Ctx) error {
ctx, span := tracing.FStart(c, layer_attr.Handler{
Level: level.DEBUG,
}.AsOpts())
defer span.End()
event.NewInSpan(event.Event{
Level: level.WARN,
ShortMessage: "a warning event",
}, span)
err := Serv(ctx)
if err != nil {
return event.NewErrInSpan(event.Error{Err: err, Level: level.ERR}, span)
}
return c.SendStatus(fiber.StatusOK)
}
2024-04-30 10:41:05 +00:00
func Serv(ctx context.Context) *fiber.Error {
ctx, span := tracing.Start(ctx, "service span", layer_attr.Service{
Level: level.INFO,
Name: "some service",
}.AsOpts())
2024-04-30 10:41:05 +00:00
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 := tracing.Start(ctx, "repo span", layer_attr.Repo{
Level: level.DEBUG,
Name: "some repo",
}.AsOpts())
2024-04-30 10:41:05 +00:00
defer span.End()
for range []int{1, 2, 3} {
time.Sleep(time.Millisecond * 100)
}
return nil
}