Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Separate out Log Level for SQL driver/ORM #970

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
| AUTH_DISABLE | Disable RBAC check for resources | false (default) |
| AUTH_IMPERSONATE | Enable RBAC impersonation | true (default) |
| LOG_LEVEL | Log level for api server | info (default) |
| SQL_LOG_LEVEL | Log level for gorm logger | warn (default) |
| LOGS_API | Enable logs storage service | false (default) |
| LOGS_TYPE | Determine Logs storage backend type | File (default) |
| LOGS_BUFFER_SIZE | Buffer for streaming logs | 32768 (default) |
Expand Down
9 changes: 6 additions & 3 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (

_ "net/http/pprof"

serverdb "github.com/tektoncd/results/pkg/api/server/db"

"github.com/golang-jwt/jwt/v4"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
Expand All @@ -66,7 +68,6 @@ import (
"google.golang.org/grpc/reflection"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"k8s.io/apimachinery/pkg/util/wait"
)

Expand Down Expand Up @@ -105,10 +106,12 @@ func main() {
var err error

dbURI := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s sslrootcert=%s", serverConfig.DB_HOST, serverConfig.DB_USER, serverConfig.DB_PASSWORD, serverConfig.DB_NAME, serverConfig.DB_PORT, serverConfig.DB_SSLMODE, serverConfig.DB_SSLROOTCERT)

gormConfig := &gorm.Config{}
if log.Level() != zap.DebugLevel {
gormConfig.Logger = gormlogger.Default.LogMode(gormlogger.Silent)
if err = serverdb.SetLogLevel(serverConfig.SQL_LOG_LEVEL); err != nil {
log.Warnf("Failed to configure sql log level: %v", err)
}

// Retry database connection, sometimes the database is not ready to accept connection
err = wait.PollImmediate(10*time.Second, 2*time.Minute, func() (bool, error) { //nolint:staticcheck
db, err = gorm.Open(postgres.Open(dbURI), gormConfig)
Expand Down
1 change: 1 addition & 0 deletions config/base/env/config
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TLS_PATH=/etc/tls
AUTH_DISABLE=false
AUTH_IMPERSONATE=true
LOG_LEVEL=info
SQL_LOG_LEVEL=warn
LOGS_API=false
LOGS_TYPE=File
LOGS_BUFFER_SIZE=32768
Expand Down
1 change: 1 addition & 0 deletions pkg/api/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
PROMETHEUS_PORT string `mapstructure:"PROMETHEUS_PORT"`
PROMETHEUS_HISTOGRAM bool `mapstructure:"PROMETHEUS_HISTOGRAM"`
LOG_LEVEL string `mapstructure:"LOG_LEVEL"`
SQL_LOG_LEVEL string `mapstructure:"SQL_LOG_LEVEL"`
TLS_PATH string `mapstructure:"TLS_PATH"`

GRPC_WORKER_POOL int `mapstructure:"GRPC_WORKER_POOL"`
Expand Down
40 changes: 40 additions & 0 deletions pkg/api/server/db/log_level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2025 The Tekton Authors
//
// Licensed 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.

// Package db defines database models for Result data.
package db

import (
"errors"

gormlogger "gorm.io/gorm/logger"
)

var (
logLevel = map[string]gormlogger.LogLevel{
"silent": gormlogger.Silent,
"error": gormlogger.Error,
"warn": gormlogger.Warn,
"info": gormlogger.Info,
}
)

// SetLogLevel for the Default GormLogger
func SetLogLevel(level string) error {
if _, ok := logLevel[level]; !ok {
return errors.New("undefined log level for sql")
}
gormlogger.Default.LogMode(logLevel[level])
return nil
}