Skip to content

Commit

Permalink
add error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
wildum committed May 22, 2024
1 parent 3fcd62f commit 7727de5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion internal/component/prometheus/relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"go.uber.org/atomic"
)

const name = "prometheus.relabel"

func init() {
component.Register(component.Registration{
Name: "prometheus.relabel",
Name: name,
Stability: featuregate.StabilityGenerallyAvailable,
Args: Arguments{},
Exports: Exports{},
Expand Down Expand Up @@ -107,6 +109,7 @@ func New(o component.Options, args Arguments) (*Component, error) {
if err != nil {
return nil, err
}
debuggingStreamHandler.(livedebugging.DebugStreamManager).Register(name)

data, err := o.GetServiceData(labelstore.ServiceName)
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions internal/service/livedebugging/debug_stream_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import "sync"

// DebugStreamManager defines the operations for managing debug streams.
type DebugStreamManager interface {
// Register a component by name
Register(componentName string)
// IsRegistered returns true if the component name was registered
IsRegistered(componentName string) bool
// Stream streams data for a given componentID.
Stream(componentID string, data string)
// SetStream assigns a debug stream callback to a componentID.
Expand All @@ -13,14 +17,16 @@ type DebugStreamManager interface {
}

type debugStreamManager struct {
loadMut sync.RWMutex
streams map[string]map[string]func(string)
loadMut sync.RWMutex
streams map[string]map[string]func(string)
registeredComponents map[string]struct{}
}

// NewDebugStreamManager creates a new instance of DebugStreamManager.
func NewDebugStreamManager() *debugStreamManager {
return &debugStreamManager{
streams: make(map[string]map[string]func(string)),
streams: make(map[string]map[string]func(string)),
registeredComponents: make(map[string]struct{}),
}
}

Expand Down Expand Up @@ -48,3 +54,12 @@ func (s *debugStreamManager) DeleteStream(streamID string, componentID string) {
defer s.loadMut.Unlock()
delete(s.streams[componentID], streamID)
}

func (s *debugStreamManager) Register(componentName string) {
s.registeredComponents[componentName] = struct{}{}
}

func (s *debugStreamManager) IsRegistered(componentName string) bool {
_, exist := s.registeredComponents[componentName]
return exist
}
9 changes: 9 additions & 0 deletions internal/service/livedebugging/debug_stream_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import (
"github.com/stretchr/testify/require"
)

func TestRegister(t *testing.T) {
manager := NewDebugStreamManager()
require.False(t, manager.IsRegistered("type1"))
manager.Register("type1")
require.True(t, manager.IsRegistered("type1"))
// registering a component name that has already been registered does not do anything
require.NotPanics(t, func() { manager.Register("type1") })
}

func TestStream(t *testing.T) {
manager := NewDebugStreamManager()
componentID := "component1"
Expand Down
13 changes: 13 additions & 0 deletions internal/web/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api

import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"path"
Expand Down Expand Up @@ -119,6 +120,18 @@ func (a *AlloyAPI) startDebugStream() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
componentID := vars["id"]
requestedComponent := component.ParseID(componentID)

component, err := a.alloy.GetComponent(requestedComponent, component.InfoOptions{})
if err != nil {
http.NotFound(w, r)
return
}

if !a.debuggingStreamHandler.IsRegistered(component.ComponentName) {
http.Error(w, fmt.Sprintf("Live debugging is not supported by %s", component.ComponentName), http.StatusInternalServerError)
return
}

// Buffer of 1000 entries to handle load spikes and prevent this functionality from eating up too much memory.
// TODO: in the future we may want to make this value configurable to handle heavy load
Expand Down

0 comments on commit 7727de5

Please sign in to comment.