如何组合 golang 中不同事件的输出,以实现“google.golang.org/grpc/stats”包中的 func HandleRPC

问题描述 投票:0回答:1
// HandleRPC implements per-RPC tracing and stats instrumentation.
func (c *statsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
 switch rs := rs.(type) {

  case *stats.InHeader:

     if rs.Client {
        fmt.Printf(rs.Header)
    }

  case *stats.End:
    ign := false
    c.lock.RLock()
    ign = c.ignore
    c.lock.RUnlock()

    if !ign {
        duration := rs.EndTime.Sub(rs.BeginTime)

        var st string
        s, ok := status.FromError(rs.Error)
        if ok {
            st = s.Code().String()
        }

        c.results <- &callResult{rs.Error, st, duration, rs.EndTime}

        if c.hasLog {
            c.log.Debugw("Received RPC Stats",
                "statsID", c.id, "code", st, "error", rs.Error,
                "duration", duration, "stats", rs)
        }
    }
}

}

我有

response metadata
出现在事件
*stats.InHeader
上,其余的
grpc stats
出现在事件
*stats.End
上,我想在 grpc 末尾将
metadata
grpc stats
组合成一个结构对象
callResult
称呼。我很不知道如何实现这一点,我对 go 和 grpc 真的很陌生。有人可以帮我解决这个问题吗,谢谢!

go grpc grpc-go
1个回答
0
投票
type MutableObject struct {
    InMetadata metadata.MD // Example mutable field
}

// TagRPC implements per-RPC context management.
func (c *statsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {

    ctx = context.WithValue(ctx, "InHeader", &MutableObject{})

    return ctx
}

// HandleRPC implements per-RPC tracing and stats instrumentation.
func (c *statsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
    switch rs := rs.(type) {

    case *stats.InHeader:
        var headerValue metadata.MD
        // You can access the `InHeader` field from the `s` object to get the received headers.
        if rs.Client {
            ign := false
            c.lock.RLock()
            ign = c.ignore
            c.lock.RUnlock()
            if !ign {
                headerValue = rs.Header

                if header, ok := ctx.Value("InHeader").(*MutableObject); ok {
                    header.InMetadata = headerValue
                }
            }
        }

    case *stats.End:

        ign := false
        c.lock.RLock()
        ign = c.ignore
        c.lock.RUnlock()

        if !ign {
            duration := rs.EndTime.Sub(rs.BeginTime)

            var st string
            s, ok := status.FromError(rs.Error)
            if ok {
                st = s.Code().String()
            }

            // Retrieve the header value from the context

            // fmt.Printf("------------->>>>>>>>>%T",ctx.Value("InHeader"))
            // fmt.Println("------------->>>>>>>>>",ctx.Value("InHeader"))
            var ts time.Time
            if header, ok := ctx.Value("InHeader").(*MutableObject); ok {
                databroker_timestamp, err := strconv.ParseInt(header.InMetadata["ts"][0], 10, 64)
                if err == nil {
                    ts = time.Unix(int64(math.Abs(float64(databroker_timestamp)/1000000000)), databroker_timestamp%1000000000)
                }

            }

            c.results <- &callResult{rs.Error, st, duration, rs.EndTime, ts, 10, 10}

            if c.hasLog {
                c.log.Debugw("Received RPC Stats",
                    "statsID", c.id, "code", st, "error", rs.Error,
                    "duration", duration, "stats", rs)
            }
        }
    }

}
  1. 将可变对象添加到上下文中
    TagRPC
  2. 修改
    *stats.InHeader
    事件上的可变对象
  3. 最终访问
    *stats.End
  4. 上的对象
© www.soinside.com 2019 - 2024. All rights reserved.