observer/example/main.go
Natalia Goc ab5b70704d standardize commonly used attributes
Some commonly used at maal attributes have been encoded as consts with
convinience wrappers similar to those of semconv package from otel sdk.
Additionally some utils that can generate mutliple attributes were added.
2024-05-16 13:45:13 +02:00

98 lines
1.8 KiB
Go

package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"time"
"git.ma-al.com/gora_filip/observer/pkg/level"
"git.ma-al.com/gora_filip/observer/pkg/tracer"
"github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel/trace"
)
type AttributesX struct {
}
func main() {
main := fiber.New(fiber.Config{
StreamRequestBody: true,
})
main.Use(tracer.NewTracer(tracer.Config{
AppName: "test",
JaegerUrl: "http://localhost:4318/v1/traces",
GelfUrl: "192.168.220.30:12201",
Version: "1",
}))
defer tracer.ShutdownTracer()
main.Get("/", func(c *fiber.Ctx) error {
ctx, span := tracer.Handler(c)
defer span.End()
span.AddEvent(
"smthing is happening",
trace.WithAttributes(
tracer.LongMessage("smthing is happening - long"),
tracer.JsonAttr("smth", map[string]interface{}{
"xd": 1,
}),
tracer.Level(level.ALERT),
),
)
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
}