Skip to content

Commit b6807b7

Browse files
authored
fix: add log (#2418)
Signed-off-by: 张启航 <[email protected]>
1 parent d4a1b47 commit b6807b7

File tree

6 files changed

+323
-58
lines changed

6 files changed

+323
-58
lines changed

worker/appm/conversion/conversion.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,12 @@ func InitCacheOperatorManaged(appID string) *v1.OperatorManaged {
128128
// InitCacheAppService init cache app service.
129129
// if store manager receive a kube model belong with service and not find in store,will create
130130
func InitCacheAppService(dbm db.Manager, serviceID, creatorID string) (*v1.AppService, error) {
131-
logrus.Infof("[InitCacheAppService] Initializing AppService for serviceID=%s, creatorID=%s", serviceID, creatorID)
131+
// Import ShouldDebugService from store package
132+
// Note: This creates a dependency, but it's for debugging only
133+
shouldDebug := shouldDebugServiceID(serviceID)
134+
if shouldDebug {
135+
logrus.Infof("[InitCacheAppService] Initializing AppService for serviceID=%s, creatorID=%s", serviceID, creatorID)
136+
}
132137
appService := &v1.AppService{
133138
AppServiceBase: v1.AppServiceBase{
134139
ServiceID: serviceID,
@@ -140,27 +145,37 @@ func InitCacheAppService(dbm db.Manager, serviceID, creatorID string) (*v1.AppSe
140145
}
141146

142147
// setup governance mode
143-
logrus.Debugf("[InitCacheAppService] Querying application info for service: %s", serviceID)
148+
if shouldDebug {
149+
logrus.Infof("[InitCacheAppService] Querying application info for service: %s", serviceID)
150+
}
144151
app, err := dbm.ApplicationDao().GetByServiceID(serviceID)
145152
if err != nil && err != bcode.ErrApplicationNotFound {
146-
logrus.Errorf("[InitCacheAppService] Failed to get app for service %s: %v", serviceID, err)
153+
if shouldDebug {
154+
logrus.Errorf("[InitCacheAppService] Failed to get app for service %s: %v", serviceID, err)
155+
}
147156
return nil, fmt.Errorf("get app based on service id(%s): %v", serviceID, err)
148157
}
149158
if app != nil {
150-
logrus.Infof("[InitCacheAppService] Found application for service %s: appID=%s, governanceMode=%s",
151-
serviceID, app.AppID, app.GovernanceMode)
159+
if shouldDebug {
160+
logrus.Infof("[InitCacheAppService] Found application for service %s: appID=%s, governanceMode=%s",
161+
serviceID, app.AppID, app.GovernanceMode)
162+
}
152163
appService.AppServiceBase.GovernanceMode = app.GovernanceMode
153164
appService.AppServiceBase.K8sApp = app.K8sApp
154-
} else {
155-
logrus.Debugf("[InitCacheAppService] No application found for service %s (this is OK)", serviceID)
156165
}
157166

158-
logrus.Debugf("[InitCacheAppService] Loading tenant service base info for service: %s", serviceID)
167+
if shouldDebug {
168+
logrus.Infof("[InitCacheAppService] Loading tenant service base info for service: %s", serviceID)
169+
}
159170
if err := TenantServiceBase(appService, dbm); err != nil {
160-
logrus.Errorf("[InitCacheAppService] TenantServiceBase failed for service %s: %v", serviceID, err)
171+
if shouldDebug {
172+
logrus.Errorf("[InitCacheAppService] TenantServiceBase failed for service %s: %v", serviceID, err)
173+
}
161174
return nil, err
162175
}
163176

164-
logrus.Infof("[InitCacheAppService] Successfully initialized AppService for service: %s", serviceID)
177+
if shouldDebug {
178+
logrus.Infof("[InitCacheAppService] Successfully initialized AppService for service: %s", serviceID)
179+
}
165180
return appService, nil
166181
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package conversion
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
var (
9+
debugServiceIDs map[string]bool
10+
)
11+
12+
func init() {
13+
debugServiceIDs = make(map[string]bool)
14+
if envIDs := os.Getenv("DEBUG_SERVICE_IDS"); envIDs != "" {
15+
ids := strings.Split(envIDs, ",")
16+
for _, id := range ids {
17+
id = strings.TrimSpace(id)
18+
if id != "" {
19+
debugServiceIDs[id] = true
20+
}
21+
}
22+
}
23+
}
24+
25+
// shouldDebugServiceID checks if we should output debug logs for this service
26+
func shouldDebugServiceID(serviceID string) bool {
27+
// If no debug service IDs configured, debug all services
28+
if len(debugServiceIDs) == 0 {
29+
return true
30+
}
31+
// Otherwise only debug configured service IDs
32+
return debugServiceIDs[serviceID]
33+
}

worker/appm/conversion/service.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,38 @@ func int32Ptr(i int) *int32 {
9999

100100
// TenantServiceBase conv tenant service base info
101101
func TenantServiceBase(as *v1.AppService, dbmanager db.Manager) error {
102-
logrus.Debugf("[TenantServiceBase] Querying tenant_service table for serviceID: %s", as.ServiceID)
102+
shouldDebug := shouldDebugServiceID(as.ServiceID)
103+
if shouldDebug {
104+
logrus.Infof("[TenantServiceBase] Querying tenant_service table for serviceID: %s", as.ServiceID)
105+
}
103106
tenantService, err := dbmanager.TenantServiceDao().GetServiceByID(as.ServiceID)
104107
if err != nil {
105108
if err == gorm.ErrRecordNotFound {
106-
logrus.Warnf("[TenantServiceBase] Service %s not found in database (ErrRecordNotFound)", as.ServiceID)
109+
if shouldDebug {
110+
logrus.Warnf("[TenantServiceBase] Service %s not found in database (ErrRecordNotFound)", as.ServiceID)
111+
}
107112
return ErrServiceNotFound
108113
}
109-
logrus.Errorf("[TenantServiceBase] Database error querying service %s: %v", as.ServiceID, err)
114+
if shouldDebug {
115+
logrus.Errorf("[TenantServiceBase] Database error querying service %s: %v", as.ServiceID, err)
116+
}
110117
return fmt.Errorf("error getting service base info by serviceID(%s) %s", as.ServiceID, err.Error())
111118
}
112-
logrus.Infof("[TenantServiceBase] Found service in DB: serviceID=%s, alias=%s, kind=%s, tenantID=%s, deployVersion=%s",
113-
as.ServiceID, tenantService.ServiceAlias, tenantService.Kind, tenantService.TenantID, tenantService.DeployVersion)
119+
if shouldDebug {
120+
logrus.Infof("[TenantServiceBase] Found service in DB: serviceID=%s, alias=%s, kind=%s, tenantID=%s, deployVersion=%s",
121+
as.ServiceID, tenantService.ServiceAlias, tenantService.Kind, tenantService.TenantID, tenantService.DeployVersion)
122+
}
114123
as.ServiceKind = dbmodel.ServiceKind(tenantService.Kind)
115-
logrus.Debugf("[TenantServiceBase] Querying tenant info for tenantID: %s", tenantService.TenantID)
124+
if shouldDebug {
125+
logrus.Infof("[TenantServiceBase] Querying tenant info for tenantID: %s", tenantService.TenantID)
126+
}
116127
tenant, err := dbmanager.TenantDao().GetTenantByUUID(tenantService.TenantID)
117128
if err != nil {
118-
logrus.Errorf("[TenantServiceBase] Failed to get tenant %s: %v", tenantService.TenantID, err)
129+
if shouldDebug {
130+
logrus.Errorf("[TenantServiceBase] Failed to get tenant %s: %v", tenantService.TenantID, err)
131+
}
119132
return fmt.Errorf("get tenant info failure %s", err.Error())
120133
}
121-
logrus.Debugf("[TenantServiceBase] Found tenant: %s", tenant.Name)
122134
as.TenantID = tenantService.TenantID
123135
if as.DeployVersion == "" {
124136
as.DeployVersion = tenantService.DeployVersion
@@ -135,18 +147,24 @@ func TenantServiceBase(as *v1.AppService, dbmanager db.Manager) error {
135147
}
136148
as.TenantName = tenant.Name
137149
if err := initTenant(as, tenant); err != nil {
138-
logrus.Errorf("[TenantServiceBase] Failed to init tenant for service %s: %v", as.ServiceID, err)
150+
if shouldDebug {
151+
logrus.Errorf("[TenantServiceBase] Failed to init tenant for service %s: %v", as.ServiceID, err)
152+
}
139153
return fmt.Errorf("conversion tenant info failure %s", err.Error())
140154
}
141155
if tenantService.Kind == dbmodel.ServiceKindThirdParty.String() {
142-
logrus.Debugf("[TenantServiceBase] Service %s is third-party, loading discovery config", as.ServiceID)
156+
if shouldDebug {
157+
logrus.Infof("[TenantServiceBase] Service %s is third-party, loading discovery config", as.ServiceID)
158+
}
143159
disCfg, _ := dbmanager.ThirdPartySvcDiscoveryCfgDao().GetByServiceID(as.ServiceID)
144160
as.SetDiscoveryCfg(disCfg)
145161
return nil
146162
}
147163

148164
if tenantService.Kind == dbmodel.ServiceKindCustom.String() {
149-
logrus.Debugf("[TenantServiceBase] Service %s is custom component", as.ServiceID)
165+
if shouldDebug {
166+
logrus.Infof("[TenantServiceBase] Service %s is custom component", as.ServiceID)
167+
}
150168
return nil
151169
}
152170
label, _ := dbmanager.TenantServiceLabelDao().GetLabelByNodeSelectorKey(as.ServiceID, "windows")

worker/appm/store/debug_helper.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package store
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
var (
9+
// DEBUG_SERVICE_IDS is a comma-separated list of service IDs to debug
10+
// Set via environment variable: DEBUG_SERVICE_IDS=service-id-1,service-id-2
11+
debugServiceIDs map[string]bool
12+
)
13+
14+
func init() {
15+
debugServiceIDs = make(map[string]bool)
16+
if envIDs := os.Getenv("DEBUG_SERVICE_IDS"); envIDs != "" {
17+
ids := strings.Split(envIDs, ",")
18+
for _, id := range ids {
19+
id = strings.TrimSpace(id)
20+
if id != "" {
21+
debugServiceIDs[id] = true
22+
}
23+
}
24+
}
25+
}
26+
27+
// ShouldDebugService checks if we should output debug logs for this service
28+
func ShouldDebugService(serviceID string) bool {
29+
// If no debug service IDs configured, debug all services
30+
if len(debugServiceIDs) == 0 {
31+
return true
32+
}
33+
// Otherwise only debug configured service IDs
34+
return debugServiceIDs[serviceID]
35+
}
36+
37+
// IsDebugMode returns true if debug mode is enabled (any service IDs configured)
38+
func IsDebugMode() bool {
39+
return len(debugServiceIDs) > 0
40+
}
41+
42+
// GetDebugServiceIDs returns the list of service IDs being debugged
43+
func GetDebugServiceIDs() []string {
44+
ids := make([]string, 0, len(debugServiceIDs))
45+
for id := range debugServiceIDs {
46+
ids = append(ids, id)
47+
}
48+
return ids
49+
}

0 commit comments

Comments
 (0)