gm

package
v0.9.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 12 Imported by: 0

README

gRPC Middleware (gm)

gm 包提供了构建 gRPC 微服务所需的通用中间件(Interceptor)。

功能列表

1. Access Logger (NewServiceAccessLogger)

提供详细的 gRPC 访问日志记录功能,支持结构化日志和人类可读日志的输出。

特性

  • 全链路追踪:自动关联 TraceId(若缺失则自动生成)。
  • 身份识别
    • Trace Identity: SourceIp, ClientIp
    • Invoke Identity: InvokeServiceAppId, InvokeServiceEndpoint (调用方信息)
    • Target Identity: TargetServiceAppId, TargetServiceEndpoint (目标服务信息)
    • User/App Identity: UserId, AppId, TenantId
  • 客户端信息:自动提取 System (Name, Ver, Type), Client (Name, Ver, Type), DeviceFormFactor 等元数据。
  • 性能监控:记录请求/响应耗时 (Duration) 和状态 (Status)。
  • 载荷记录:记录 Request/Response JSON 载荷(用于调试)。

用法

import (
	"fmt"
	
	"github.com/fireflycore/go-micro/middleware/grpc" // 别名通常建议为 gm
	"google.golang.org/grpc"
)

// 定义日志处理函数
logHandler := func(b []byte, msg string) {
    // b: JSON 格式的结构化日志
    // msg: 格式化好的人类可读字符串
    fmt.Print(msg) 
}

// 创建 gRPC Server 时注入
s := grpc.NewServer(
	grpc.UnaryInterceptor(gm.NewServiceAccessLogger(logHandler)),
)
2. Metadata Propagation (PropagateIncomingMetadata)

将入站请求的 gRPC metadata 自动透传到出站 context 中。

场景

  • 在微服务调用链中,保持 TraceIdUserIdLanguage 等上下文信息的连续传递,确保下游服务能获取到完整的调用链路信息。

用法

s := grpc.NewServer(
	grpc.UnaryInterceptor(gm.PropagateIncomingMetadata),
)
3. Service Context Injection (NewInjectServiceContext)

将当前服务的基本信息注入到 Context 中,用于在发起下游调用时,自动携带当前服务的身份信息。

注入信息

  • AppId: 当前服务 ID
  • ServiceEndpoint: 当前服务节点地址
  • ServiceAuthToken: 服务间认证 Token

用法

import (
    "github.com/fireflycore/go-micro/conf"
    "github.com/fireflycore/go-micro/middleware/grpc"
)

// 假设已有配置对象
var bootstrapConf conf.BootstrapConf 

s := grpc.NewServer(
	grpc.UnaryInterceptor(gm.NewInjectServiceContext(bootstrapConf)),
)

组合使用

通常建议使用 grpc.ChainUnaryInterceptor 组合多个中间件:

s := grpc.NewServer(
    grpc.ChainUnaryInterceptor(
        gm.PropagateIncomingMetadata,           // 1. 先透传元数据
        gm.NewInjectServiceContext(conf),       // 2. 注入当前服务身份
        gm.NewServiceAccessLogger(logHandler),  // 3. 记录访问日志
    ),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewInjectServiceContext

func NewInjectServiceContext(conf conf.BootstrapConf) grpc.UnaryServerInterceptor

NewInjectServiceContext 将服务的信息注入到上下文中

func NewServiceAccessLogger

func NewServiceAccessLogger(handle func(b []byte, msg string)) grpc.UnaryServerInterceptor

NewServiceAccessLogger 服务访问日志中间件,一般在NewInjectServiceContext中间件之前使用 handle 接收两类日志:b 为结构化 JSON,msg 为人类可读文本行; 字段包含 path/request/response/duration/status/trace_id 等,便于统一采集。

func PropagateIncomingMetadata

func PropagateIncomingMetadata(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

PropagateIncomingMetadata 将入站元数据传播到出站上下文中

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL