Skip to content

Commit

Permalink
Change recentAPIKey to lastAPIKey for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Feb 12, 2025
1 parent 5e9c5c5 commit 876e191
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions test/fakeintake/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (fi *Server) handleDatadogRequest(w http.ResponseWriter, req *http.Request)

apiKey := fi.extractDatadogAPIKey(req)
if apiKey != "" {
fi.store.SetRecentAPIKey(apiKey)
fi.store.SetLastAPIKey(apiKey)
}

log.Printf("Handling Datadog %s request to %s, header %v", req.Method, req.URL.Path, redactHeader(req.Header))
Expand Down Expand Up @@ -477,7 +477,7 @@ func (fi *Server) extractDatadogAPIKey(req *http.Request) string {
}

func (fi *Server) handleGetLastAPIKey(w http.ResponseWriter, _req *http.Request) {
apiKey, err := fi.store.GetRecentAPIKey()
apiKey, err := fi.store.GetLastAPIKey()
if err != nil {
response := buildErrorResponse(err)
writeHTTPResponse(w, response)
Expand Down
6 changes: 3 additions & 3 deletions test/fakeintake/server/serverstore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (s *sqlStore) Close() {
os.Remove(s.path)
}

func (s *sqlStore) SetRecentAPIKey(_ string) {
func (s *sqlStore) SetLastAPIKey(_ string) {
// pass
}

func (s *sqlStore) GetRecentAPIKey() (string, error) {
return "", fmt.Errorf("sqlstore does not track recent APIKey")
func (s *sqlStore) GetLastAPIKey() (string, error) {
return "", fmt.Errorf("sqlstore does not track last APIKey")
}

// AppendPayload adds a payload to the store and tries parsing and adding a dumped json to the parsed store
Expand Down
19 changes: 9 additions & 10 deletions test/fakeintake/server/serverstore/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
type inMemoryStore struct {
mutex sync.RWMutex

rawPayloads map[string][]api.Payload
recentAPIKey string
rawPayloads map[string][]api.Payload
lastAPIKey string

// NbPayloads is a prometheus metric to track the number of payloads collected by route
NbPayloads *prometheus.GaugeVec
Expand All @@ -39,26 +39,25 @@ func newInMemoryStore() *inMemoryStore {
}
}

func (s *inMemoryStore) SetRecentAPIKey(key string) {
func (s *inMemoryStore) SetLastAPIKey(key string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.recentAPIKey = key
s.lastAPIKey = key
}

func (s *inMemoryStore) GetRecentAPIKey() (string, error) {
func (s *inMemoryStore) GetLastAPIKey() (string, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
lastAPIKey := s.recentAPIKey
if lastAPIKey == "" {
if s.lastAPIKey == "" {
return "", fmt.Errorf("no apiKey sent")
}
return lastAPIKey, nil
return s.lastAPIKey, nil
}

// AppendPayload adds a payload to the store and tries parsing and adding a dumped json to the parsed store
func (s *inMemoryStore) AppendPayload(route string, apiKey string, data []byte, encoding string, contentType string, collectTime time.Time) error {
// Set the recent APIKey first, to avoid deadlocking
s.SetRecentAPIKey(apiKey)
// Set the last APIKey first, to avoid deadlocking
s.SetLastAPIKey(apiKey)
s.mutex.Lock()
defer s.mutex.Unlock()
rawPayload := api.Payload{
Expand Down
8 changes: 4 additions & 4 deletions test/fakeintake/server/serverstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
type Store interface {
// AppendPayload adds a payload to the store and tries parsing and adding a dumped json to the parsed store
AppendPayload(route string, apiKey string, data []byte, encoding string, contentType string, collectTime time.Time) error
// SetRecentAPIKey sets the most recent API Key
SetRecentAPIKey(apiKey string)
// GetRecentAPIKey gets the most recent API Keys
GetRecentAPIKey() (string, error)
// SetLastAPIKey sets the last seen API Key
SetLastAPIKey(apiKey string)
// GetLastAPIKey gets the last seen API Keys
GetLastAPIKey() (string, error)
// CleanUpPayloadsOlderThan removes payloads older than the given time
CleanUpPayloadsOlderThan(time.Time)
// GetRawPayloads returns all raw payloads for a given route
Expand Down

0 comments on commit 876e191

Please sign in to comment.