Skip to content

Commit a6ac2e5

Browse files
committed
add effective log in alloy
1 parent 5b06f76 commit a6ac2e5

File tree

2 files changed

+18
-72
lines changed

2 files changed

+18
-72
lines changed

internal/store/fleetdb/attributes_test.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"log"
98
"net/http"
109
"net/http/httptest"
1110
"os"
@@ -291,77 +290,6 @@ func Test_FleetDB_CreateUpdateServerComponents_ObjectsEqual(t *testing.T) {
291290
}
292291
}
293292

294-
func Test_FleetDB_CreateDuplicatedComponents(t *testing.T) {
295-
serverID, _ := uuid.Parse(fixtures.TestserverID_Dell_fc167440)
296-
handler := http.NewServeMux()
297-
298-
asBytes, err := json.Marshal(fleetdbapi.ServerComponentSlice{})
299-
if err != nil {
300-
log.Fatal(err)
301-
}
302-
303-
records := []byte(`{
304-
"page_size": 100,
305-
"page": 1,
306-
"page_count": 29,
307-
"_links": {
308-
"self": {
309-
"href": "/api/v1/servers/fc167440-18d3-4455-b5ee-1c8e347b3f36/components"
310-
},
311-
"first": {
312-
"href": "/api/v1/servers/fc167440-18d3-4455-b5ee-1c8e347b3f36/components?page=1"
313-
},
314-
"last": {
315-
"href": "/api/v1/servers/fc167440-18d3-4455-b5ee-1c8e347b3f36/components?page=0"
316-
}
317-
},
318-
"records": %s
319-
}`)
320-
321-
getResponse := []byte(fmt.Sprintf(string(records), asBytes))
322-
323-
// get components query
324-
handler.HandleFunc(
325-
fmt.Sprintf("/api/v1/servers/%s/components", serverID.String()),
326-
func(w http.ResponseWriter, r *http.Request) {
327-
switch r.Method {
328-
case http.MethodGet:
329-
w.Header().Set("Content-Type", "application/json")
330-
_, _ = w.Write(getResponse)
331-
case http.MethodPost:
332-
// w.Header().Set("Content-Type", "application/json")
333-
http.Error(w, `hollow client received a server error - response code: 400, message: , details: duplicate key value violates unique constraint idx_server_components: Key (server_id, serial, server_component_type_id)=('ffe293bf-b331-4c27-b48f-e0f6ffb8337f', '80AD01222796639497', 'b2dee190-d4e5-4b94-8c10-75383003d836') already exists.: pq: duplicate key value violates unique constraint \"idx_server_components\"","time":"2024-06-26T03:23:35Z"`, http.StatusBadRequest)
334-
default:
335-
t.Fatal("expected POST request, got: " + r.Method)
336-
}
337-
},
338-
)
339-
340-
// get firmwares query
341-
handler.HandleFunc(
342-
"/api/v1/server-component-firmwares",
343-
func(w http.ResponseWriter, r *http.Request) {
344-
switch r.Method {
345-
case http.MethodGet:
346-
w.Header().Set("Content-Type", "application/json")
347-
_, _ = w.Write([]byte(`{}`))
348-
default:
349-
t.Fatal("expected GET request, got: " + r.Method)
350-
}
351-
},
352-
)
353-
354-
mock := httptest.NewServer(handler)
355-
p := testStoreInstance(t, mock.URL)
356-
357-
device := &model.Asset{ID: serverID.String(), Vendor: "dell", Inventory: fixtures.CopyDevice(fixtures.R6515_fc167440)}
358-
359-
err = p.createUpdateServerComponents(context.TODO(), serverID, device)
360-
if err != nil {
361-
t.Fatal(err)
362-
}
363-
}
364-
365293
func Test_FleetDB_CreateUpdateServerComponents_ObjectsUpdated(t *testing.T) {
366294
// comment left here for future reference
367295
//

internal/store/fleetdb/fleetdb.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66
"reflect"
7+
"regexp"
78
"strings"
89

910
"github.com/bmc-toolbox/common"
@@ -387,6 +388,23 @@ func (r *Store) createUpdateServerComponents(ctx context.Context, serverID uuid.
387388

388389
_, err = r.CreateComponents(ctx, serverID, add)
389390
if err != nil {
391+
// Log the specific component if it causes duplicate key error.
392+
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
393+
pattern := `serial, server_component_type_id\)=\('.*', '([^']*)', '.*'\)`
394+
re := regexp.MustCompile(pattern)
395+
match := re.FindStringSubmatch(err.Error())
396+
if len(match) > 1 {
397+
for i := range add {
398+
component := &add[i] // Use pointer to the component
399+
if component.Serial == match[1] {
400+
r.logger.WithFields(logrus.Fields{
401+
"device inventory": device.Inventory,
402+
}).Error(err)
403+
break
404+
}
405+
}
406+
}
407+
}
390408
// count error
391409
metrics.FleetDBAPIQueryErrorCount.With(stageLabel).Inc()
392410

0 commit comments

Comments
 (0)