interceptor

package
v0.10.4 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Copyright 2020 io.xream.sqlxb

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear()

Clear 清空所有拦截器 主要用于测试环境

func Register

func Register(i Interceptor)

Register 注册全局拦截器 拦截器按注册顺序执行

示例:

interceptor.Register(&LoggingInterceptor{})
interceptor.Register(&PrometheusInterceptor{})

func Unregister

func Unregister(name string)

Unregister 卸载拦截器(按名称)

示例:

interceptor.Unregister("logging")

Types

type Interceptor

type Interceptor interface {
	// Name 拦截器名称(用于注册/卸载)
	Name() string

	// BeforeBuild 在 Build() 之前执行
	// ⭐ 只传 Metadata,编译时强制只能设置元数据
	// ⭐ 无法修改查询逻辑(类型系统保证)
	// 返回 error 可以阻止 Build()
	BeforeBuild(meta *Metadata) error

	// AfterBuild 在 Build() 之后执行
	// 用于观察生成的 SQL(日志、监控、审计)
	// 返回 error 可以阻止后续执行
	AfterBuild(built interface{}) error
}

Interceptor 拦截器接口 用于基础设施观察(日志、监控) 不用于业务逻辑

func GetAll

func GetAll() []Interceptor

GetAll 获取所有拦截器(只读) 主要用于内部调用

type Metadata

type Metadata struct {
	// 追踪相关
	TraceID   string `json:"trace_id,omitempty"`
	RequestID string `json:"request_id,omitempty"`

	// 用户相关
	UserID   int64 `json:"user_id,omitempty"`
	TenantID int64 `json:"tenant_id,omitempty"`

	// 性能相关
	StartTime time.Time `json:"start_time,omitempty"`

	// 扩展点(自定义元数据)
	Custom map[string]interface{} `json:"custom,omitempty"`
}

Metadata 查询元数据 不影响查询逻辑,只用于观察

func (*Metadata) Get

func (m *Metadata) Get(key string) interface{}

Get 获取自定义元数据

func (*Metadata) GetBool

func (m *Metadata) GetBool(key string) bool

GetBool 获取 bool 类型的元数据

func (*Metadata) GetFloat64

func (m *Metadata) GetFloat64(key string) float64

GetFloat64 获取 float64 类型的元数据

func (*Metadata) GetInt64

func (m *Metadata) GetInt64(key string) int64

GetInt64 获取 int64 类型的元数据

func (*Metadata) GetString

func (m *Metadata) GetString(key string) string

GetString 获取字符串类型的元数据

func (*Metadata) Set

func (m *Metadata) Set(key string, value interface{})

Set 设置自定义元数据

Jump to

Keyboard shortcuts

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