Skip to content

Commit e5437f9

Browse files
committed
Merge branch 'RA-8279_improve_application_visibility_timing_logs' into RA-8661_apply_updates_from_google
2 parents 83504a8 + 61c6765 commit e5437f9

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
/trillian_log_signer
2828
/trillian.json
2929
trillian/ctfe/ct_server/ctfe_server
30+
**/Dockerfile.local

trillian/ctfe/config/config.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,47 @@
11
package config
22

33
import (
4+
"os"
5+
46
"github.com/digicert/ctutils/logging"
57
"github.com/digicert/ctutils/logging/adapters"
68
)
79

810
// InitLogging sets up the logging adapter for CTFE.
9-
func InitLogging() {
11+
// Note: This function is fail-safe. If OpenTelemetry initialization fails,
12+
// it logs an error and falls back to a no-op tracer provider to ensure the binary continues running.
13+
// Returns a shutdown function that should be deferred in main().
14+
func InitLogging() func() {
1015
// Initialize OpenTelemetry with config struct
11-
logging.InitOpenTelemetry(logging.TelemetryConfigFromEnv())
16+
shutdown := logging.InitOpenTelemetry(logging.TelemetryConfigFromEnv())
17+
18+
// Read logger backend configuration from env vars
19+
logFormat := os.Getenv("LOG_FORMAT")
20+
if logFormat == "" {
21+
logFormat = "text" // Default to text for human readability if not specified
22+
}
1223

13-
// Initialize logging adapter with JSON format
14-
logCfg := logging.Config{Format: "json"}
24+
logLevelStr := os.Getenv("LOG_LEVEL")
25+
var logLevel logging.LogLevel
26+
switch logLevelStr {
27+
case "DEBUG":
28+
logLevel = logging.DebugLevel
29+
case "INFO":
30+
logLevel = logging.InfoLevel
31+
case "WARN":
32+
logLevel = logging.WarnLevel
33+
case "ERROR":
34+
logLevel = logging.ErrorLevel
35+
default:
36+
logLevel = logging.InfoLevel
37+
}
38+
39+
logCfg := logging.Config{
40+
Format: logFormat,
41+
Level: logLevel,
42+
}
1543
adapter := adapters.NewLogrusAdapter(logCfg)
1644
logging.SetLoggerAdapter(adapter)
45+
46+
return shutdown
1747
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/digicert/ctutils/logging"
7+
)
8+
9+
func TestInitLogging(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
logFormat string
13+
logLevel string
14+
}{
15+
{"Default", "", ""},
16+
{"JSON_Debug", "json", "DEBUG"},
17+
{"Text_Error", "text", "ERROR"},
18+
{"Text_Info", "text", "INFO"},
19+
{"Invalid_Formats", "invalid", "INVALID"}, // Should fallback to defaults
20+
}
21+
22+
for _, tt := range tests {
23+
t.Run(tt.name, func(t *testing.T) {
24+
if tt.logFormat != "" {
25+
t.Setenv("LOG_FORMAT", tt.logFormat)
26+
}
27+
if tt.logLevel != "" {
28+
t.Setenv("LOG_LEVEL", tt.logLevel)
29+
}
30+
31+
// Verify shutdown function is returned and works
32+
shutdown := InitLogging()
33+
if shutdown == nil {
34+
t.Fatal("InitLogging() should return non-nil shutdown function")
35+
}
36+
defer shutdown() // Verify shutdown doesn't panic
37+
38+
// Check that a logger is set (not nil)
39+
if logging.GetLogger() == nil {
40+
t.Error("InitLogging() failed to set global logger")
41+
}
42+
})
43+
}
44+
}

trillian/ctfe/ct_server/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ func main() {
103103
ctx := context.Background()
104104

105105
// Initialize logging with OpenTelemetry support
106-
config.InitLogging()
106+
shutdown := config.InitLogging()
107+
defer shutdown()
107108

108109
keys.RegisterHandler(&keyspb.PEMKeyFile{}, pem.FromProto)
109110
keys.RegisterHandler(&keyspb.PrivateKey{}, der.FromProto)

0 commit comments

Comments
 (0)