从GRPC获取请求的详细信息

huangapple go评论66阅读模式
英文:

Get request details from GRPC

问题

对于普通的HTTP请求,我可以获取协议、远程IP、主机、方法、请求URL、引用页、用户代理等详细信息。我知道可以通过以下方式提取与请求相关的元数据:

var extracted string

meta, ok := metadata.FromIncomingContext(ctx)
if ok {
    if value, ok := meta[header]; ok && len(value) > 0 {
		 extracted = value[0]
	}
}

此外,我了解HTTP头将被插入到元数据中,但这仅适用于通过HTTP网关进行的请求,因此从GRPC客户端发出的请求可能不包含此信息。

但我不确定header应该使用哪些值,或者这些信息是否在元数据中可用,或者我是否应该在其他地方进行检查。如何从GRPC请求中获取与请求相关的信息呢?

英文:

For normal HTTP requests, I can get details like the protocol, remote IP, host, method, request URL, referrer, user agent, etc. I know I can extract metadata associated with the request by doing this:

var extracted string

meta, ok := metadata.FromIncomingContext(ctx)
if ok {
    if value, ok := meta[header]; ok && len(value) > 0 {
		 extracted = value[0]
	}
}

Furthermore, I understand that HTTP headers will be inserted into the metadata, but that only applies for requests made via the HTTP gateway so requests made from a GRPC client wouldn't necessarily include this information.

But I'm not sure which values to use for header or if this information would be available in the metadata at all, or if I should check somewhere else. How can I get this request-related information from a GRPC request?

答案1

得分: 1

根据文档Metadata,在通过metadata.FromIncomingContext读取元数据后,可以通过Get方法来读取头部信息。由于MD的类型是type MD map[string][]string,你可以尝试迭代MD映射,并将其视为HTTP请求的头部信息。

注意:所有的键将自动转换为小写。

文档gRPC over http2中提到:

> 请求头部以HEADERS + CONTINUATION帧的形式作为HTTP2头部传递。

> 请求头部 → 调用定义 *自定义元数据

> HTTP2要求保留头部(以":"开头的头部)出现在所有其他头部之前。此外,实现应该在保留头部之后立即发送超时信息,并且在发送自定义元数据之前发送调用定义头部。

示例代码:

import "google.golang.org/grpc/metadata"

func (s Server) Method(ctx context.Context, *Request) (*Response, error) {
  var values []string

  md, ok := metadata.FromIncomingContext(ctx)
  for key, val := range md {
     // 获取头部的键和值
  }
  
}
英文:

Per doc Metadata, in order to read the head from the metadata through Get after metadata.FromIncomingContext. And since the MD is type MD map[string][]string you could try to iterate the map MD and treat it as same as Headers of HTTP request.

Note: all the keys will be automatically converted to lowercase

Doc gRPC over http2

> Request-Headers are delivered as HTTP2 headers in HEADERS + CONTINUATION frames.

> Request-Headers → Call-Definition *Custom-Metadata

> HTTP2 requires that reserved headers, ones starting with ":" appear before all other headers. Additionally implementations should send Timeout immediately after the reserved headers and they should send the Call-Definition headers before sending Custom-Metadata.

Sample codes

import "google.golang.org/grpc/metadata"

func (s Server) Method(ctx context.Context, *Request) (*Response, error) {
  var values []string

  md, ok := metadata.FromIncomingContext(ctx)
  for key, val := range md {
     // get key and val of headers
  }
  
}

huangapple
  • 本文由 发表于 2022年10月18日 09:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/74104751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定