Skip to content

Commit

Permalink
fix: resolve the issue of metrics and logging code default to zero (g…
Browse files Browse the repository at this point in the history
…o-kratos#3447)

* fix: resolve the issue of metrics and logging code default to zero

* fix lint
  • Loading branch information
shenqidebaozi authored Oct 23, 2024
1 parent 8b8dc4b commit 4e9dac0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 11 additions & 0 deletions middleware/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"fmt"
"time"

"google.golang.org/grpc/codes"

"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http/status"
)

// Redacter defines how to log an object
Expand All @@ -26,6 +29,10 @@ func Server(logger log.Logger) middleware.Middleware {
kind string
operation string
)

// default code
code = int32(status.FromGRPCCode(codes.OK))

startTime := time.Now()
if info, ok := transport.FromServerContext(ctx); ok {
kind = info.Kind().String()
Expand Down Expand Up @@ -62,6 +69,10 @@ func Client(logger log.Logger) middleware.Middleware {
kind string
operation string
)

// default code
code = int32(status.FromGRPCCode(codes.OK))

startTime := time.Now()
if info, ok := transport.FromClientContext(ctx); ok {
kind = info.Kind().String()
Expand Down
18 changes: 14 additions & 4 deletions middleware/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"time"

"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
"google.golang.org/grpc/codes"

"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http/status"
)

const (
Expand Down Expand Up @@ -115,6 +117,10 @@ func Server(opts ...Option) middleware.Middleware {
kind string
operation string
)

// default code
code = status.FromGRPCCode(codes.OK)

startTime := time.Now()
if info, ok := transport.FromServerContext(ctx); ok {
kind = info.Kind().String()
Expand Down Expand Up @@ -164,6 +170,10 @@ func Client(opts ...Option) middleware.Middleware {
kind string
operation string
)

// default code
code = status.FromGRPCCode(codes.OK)

startTime := time.Now()
if info, ok := transport.FromClientContext(ctx); ok {
kind = info.Kind().String()
Expand Down

0 comments on commit 4e9dac0

Please sign in to comment.