Skip to content

Commit

Permalink
添加apm包版本查询函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Oct 4, 2024
1 parent a64b671 commit 75f5fbe
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
26 changes: 26 additions & 0 deletions apm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package authkratos

import (
"github.com/yyle88/zaplog"
"go.elastic.co/apm/v2"
"go.uber.org/zap"
)

// CheckApmAgentVersion 检查apm包版本是否相同,这是因为apm作为单独的模块,假如使用的版本不同,逻辑就无法正常执行
// 因此建议是所有使用到 apm 的三方包都能够实现这个函数
// 以确保不同模块使用的apm包版本相同
func CheckApmAgentVersion(version string) bool {
if agentVersion := apm.AgentVersion; version != agentVersion {
zaplog.LOGGER.LOG.Warn("check apm agent versions not match", zap.String("arg_version", version), zap.String("pkg_version", agentVersion))
return false
}
return true
}

// GetApmAgentVersion 获得版本号
// 假如你用的我的包,我的包 go.mod 里面引用的 apm 是 v2.0.0 的,这里就会返回 v2.0.0 版本号字符串
// 假如你项目里直接用到 apm 包,则你需要检查你的包和我的包版本是否相同
// 假如不同,逻辑不通
func GetApmAgentVersion() string {
return apm.AgentVersion
}
16 changes: 16 additions & 0 deletions apm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package authkratos

import (
"testing"

"github.com/stretchr/testify/require"
"go.elastic.co/apm/v2"
)

func TestGetApmAgentVersion(t *testing.T) {
t.Log(GetApmAgentVersion())
}

func TestCheckApmAgentVersion(t *testing.T) {
require.True(t, CheckApmAgentVersion(apm.AgentVersion))
}
4 changes: 2 additions & 2 deletions authkratossimple/auth_kratos_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func NewConfig(field string, check CheckFunc, selectPath *authkratospath.SelectP
}
}

func (a *Config) SetEnable(v bool) {
a.enable = v
func (a *Config) SetEnable(enable bool) {
a.enable = enable
}

func (a *Config) IsEnable() bool {
Expand Down
4 changes: 2 additions & 2 deletions authkratostokens/auth_kratos_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewConfig(field string, tokens map[string]string, selectPath *authkratospat
}
}

func (a *Config) SetEnable(v bool) {
a.enable = v
func (a *Config) SetEnable(enable bool) {
a.enable = enable
}

func (a *Config) IsEnable() bool {
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ require (
github.com/go-kratos/kratos/v2 v2.8.0
github.com/go-redis/redis_rate/v10 v10.0.1
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
github.com/yyle88/erero v1.0.11
github.com/yyle88/must v0.0.3
github.com/yyle88/neatjson v0.0.6
github.com/yyle88/zaplog v0.0.10
go.elastic.co/apm/v2 v2.6.2
go.uber.org/zap v1.27.0
)

require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/elastic/go-sysinfo v1.14.2 // indirect
github.com/elastic/go-windows v1.0.2 // indirect
Expand All @@ -23,6 +27,7 @@ require (
github.com/go-playground/form/v4 v4.2.1 // indirect
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
Expand All @@ -37,10 +42,8 @@ require (
github.com/yyle88/runpath v1.0.9 // indirect
github.com/yyle88/sure v0.0.23 // indirect
github.com/yyle88/syntaxgo v0.0.27 // indirect
github.com/yyle88/zaplog v0.0.10 // indirect
go.elastic.co/fastjson v1.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f // indirect
Expand Down
4 changes: 2 additions & 2 deletions passkratosrandom/pass_kratos_random.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func NewConfig(
}
}

func (a *Config) SetEnable(v bool) {
a.enable = v
func (a *Config) SetEnable(enable bool) {
a.enable = enable
}

func (a *Config) IsEnable() bool {
Expand Down
12 changes: 6 additions & 6 deletions ratekratoslimits/rate_kratos_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ import (
type Config struct {
rateLimitBottle *redis_rate.Limiter
rule *redis_rate.Limit
ucGetUniqueCode func(ctx context.Context) string
parseUniqueCode func(ctx context.Context) string
selectPath *authkratospath.SelectPath
enable bool
}

func NewConfig(
rateLimitBottle *redis_rate.Limiter,
rule *redis_rate.Limit,
ucGetUniqueCode func(ctx context.Context) string,
parseUniqueCode func(ctx context.Context) string,
selectPath *authkratospath.SelectPath,
) *Config {
return &Config{
rateLimitBottle: rateLimitBottle,
rule: rule,
ucGetUniqueCode: ucGetUniqueCode,
parseUniqueCode: parseUniqueCode,
selectPath: selectPath,
enable: true,
}
}

func (a *Config) SetEnable(v bool) {
a.enable = v
func (a *Config) SetEnable(enable bool) {
a.enable = enable
}

func (a *Config) IsEnable() bool {
Expand Down Expand Up @@ -88,7 +88,7 @@ func middlewareFunc(cfg *Config, LOGGER log.Logger) middleware.Middleware {
return handleFunc(ctx, req)
}

uck := cfg.ucGetUniqueCode(ctx)
uck := cfg.parseUniqueCode(ctx)

rls, err := cfg.rateLimitBottle.Allow(ctx, uck, rateLimitRule)
if err != nil {
Expand Down

0 comments on commit 75f5fbe

Please sign in to comment.