sync
This commit is contained in:
32
pkg/tracer/event.go
Normal file
32
pkg/tracer/event.go
Normal file
@ -0,0 +1,32 @@
|
||||
package tracer
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"maal/tracer/pkg/level"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func LongMessage(message string) attribute.KeyValue {
|
||||
return attribute.KeyValue{Key: "long_message_", Value: attribute.StringValue(message)}
|
||||
}
|
||||
|
||||
func Level(level level.SyslogLevel) attribute.KeyValue {
|
||||
return attribute.KeyValue{Key: "level_", Value: attribute.Int64Value(int64(level))}
|
||||
}
|
||||
|
||||
func JsonAttr(key string, jsonEl map[string]interface{}) attribute.KeyValue {
|
||||
|
||||
jsonStr, _ := json.Marshal(jsonEl)
|
||||
return attribute.KeyValue{Key: attribute.Key(key), Value: attribute.StringValue(string(jsonStr))}
|
||||
|
||||
}
|
||||
|
||||
func RecordError(span trace.Span, err error) error {
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
|
||||
return nil
|
||||
}
|
@ -2,9 +2,11 @@ package tracer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
gelfexporter "maal/tracer/pkg/gelf_exporter"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
fiberOpentelemetry "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel"
|
||||
@ -111,13 +113,15 @@ func NewTracer(config Config) func(*fiber.Ctx) error {
|
||||
|
||||
otlpExporter := otlptracehttp.NewUnstarted(otlptracehttp.WithEndpointURL(config.JaegerUrl))
|
||||
|
||||
gelfExporter, err := gelfexporter.New(gelfexporter.WithGelfUrl(config.GelfUrl))
|
||||
gelfExporter, err := gelfexporter.New(
|
||||
gelfexporter.WithGelfUrl(config.GelfUrl),
|
||||
gelfexporter.WithAppName("salego"),
|
||||
)
|
||||
if err != nil {
|
||||
l.Fatal(err)
|
||||
}
|
||||
tracerProviders = append(tracerProviders, trc.WithBatcher(otlpExporter))
|
||||
tracerProviders = append(tracerProviders, trc.WithBatcher(gelfExporter))
|
||||
|
||||
tracerProviders = append(tracerProviders, trc.WithResource(newResource(config)))
|
||||
|
||||
TP = *trc.NewTracerProvider(tracerProviders...)
|
||||
@ -154,7 +158,45 @@ func Handler(fc *fiber.Ctx) (context.Context, trace.Span) {
|
||||
simpleCtx, span := fiberOpentelemetry.Tracer.Start(fc.UserContext(), fc.OriginalURL())
|
||||
fc.SetUserContext(simpleCtx)
|
||||
|
||||
span.SetAttributes(attribute.String("service.layer", "handler"))
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
span.SetAttributes(
|
||||
attribute.String("service.layer", "handler"),
|
||||
attribute.String("file", file),
|
||||
attribute.String("line", fmt.Sprintf("%d", line)),
|
||||
)
|
||||
|
||||
return simpleCtx, span
|
||||
}
|
||||
|
||||
func Service(c context.Context, serviceName string, spanName string) (context.Context, trace.Span) {
|
||||
simpleCtx, span := fiberOpentelemetry.Tracer.Start(c, spanName)
|
||||
var attribs []attribute.KeyValue
|
||||
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
attribs = append(
|
||||
attribs,
|
||||
attribute.String("service.name", serviceName),
|
||||
attribute.String("service.layer", "service"),
|
||||
attribute.String("file", file),
|
||||
attribute.String("line", fmt.Sprintf("%d", line)),
|
||||
)
|
||||
|
||||
span.SetAttributes(attribs...)
|
||||
|
||||
return simpleCtx, span
|
||||
}
|
||||
|
||||
func Repository(c context.Context, repositoryName string, spanName string) (context.Context, trace.Span) {
|
||||
ctx2, span := fiberOpentelemetry.Tracer.Start(c, spanName)
|
||||
var attribs []attribute.KeyValue
|
||||
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
attribs = append(attribs,
|
||||
attribute.String("service.repository", repositoryName),
|
||||
attribute.String("file", file),
|
||||
attribute.String("line", fmt.Sprintf("%d", line)),
|
||||
)
|
||||
span.SetAttributes(attribs...)
|
||||
|
||||
return ctx2, span
|
||||
}
|
||||
|
Reference in New Issue
Block a user