Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e51069595 | |||
| 791c9905df | |||
| 6097209c48 | |||
| 1835faddc0 | |||
| 0ddc2f17b9 |
@@ -7,10 +7,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/bwmarrin/snowflake"
|
"github.com/bwmarrin/snowflake"
|
||||||
"github.com/gogf/gf/v2/crypto/gmd5"
|
"github.com/gogf/gf/v2/crypto/gmd5"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
@@ -29,8 +30,28 @@ import (
|
|||||||
var (
|
var (
|
||||||
localCache *gcache.Cache
|
localCache *gcache.Cache
|
||||||
snowflakeNode *snowflake.Node
|
snowflakeNode *snowflake.Node
|
||||||
|
snowflakeOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
ctx := context.Background()
|
||||||
|
snowflakeOnce.Do(func() {
|
||||||
|
nodeId := genv.Get("APP_NODE", 1).Int64()
|
||||||
|
// 安全范围 0~1023
|
||||||
|
if nodeId < 0 || nodeId > 1023 {
|
||||||
|
nodeId = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
node, err := snowflake.NewNode(nodeId)
|
||||||
|
if err != nil {
|
||||||
|
g.Log().Errorf(ctx, "snowflake init failed: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
snowflakeNode = node
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// getLocalCache 获取本地缓存实例
|
// getLocalCache 获取本地缓存实例
|
||||||
func getLocalCache() *gcache.Cache {
|
func getLocalCache() *gcache.Cache {
|
||||||
if localCache == nil {
|
if localCache == nil {
|
||||||
@@ -166,17 +187,8 @@ func insertHook(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 懒加载初始化全局snowflake节点,只创建一次
|
if g.IsEmpty(snowflakeNode) {
|
||||||
if snowflakeNode == nil {
|
return nil, fmt.Errorf("snowflakeNode is nil")
|
||||||
nodeId := genv.Get("APP_NODE", "").Int64()
|
|
||||||
if g.IsEmpty(nodeId) {
|
|
||||||
nodeId = 1
|
|
||||||
}
|
|
||||||
node, err := snowflake.NewNode(nodeId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
snowflakeNode = node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range in.Data {
|
for i := range in.Data {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/log/consts"
|
"gitea.redpowerfuture.com/red-future/common/log/consts"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/glog"
|
"github.com/gogf/gf/v2/os/glog"
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/log/consts"
|
"gitea.redpowerfuture.com/red-future/common/log/consts"
|
||||||
"go.mongodb.org/mongo-driver/v2/event"
|
"go.mongodb.org/mongo-driver/v2/event"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/log/model/entity"
|
"gitea.redpowerfuture.com/red-future/common/log/model/entity"
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/container/gvar"
|
"github.com/gogf/gf/v2/container/gvar"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/container/gvar"
|
"github.com/gogf/gf/v2/container/gvar"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
|||||||
3
go.mod
3
go.mod
@@ -1,5 +1,4 @@
|
|||||||
module gitea.com/red-future/common
|
module gitea.redpowerfuture.com/red-future/common
|
||||||
|
|
||||||
go 1.26.0
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
_ "gitea.com/red-future/common/consul"
|
_ "gitea.redpowerfuture.com/red-future/common/consul"
|
||||||
"gitea.com/red-future/common/jaeger"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"gitea.com/red-future/common/utils"
|
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/gclient"
|
"github.com/gogf/gf/v2/net/gclient"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
@@ -80,7 +79,6 @@ func RouteRegister(controllers []interface{}) {
|
|||||||
return fmt.Sprintf("/%s", strings.ToLower(s))
|
return fmt.Sprintf("/%s", strings.ToLower(s))
|
||||||
})
|
})
|
||||||
Httpserver.Group(convertedStr, func(group *ghttp.RouterGroup) {
|
Httpserver.Group(convertedStr, func(group *ghttp.RouterGroup) {
|
||||||
group.Middleware(jaeger.NewTracer)
|
|
||||||
group.Bind(t)
|
group.Bind(t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,12 @@ import (
|
|||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/net/gtrace"
|
"github.com/gogf/gf/v2/net/gtrace"
|
||||||
|
"go.opentelemetry.io/otel"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/codes"
|
"go.opentelemetry.io/otel/codes"
|
||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
|
"go.opentelemetry.io/otel/trace/embedded"
|
||||||
|
"go.opentelemetry.io/otel/trace/noop"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -41,6 +44,10 @@ func Init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ShutDown = shutdown
|
ShutDown = shutdown
|
||||||
|
|
||||||
|
// 包装 TracerProvider:只保留 HTTP Server 追踪,屏蔽 DB/Redis/Client 等内部 span
|
||||||
|
wrapTracerProvider()
|
||||||
|
|
||||||
g.Log().Infof(ctx, "✅ Jaeger 初始化成功: %s", jaegerAgent)
|
g.Log().Infof(ctx, "✅ Jaeger 初始化成功: %s", jaegerAgent)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -50,6 +57,32 @@ func init() {
|
|||||||
Init()
|
Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filterTracerProvider 只放行指定 instrument 的 span,其余返回 noop tracer
|
||||||
|
type filterTracerProvider struct {
|
||||||
|
embedded.TracerProvider
|
||||||
|
real trace.TracerProvider
|
||||||
|
noop trace.TracerProvider
|
||||||
|
allowed map[string]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *filterTracerProvider) Tracer(instrumentName string, opts ...trace.TracerOption) trace.Tracer {
|
||||||
|
if f.allowed[instrumentName] {
|
||||||
|
return f.real.Tracer(instrumentName, opts...)
|
||||||
|
}
|
||||||
|
return f.noop.Tracer(instrumentName, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wrapTracerProvider 包装全局 TracerProvider,只保留 HTTP Server 追踪
|
||||||
|
func wrapTracerProvider() {
|
||||||
|
otel.SetTracerProvider(&filterTracerProvider{
|
||||||
|
real: otel.GetTracerProvider(),
|
||||||
|
noop: noop.NewTracerProvider(),
|
||||||
|
allowed: map[string]bool{
|
||||||
|
"github.com/gogf/gf/v2/net/ghttp.Server": true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// NewSpan 创建新的链路追踪 Span
|
// NewSpan 创建新的链路追踪 Span
|
||||||
// spanName: Span 名称,用于在 Jaeger UI 中标识
|
// spanName: Span 名称,用于在 Jaeger UI 中标识
|
||||||
// 返回带有 Span 的 context 和 Span 对象,调用方需 defer span.End()
|
// 返回带有 Span 的 context 和 Span 对象,调用方需 defer span.End()
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitea.com/red-future/common/log/model/dto"
|
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
|
||||||
"gitea.com/red-future/common/log/service"
|
"gitea.redpowerfuture.com/red-future/common/log/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
type operationLog struct{}
|
type operationLog struct{}
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ package dao
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/db/mongo"
|
"gitea.redpowerfuture.com/red-future/common/db/mongo"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/log/consts"
|
"gitea.redpowerfuture.com/red-future/common/log/consts"
|
||||||
"gitea.com/red-future/common/log/model/dto"
|
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
|
||||||
"gitea.com/red-future/common/log/model/entity"
|
"gitea.redpowerfuture.com/red-future/common/log/model/entity"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package dto
|
package dto
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gtime"
|
"github.com/gogf/gf/v2/os/gtime"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package entity
|
package entity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OperationLog 操作日志实体 - 用于记录数据增删改操作行为
|
// OperationLog 操作日志实体 - 用于记录数据增删改操作行为
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/log/dao"
|
"gitea.redpowerfuture.com/red-future/common/log/dao"
|
||||||
"gitea.com/red-future/common/log/model/dto"
|
"gitea.redpowerfuture.com/red-future/common/log/model/dto"
|
||||||
logEntity "gitea.com/red-future/common/log/model/entity"
|
logEntity "gitea.redpowerfuture.com/red-future/common/log/model/entity"
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/alibaba/sentinel-golang/api"
|
"github.com/alibaba/sentinel-golang/api"
|
||||||
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
|
"github.com/alibaba/sentinel-golang/core/circuitbreaker"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
commonHttp "gitea.com/red-future/common/http"
|
commonHttp "gitea.redpowerfuture.com/red-future/common/http"
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/database/gredis"
|
"github.com/gogf/gf/v2/database/gredis"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package swagger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.com/red-future/common/consul"
|
"gitea.redpowerfuture.com/red-future/common/consul"
|
||||||
"gitea.com/red-future/common/http"
|
"gitea.redpowerfuture.com/red-future/common/http"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/net/ghttp"
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
"github.com/gogf/gf/v2/util/gconv"
|
"github.com/gogf/gf/v2/util/gconv"
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
||||||
"github.com/gogf/gf/v2/container/gvar"
|
"github.com/gogf/gf/v2/container/gvar"
|
||||||
"github.com/gogf/gf/v2/database/gredis"
|
"github.com/gogf/gf/v2/database/gredis"
|
||||||
"github.com/gogf/gf/v2/errors/gcode"
|
"github.com/gogf/gf/v2/errors/gcode"
|
||||||
@@ -583,7 +583,8 @@ func GetLocalBaseURL(ctx context.Context) string {
|
|||||||
|
|
||||||
// GetCallbackURL 获取回调地址(完整 URL)
|
// GetCallbackURL 获取回调地址(完整 URL)
|
||||||
func GetCallbackURL(ctx context.Context, path string) string {
|
func GetCallbackURL(ctx context.Context, path string) string {
|
||||||
baseURL := GetLocalBaseURL(ctx)
|
//baseURL := GetLocalBaseURL(ctx)
|
||||||
|
baseURL := "http://" + GetLocalAddress(ctx)
|
||||||
// 确保 path 以 / 开头
|
// 确保 path 以 / 开头
|
||||||
if !strings.HasPrefix(path, "/") {
|
if !strings.HasPrefix(path, "/") {
|
||||||
path = "/" + path
|
path = "/" + path
|
||||||
|
|||||||
Reference in New Issue
Block a user