Skip to content

Commit 3dc6d84

Browse files
Merge pull request #45 from hhftechnology/dev
Dev
2 parents a30fea8 + fdb7851 commit 3dc6d84

File tree

4 files changed

+544
-156
lines changed

4 files changed

+544
-156
lines changed

backend/Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ COPY --from=builder /app/main .
3636
RUN mkdir -p /logs /maxmind
3737

3838
# Expose port
39-
EXPOSE 3001
39+
EXPOSE 3001 4317 4318
40+
41+
# Set default environment variables
4042

4143
# Set default environment variables
4244
ENV PORT=3001
43-
ENV TRAEFIK_LOG_FILE=/logs/traefik.log
45+
ENV OTLP_ENABLED=true
46+
ENV OTLP_GRPC_PORT=4317
47+
ENV OTLP_HTTP_PORT=4318
48+
ENV OTLP_DEBUG=true
49+
ENV TRAEFIK_LOG_FILE=
4450
ENV USE_MAXMIND=false
4551
ENV MAXMIND_DB_PATH=/maxmind/GeoLite2-City.mmdb
4652
ENV MAXMIND_FALLBACK_ONLINE=true

backend/main.go

Lines changed: 156 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
)
2020

2121
var (
22-
logParser *LogParser
22+
logParser *LogParser
2323
otlpReceiver *OTLPReceiver
24-
upgrader = websocket.Upgrader{
24+
upgrader = websocket.Upgrader{
2525
CheckOrigin: func(r *http.Request) bool {
2626
return true // Allow connections from any origin
2727
},
@@ -41,6 +41,20 @@ func main() {
4141
// Initialize log parser
4242
logParser = NewLogParser()
4343

44+
// Initialize OTLP receiver if enabled
45+
otlpConfig := GetOTLPConfig()
46+
if otlpConfig.Enabled {
47+
otlpReceiver = NewOTLPReceiver(logParser, otlpConfig)
48+
log.Printf("OTLP receiver initialized - GRPC:%d, HTTP:%d", otlpConfig.GRPCPort, otlpConfig.HTTPPort)
49+
50+
// Start OTLP receiver
51+
if err := otlpReceiver.Start(); err != nil {
52+
log.Printf("Failed to start OTLP receiver: %v", err)
53+
}
54+
} else {
55+
log.Printf("OTLP receiver is disabled")
56+
}
57+
4458
// Setup graceful shutdown
4559
ctx, cancel := context.WithCancel(context.Background())
4660
defer cancel()
@@ -82,6 +96,12 @@ func main() {
8296
r.POST("/api/set-log-file", setLogFile)
8397
r.POST("/api/set-log-files", setLogFiles)
8498

99+
// OTLP API Routes
100+
r.GET("/api/otlp/status", getOTLPStatus)
101+
r.POST("/api/otlp/start", startOTLPReceiver)
102+
r.POST("/api/otlp/stop", stopOTLPReceiver)
103+
r.GET("/api/otlp/stats", getOTLPStats)
104+
85105
// MaxMind API Routes
86106
r.GET("/api/maxmind/config", getMaxMindConfig)
87107
r.POST("/api/maxmind/reload", reloadMaxMindDatabase)
@@ -96,21 +116,30 @@ func main() {
96116
// WebSocket endpoint
97117
r.GET("/ws", handleWebSocket)
98118

99-
// Start watching log files from environment variable
119+
// Handle log files ONLY if OTLP is disabled OR if TRAEFIK_LOG_FILE is explicitly set
100120
logFile := os.Getenv("TRAEFIK_LOG_FILE")
101-
if logFile == "" {
102-
logFile = "/logs/traefik.log"
103-
}
121+
122+
// FIXED: Only watch log files if explicitly configured or OTLP is disabled
123+
if !otlpConfig.Enabled || (logFile != "" && logFile != "none") {
124+
if logFile == "" {
125+
logFile = "/logs/traefik.log" // Default only when OTLP is disabled
126+
}
127+
128+
log.Printf("Setting up log file monitoring for: %s", logFile)
104129

105-
// Check if multiple log files are specified
106-
if strings.Contains(logFile, ",") {
107-
logFiles := strings.Split(logFile, ",")
108-
for i := range logFiles {
109-
logFiles[i] = strings.TrimSpace(logFiles[i])
130+
// Check if multiple log files are specified
131+
if strings.Contains(logFile, ",") {
132+
logFiles := strings.Split(logFile, ",")
133+
for i := range logFiles {
134+
logFiles[i] = strings.TrimSpace(logFiles[i])
135+
}
136+
go logParser.SetLogFiles(logFiles)
137+
} else {
138+
go logParser.SetLogFiles([]string{logFile})
110139
}
111-
go logParser.SetLogFiles(logFiles)
112140
} else {
113-
go logParser.SetLogFiles([]string{logFile})
141+
log.Printf("Running in OTLP-only mode - log file monitoring disabled")
142+
log.Printf("OTLP_ENABLED=%t, TRAEFIK_LOG_FILE='%s'", otlpConfig.Enabled, logFile)
114143
}
115144

116145
// Start the server
@@ -121,6 +150,7 @@ func main() {
121150

122151
log.Printf("Server running on port %s", port)
123152
log.Printf("MaxMind configuration: %+v", GetMaxMindConfig())
153+
log.Printf("OTLP configuration: %+v", otlpConfig)
124154
log.Printf("WebSocket clients tracking enabled")
125155

126156
// Start server with graceful shutdown
@@ -155,6 +185,12 @@ func cleanup() {
155185
close(healthStop)
156186
}
157187

188+
// Stop OTLP receiver
189+
if otlpReceiver != nil {
190+
log.Println("Stopping OTLP receiver...")
191+
otlpReceiver.Stop()
192+
}
193+
158194
// Stop log parser
159195
if logParser != nil {
160196
logParser.Stop()
@@ -375,6 +411,7 @@ func getLogs(c *gin.Context) {
375411
params.Filters.Router = c.Query("router")
376412
params.Filters.HideUnknown = c.Query("hideUnknown") == "true"
377413
params.Filters.HidePrivateIPs = c.Query("hidePrivateIPs") == "true"
414+
params.Filters.DataSource = c.Query("dataSource")
378415

379416
result := logParser.GetLogs(params)
380417
c.JSON(http.StatusOK, result)
@@ -521,6 +558,96 @@ func getWebSocketStatus(c *gin.Context) {
521558
c.JSON(http.StatusOK, status)
522559
}
523560

561+
// OTLP API Route Handlers
562+
func getOTLPStatus(c *gin.Context) {
563+
if otlpReceiver == nil {
564+
c.JSON(http.StatusServiceUnavailable, gin.H{
565+
"error": "OTLP receiver is not initialized",
566+
"enabled": false,
567+
})
568+
return
569+
}
570+
571+
stats := otlpReceiver.GetStats()
572+
config := otlpReceiver.GetConfig()
573+
574+
status := gin.H{
575+
"config": config,
576+
"stats": stats,
577+
"running": otlpReceiver.IsRunning(),
578+
}
579+
580+
c.JSON(http.StatusOK, status)
581+
}
582+
583+
// Handler for /api/otlp/stats
584+
func getOTLPStats(c *gin.Context) {
585+
if otlpReceiver == nil {
586+
c.JSON(http.StatusServiceUnavailable, gin.H{
587+
"error": "OTLP receiver is not initialized",
588+
})
589+
return
590+
}
591+
stats := otlpReceiver.GetStats()
592+
c.JSON(http.StatusOK, stats)
593+
}
594+
595+
func startOTLPReceiver(c *gin.Context) {
596+
if otlpReceiver == nil {
597+
c.JSON(http.StatusServiceUnavailable, gin.H{
598+
"success": false,
599+
"error": "OTLP receiver is not initialized",
600+
})
601+
return
602+
}
603+
604+
config := otlpReceiver.GetConfig()
605+
if !config.Enabled {
606+
c.JSON(http.StatusBadRequest, gin.H{
607+
"success": false,
608+
"error": "OTLP receiver is not enabled in configuration",
609+
})
610+
return
611+
}
612+
613+
if err := otlpReceiver.Start(); err != nil {
614+
c.JSON(http.StatusInternalServerError, gin.H{
615+
"success": false,
616+
"error": err.Error(),
617+
})
618+
return
619+
}
620+
621+
c.JSON(http.StatusOK, gin.H{
622+
"success": true,
623+
"message": "OTLP receiver started successfully",
624+
"status": otlpReceiver.GetStats(),
625+
})
626+
}
627+
628+
func stopOTLPReceiver(c *gin.Context) {
629+
if otlpReceiver == nil {
630+
c.JSON(http.StatusServiceUnavailable, gin.H{
631+
"success": false,
632+
"error": "OTLP receiver is not initialized",
633+
})
634+
return
635+
}
636+
637+
if err := otlpReceiver.Stop(); err != nil {
638+
c.JSON(http.StatusInternalServerError, gin.H{
639+
"success": false,
640+
"error": err.Error(),
641+
})
642+
return
643+
}
644+
645+
c.JSON(http.StatusOK, gin.H{
646+
"success": true,
647+
"message": "OTLP receiver stopped successfully",
648+
})
649+
}
650+
524651
func healthCheck(c *gin.Context) {
525652
config := GetMaxMindConfig()
526653

@@ -544,6 +671,22 @@ func healthCheck(c *gin.Context) {
544671
},
545672
}
546673

674+
// Add OTLP status if receiver exists
675+
if otlpReceiver != nil {
676+
otlpConfig := otlpReceiver.GetConfig()
677+
health["otlp"] = gin.H{
678+
"enabled": otlpConfig.Enabled,
679+
"running": otlpReceiver.IsRunning(),
680+
"config": otlpConfig,
681+
}
682+
} else {
683+
health["otlp"] = gin.H{
684+
"enabled": false,
685+
"running": false,
686+
"error": "OTLP receiver not initialized",
687+
}
688+
}
689+
547690
if config.DatabaseError != "" {
548691
health["maxmind"].(gin.H)["error"] = config.DatabaseError
549692
}

0 commit comments

Comments
 (0)