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"
|
|
|
|
|
2024-05-20 06:20:13 +00:00
|
|
|
"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"
|
2024-05-20 10:18:03 +00:00
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/exporters/console_exporter"
|
2024-05-20 06:20:13 +00:00
|
|
|
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"
|
2024-05-17 16:21:09 +00:00
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
2024-05-20 09:45:27 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-04-26 13:09:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-05-17 16:21:09 +00:00
|
|
|
main := fiber.New()
|
2024-04-26 13:09:54 +00:00
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
exps := make([]exporters.TraceExporter, 0)
|
2024-06-04 11:02:29 +00:00
|
|
|
|
|
|
|
envFilter := "OBSERVER_CONSOLE"
|
|
|
|
|
2024-09-10 13:52:03 +00:00
|
|
|
singlelineFmt := console_exporter.NewSimpleSinglelineFormatter()
|
|
|
|
|
2024-06-04 11:02:29 +00:00
|
|
|
exps = append(exps, exporters.DevConsoleExporter(console_exporter.ProcessorOptions{
|
|
|
|
FilterFromEnvVar: &envFilter,
|
2024-09-10 13:52:03 +00:00
|
|
|
TraceFormatter: &singlelineFmt,
|
2024-06-04 11:02:29 +00:00
|
|
|
}))
|
|
|
|
|
2024-05-17 08:37:05 +00:00
|
|
|
gelfExp, err := exporters.GelfExporter()
|
|
|
|
if err == nil {
|
|
|
|
exps = append(exps, gelfExp)
|
|
|
|
}
|
2024-05-17 16:21:09 +00:00
|
|
|
jaegerExp, err := exporters.OtlpHTTPExporter(otlptracehttp.WithEndpointURL("http://localhost:4318/v1/traces"))
|
2024-05-17 13:36:35 +00:00
|
|
|
if err == nil {
|
|
|
|
exps = append(exps, jaegerExp)
|
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
|
2024-05-17 13:36:35 +00:00
|
|
|
main.Use(tracing.NewMiddleware(tracing.Config{
|
2024-05-16 16:19:36 +00:00
|
|
|
AppName: "example",
|
|
|
|
Version: "0.0.0",
|
|
|
|
ServiceProvider: "maal",
|
2024-05-17 08:37:05 +00:00
|
|
|
Exporters: exps,
|
2024-04-26 13:09:54 +00:00
|
|
|
}))
|
2024-05-17 13:36:35 +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")
|
2024-05-20 09:45:27 +00:00
|
|
|
|
|
|
|
span = trace.SpanFromContext(c.UserContext())
|
|
|
|
span.AddEvent("span also available from c.UserContext()")
|
2024-05-20 09:04:36 +00:00
|
|
|
return c.Next()
|
|
|
|
})
|
|
|
|
|
2024-05-17 16:21:09 +00:00
|
|
|
main.Get("/", Handler)
|
2024-05-20 17:43:39 +00:00
|
|
|
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
|
|
|
|
2024-05-17 16:21:09 +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 {
|
2024-05-17 16:21:09 +00:00
|
|
|
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 {
|
2024-05-17 16:21:09 +00:00
|
|
|
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
|
|
|
|
}
|