middleware

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GinCrossServiceMiddleware

func GinCrossServiceMiddleware(opts ...GinOption) gin.HandlerFunc

GinCrossServiceMiddleware 跨服务调用的 Gin 中间件 用于处理跨服务链路追踪,会自动从 HTTP Header 提取上游链路信息 适用于微服务架构中的服务端。

与 GinMiddleware 的区别: - GinMiddleware: 用于单体服务,不提取上游链路信息,性能更好 - GinCrossServiceMiddleware: 用于跨服务调用,会提取上游链路信息和 baggage

func GinInternalServiceMiddleware

func GinInternalServiceMiddleware(opts ...GinOption) gin.HandlerFunc

GinInternalServiceMiddleware 内部服务间调用的 Gin 中间件 行为与 GinCrossServiceMiddleware 相同(Extract 上游 trace + baggage), 适用于 manager → gateway 等内部 HTTP。

func GinMiddleware

func GinMiddleware(opts ...GinOption) gin.HandlerFunc
 GinMiddleware 是用于单体服务的 Gin 中间件
 它会在请求开始时创建一个 Span,在请求结束时结束 Span
 它会在请求发生错误时记录错误日志
 它会在请求结束时记录响应状态码
 它会在请求结束时记录请求耗时
 它会在请求结束时记录请求URL
 它会在请求结束时记录请求方法

 与 GinCrossServiceMiddleware 的区别:
 - GinMiddleware: 用于单体服务,不提取上游链路信息,性能更好
 - GinCrossServiceMiddleware: 用于跨服务调用,会提取上游链路信息和 baggage

 示例:
	// 单体服务
	r.Use(middleware.GinMiddleware())
	// 自定义 span 名称
	r.Use(middleware.GinMiddleware(
		middleware.WithSpanNameFormatter(func(c *gin.Context) string {
			return "gateway.payin.unified"
		}),
	))
	// 跨服务调用
	r.Use(middleware.GinCrossServiceMiddleware())
	// 内部服务间调用
	r.Use(middleware.GinInternalServiceMiddleware())

Types

type GinOption

type GinOption interface {
	// contains filtered or unexported methods
}

GinOption 配置 Gin 链路中间件

func WithMaxRequestBodySize

func WithMaxRequestBodySize(size int64) GinOption

WithMaxRequestBodySize 设置链路中最多记录的请求体字节数;不设置时完整记录。

func WithSpanNameFormatter

func WithSpanNameFormatter(formatter SpanNameFormatter) GinOption

WithSpanNameFormatter 自定义 span 名称;未设置时使用 defaultSpanNameFormatter 参数: - formatter: 自定义 span 名称的函数 返回: - GinOption: 配置 Gin 链路中间件

type SpanNameFormatter

type SpanNameFormatter func(*gin.Context) string

SpanNameFormatter 在 Start 前根据 gin.Context 解析 span 名称 参数: - c: gin 上下文 返回: - 解析后的 span 名称

Jump to

Keyboard shortcuts

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