Add initial project structure with core functionality
- Created a new Go module named 'teleport' for secure port forwarding. - Added essential files including .gitignore, LICENSE, and README.md with project details. - Implemented configuration management with YAML support in config package. - Developed core client and server functionalities for handling port forwarding. - Introduced DNS server capabilities and integrated logging with sanitization. - Established rate limiting and metrics tracking for performance monitoring. - Included comprehensive tests for core components and functionalities. - Set up CI workflows for automated testing and release management using Gitea actions.
This commit is contained in:
200
pkg/metrics/metrics.go
Normal file
200
pkg/metrics/metrics.go
Normal file
@@ -0,0 +1,200 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// NOTE: These metrics are for internal use only and are not exposed externally.
|
||||
// They are primarily used for testing, debugging, and internal monitoring.
|
||||
// No HTTP endpoints or external APIs are provided to access these metrics.
|
||||
|
||||
// Metrics collects application metrics
|
||||
type Metrics struct {
|
||||
// Connection metrics
|
||||
TotalConnections int64
|
||||
ActiveConnections int64
|
||||
RejectedConnections int64
|
||||
FailedConnections int64
|
||||
|
||||
// Request metrics
|
||||
TotalRequests int64
|
||||
SuccessfulRequests int64
|
||||
FailedRequests int64
|
||||
RateLimitedRequests int64
|
||||
|
||||
// DNS metrics
|
||||
DNSQueries int64
|
||||
DNSCustomRecords int64
|
||||
DNSForwardedQueries int64
|
||||
DNSFailedQueries int64
|
||||
|
||||
// UDP metrics
|
||||
UDPPacketsReceived int64
|
||||
UDPPacketsSent int64
|
||||
UDPReplayDetected int64
|
||||
|
||||
// Performance metrics
|
||||
AverageResponseTime time.Duration
|
||||
MaxResponseTime time.Duration
|
||||
MinResponseTime time.Duration
|
||||
|
||||
// System metrics
|
||||
StartTime time.Time
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
// Global metrics instance
|
||||
var globalMetrics = &Metrics{
|
||||
StartTime: time.Now(),
|
||||
}
|
||||
|
||||
// GetMetrics returns the global metrics instance
|
||||
func GetMetrics() *Metrics {
|
||||
return globalMetrics
|
||||
}
|
||||
|
||||
// IncrementTotalConnections increments the total connections counter
|
||||
func (m *Metrics) IncrementTotalConnections() {
|
||||
atomic.AddInt64(&m.TotalConnections, 1)
|
||||
}
|
||||
|
||||
// IncrementActiveConnections increments the active connections counter
|
||||
func (m *Metrics) IncrementActiveConnections() {
|
||||
atomic.AddInt64(&m.ActiveConnections, 1)
|
||||
}
|
||||
|
||||
// DecrementActiveConnections decrements the active connections counter
|
||||
func (m *Metrics) DecrementActiveConnections() {
|
||||
atomic.AddInt64(&m.ActiveConnections, -1)
|
||||
}
|
||||
|
||||
// IncrementRejectedConnections increments the rejected connections counter
|
||||
func (m *Metrics) IncrementRejectedConnections() {
|
||||
atomic.AddInt64(&m.RejectedConnections, 1)
|
||||
}
|
||||
|
||||
// IncrementFailedConnections increments the failed connections counter
|
||||
func (m *Metrics) IncrementFailedConnections() {
|
||||
atomic.AddInt64(&m.FailedConnections, 1)
|
||||
}
|
||||
|
||||
// IncrementTotalRequests increments the total requests counter
|
||||
func (m *Metrics) IncrementTotalRequests() {
|
||||
atomic.AddInt64(&m.TotalRequests, 1)
|
||||
}
|
||||
|
||||
// IncrementSuccessfulRequests increments the successful requests counter
|
||||
func (m *Metrics) IncrementSuccessfulRequests() {
|
||||
atomic.AddInt64(&m.SuccessfulRequests, 1)
|
||||
}
|
||||
|
||||
// IncrementFailedRequests increments the failed requests counter
|
||||
func (m *Metrics) IncrementFailedRequests() {
|
||||
atomic.AddInt64(&m.FailedRequests, 1)
|
||||
}
|
||||
|
||||
// IncrementRateLimitedRequests increments the rate limited requests counter
|
||||
func (m *Metrics) IncrementRateLimitedRequests() {
|
||||
atomic.AddInt64(&m.RateLimitedRequests, 1)
|
||||
}
|
||||
|
||||
// IncrementDNSQueries increments the DNS queries counter
|
||||
func (m *Metrics) IncrementDNSQueries() {
|
||||
atomic.AddInt64(&m.DNSQueries, 1)
|
||||
}
|
||||
|
||||
// IncrementDNSCustomRecords increments the DNS custom records counter
|
||||
func (m *Metrics) IncrementDNSCustomRecords() {
|
||||
atomic.AddInt64(&m.DNSCustomRecords, 1)
|
||||
}
|
||||
|
||||
// IncrementDNSForwardedQueries increments the DNS forwarded queries counter
|
||||
func (m *Metrics) IncrementDNSForwardedQueries() {
|
||||
atomic.AddInt64(&m.DNSForwardedQueries, 1)
|
||||
}
|
||||
|
||||
// IncrementDNSFailedQueries increments the DNS failed queries counter
|
||||
func (m *Metrics) IncrementDNSFailedQueries() {
|
||||
atomic.AddInt64(&m.DNSFailedQueries, 1)
|
||||
}
|
||||
|
||||
// IncrementUDPPacketsReceived increments the UDP packets received counter
|
||||
func (m *Metrics) IncrementUDPPacketsReceived() {
|
||||
atomic.AddInt64(&m.UDPPacketsReceived, 1)
|
||||
}
|
||||
|
||||
// IncrementUDPPacketsSent increments the UDP packets sent counter
|
||||
func (m *Metrics) IncrementUDPPacketsSent() {
|
||||
atomic.AddInt64(&m.UDPPacketsSent, 1)
|
||||
}
|
||||
|
||||
// IncrementUDPReplayDetected increments the UDP replay detected counter
|
||||
func (m *Metrics) IncrementUDPReplayDetected() {
|
||||
atomic.AddInt64(&m.UDPReplayDetected, 1)
|
||||
}
|
||||
|
||||
// RecordResponseTime records a response time
|
||||
func (m *Metrics) RecordResponseTime(duration time.Duration) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
// Update average response time (simple moving average)
|
||||
if m.AverageResponseTime == 0 {
|
||||
m.AverageResponseTime = duration
|
||||
} else {
|
||||
m.AverageResponseTime = (m.AverageResponseTime + duration) / 2
|
||||
}
|
||||
|
||||
// Update min/max response times
|
||||
if m.MaxResponseTime == 0 || duration > m.MaxResponseTime {
|
||||
m.MaxResponseTime = duration
|
||||
}
|
||||
if m.MinResponseTime == 0 || duration < m.MinResponseTime {
|
||||
m.MinResponseTime = duration
|
||||
}
|
||||
}
|
||||
|
||||
// GetUptime returns the application uptime
|
||||
func (m *Metrics) GetUptime() time.Duration {
|
||||
return time.Since(m.StartTime)
|
||||
}
|
||||
|
||||
// GetStats returns a snapshot of all metrics
|
||||
func (m *Metrics) GetStats() map[string]interface{} {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
|
||||
return map[string]interface{}{
|
||||
"uptime": m.GetUptime().String(),
|
||||
"connections": map[string]int64{
|
||||
"total": atomic.LoadInt64(&m.TotalConnections),
|
||||
"active": atomic.LoadInt64(&m.ActiveConnections),
|
||||
"rejected": atomic.LoadInt64(&m.RejectedConnections),
|
||||
"failed": atomic.LoadInt64(&m.FailedConnections),
|
||||
},
|
||||
"requests": map[string]int64{
|
||||
"total": atomic.LoadInt64(&m.TotalRequests),
|
||||
"successful": atomic.LoadInt64(&m.SuccessfulRequests),
|
||||
"failed": atomic.LoadInt64(&m.FailedRequests),
|
||||
"rate_limited": atomic.LoadInt64(&m.RateLimitedRequests),
|
||||
},
|
||||
"dns": map[string]int64{
|
||||
"queries": atomic.LoadInt64(&m.DNSQueries),
|
||||
"custom_records": atomic.LoadInt64(&m.DNSCustomRecords),
|
||||
"forwarded": atomic.LoadInt64(&m.DNSForwardedQueries),
|
||||
"failed": atomic.LoadInt64(&m.DNSFailedQueries),
|
||||
},
|
||||
"udp": map[string]int64{
|
||||
"packets_received": atomic.LoadInt64(&m.UDPPacketsReceived),
|
||||
"packets_sent": atomic.LoadInt64(&m.UDPPacketsSent),
|
||||
"replay_detected": atomic.LoadInt64(&m.UDPReplayDetected),
|
||||
},
|
||||
"performance": map[string]interface{}{
|
||||
"avg_response_time": m.AverageResponseTime.String(),
|
||||
"max_response_time": m.MaxResponseTime.String(),
|
||||
"min_response_time": m.MinResponseTime.String(),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user