130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
func main() {
|
|
main := fiber.New()
|
|
|
|
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,
|
|
}))
|
|
defer tracing.ShutdownTracer()
|
|
|
|
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()")
|
|
return c.Next()
|
|
})
|
|
|
|
main.Get("/", Handler)
|
|
main.Get("/just/some/more/complex/path/:with/params", Handler)
|
|
|
|
// 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 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)
|
|
}
|
|
|
|
func Serv(ctx context.Context) *fiber.Error {
|
|
ctx, span := tracing.Start(ctx, "service span", layer_attr.Service{
|
|
Level: level.INFO,
|
|
Name: "some service",
|
|
}.AsOpts())
|
|
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())
|
|
defer span.End()
|
|
|
|
for range []int{1, 2, 3} {
|
|
time.Sleep(time.Millisecond * 100)
|
|
}
|
|
|
|
return nil
|
|
}
|