Skip to content
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
26 changes: 12 additions & 14 deletions common/monitoring/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"context"
"fmt"
"net/http"
"sync/atomic"
"time"

"github.com/AliceO2Group/Control/common/logger"
Expand All @@ -38,8 +39,8 @@ import (
)

var (
// scraping endpoint implementation
server *http.Server
// atomic holder for the HTTP server instance
server atomic.Pointer[http.Server]
// objects to store incoming metrics
metricsInternal *MetricsAggregate
metricsHistogramInternal *MetricsReservoirSampling
Expand Down Expand Up @@ -154,33 +155,30 @@ func handleFunc(endpointName string) {
//
// If we attempt send more messages than the size of the buffer, these overflowing messages will be ignored and warning will be logged.
func Run(port uint16, endpointName string) error {
if IsRunning() {
localServer := &http.Server{Addr: fmt.Sprintf(":%d", port)}
// only one Run should initialize and serve
if !server.CompareAndSwap(nil, localServer) {
return nil
}

initChannels()

go eventLoop()

server = &http.Server{Addr: fmt.Sprintf(":%d", port)}
handleFunc(endpointName)
return server.ListenAndServe()
// block until Shutdown is called
return localServer.ListenAndServe()
}

func Stop() {
if !IsRunning() {
localServer := server.Swap(nil)
if localServer == nil {
return
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(ctx)

localServer.Shutdown(ctx)
endChannel <- struct{}{}
<-endChannel
server = nil
}

func IsRunning() bool {
return server != nil
return server.Load() != nil
}
12 changes: 2 additions & 10 deletions common/monitoring/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,12 @@ func TestStartMultipleStop(t *testing.T) {
Stop()
}

func cleaningUpAfterTest() {
Stop()
}

func initTest() {
go Run(12345, "notimportant")
}

// decorator function that properly inits and cleans after higher level test of Monitoring package
func testFunction(t *testing.T, testToRun func(*testing.T)) {
initTest()
go Run(12345, "notimportant")
isRunningWithTimeout(t, time.Second)
testToRun(t)
cleaningUpAfterTest()
Stop()
}

func TestSendingSingleMetric(t *testing.T) {
Expand Down
Loading