48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"connectrpc.com/connect"
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
func LogInterceptor() connect.UnaryInterceptorFunc {
|
|
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
|
|
return func(ctx context.Context, ar connect.AnyRequest) (resp connect.AnyResponse, err error) {
|
|
span := trace.SpanFromContext(ctx)
|
|
defer func() { telemetry.EndWithStatus(span, err) }()
|
|
|
|
start := time.Now()
|
|
resp, err = next(ctx, ar)
|
|
dur := time.Since(start)
|
|
if err != nil {
|
|
slog.ErrorContext(ctx, "RPC Error",
|
|
"procedure", ar.Spec().Procedure,
|
|
"method", ar.HTTPMethod(),
|
|
"duration", fmt.Sprintf("%.3fs", dur.Seconds()),
|
|
"error", errs.DrillToError(err),
|
|
"span.id", span.SpanContext().SpanID().String(),
|
|
"trace.id", span.SpanContext().TraceID().String(),
|
|
)
|
|
} else {
|
|
slog.InfoContext(ctx, "RPC Call",
|
|
"procedure", ar.Spec().Procedure,
|
|
"method", ar.HTTPMethod(),
|
|
"duration", fmt.Sprintf("%.3fs", dur.Seconds()),
|
|
"span.id", span.SpanContext().SpanID().String(),
|
|
"trace.id", span.SpanContext().TraceID().String(),
|
|
)
|
|
}
|
|
|
|
return resp, err
|
|
}
|
|
}
|
|
return connect.UnaryInterceptorFunc(interceptor)
|
|
}
|