service

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 5 Imported by: 0

README

Service

service 包定义服务内统一主上下文模型。

它只负责:

  • 定义 service.Context
  • 提供 WithContext(...) / FromContext(...) / MustFromContext(...)
  • 提供 BuildContext(...) 把入站 metadata 与当前 OTel span 结构化为服务内主上下文

它不负责:

  • 远程业务服务 DNS 建模
  • gRPC interceptor 装配
  • 出站 metadata 拼装
  • 下游服务调用

当前推荐分工:

  • middleware/grpc:在 gRPC 服务端入口创建并注入 service.Context
  • service:供 service / biz / data / log 统一读取服务内主上下文
  • invocation:负责远程业务服务 DNS 与纯出站调用语义

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithContext

func WithContext(ctx context.Context, value *Context) context.Context

WithContext 将服务主上下文注入到 ctx。

Types

type BuildContextOptions

type BuildContextOptions struct {
	ServiceAppId      string
	ServiceInstanceId string
}

BuildContextOptions 定义构建服务主上下文时需要补齐的服务自身身份。

type Context

type Context struct {
	UserId   string
	AppId    string
	TenantId string
	OrgIds   []string
	RoleIds  []string
	// TraceId 表示从当前 OTel span 提取的 trace 标识快照,不对应自定义 header。
	TraceId           string
	RouteMethod       string
	AccessMethod      string
	ServiceAppId      string
	ServiceInstanceId string
}

Context 表示当前请求在服务内部流转时的统一主上下文。

func BuildContext

func BuildContext(ctx context.Context, options BuildContextOptions) *Context

BuildContext 从入站 metadata 与运行时信息构造服务主上下文。

它只负责把服务端入口已经拿到的 metadata 与 OTel span 信息结构化, 不负责缓存 transport 原文,也不参与出站调用语义。

func FromContext

func FromContext(ctx context.Context) (*Context, bool)

FromContext 从 ctx 读取服务主上下文。

func MustFromContext

func MustFromContext(ctx context.Context) *Context

MustFromContext 从 ctx 读取服务主上下文,不存在时 panic。

Jump to

Keyboard shortcuts

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