Skip to content

Commit e3f488d

Browse files
committed
feat(search): improve search for events
simplified search fields
1 parent 64cd556 commit e3f488d

29 files changed

Lines changed: 568 additions & 173 deletions

acceptance/petstore_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,16 @@ func (suite *PetStoreSuite) TestEvents() {
303303
assert.True(t, ok, "event should be a map[string]any")
304304
assert.NotNil(t, evt)
305305
assert.Equal(t, "Event", evt["type"])
306-
assert.Equal(t, "GET http://127.0.0.1:18080/user/bob", evt["title"])
306+
assert.Equal(t, "http://127.0.0.1:18080/user/bob", evt["title"])
307307
assert.Equal(t, "Swagger Petstore", evt["domain"])
308+
params := evt["params"].(map[string]any)
309+
require.Len(t, params, 6)
310+
require.Equal(t, "event", params["type"])
311+
require.Equal(t, "http", params["traits.namespace"])
312+
require.Equal(t, "Swagger Petstore", params["traits.name"])
313+
require.Equal(t, "/user/{username}", params["traits.path"])
314+
require.Equal(t, "GET", params["traits.method"])
315+
require.Equal(t, "Swagger Petstore", params["traits.name"])
308316
}),
309317
)
310318
}

api/handler_events_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ func TestHandler_Events(t *testing.T) {
9595
}
9696
r = r.WithContext(openapi.NewContext(context.Background(), params))
9797

98-
_, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http"))
98+
traits := events.NewTraits().WithNamespace("http")
99+
ctx, err := openapi.NewLogEventContext(r, false, traits)
100+
require.NoError(t, err)
101+
l, _ := openapi.LogEventFromContext(ctx)
102+
err = sm.Push(l, traits)
99103
require.NoError(t, err)
100104
try.Handler(t,
101105
http.MethodGet,
@@ -148,7 +152,11 @@ func TestHandler_Events(t *testing.T) {
148152
}
149153
r = r.WithContext(openapi.NewContext(context.Background(), params))
150154

151-
_, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http"))
155+
traits := events.NewTraits().WithNamespace("http")
156+
ctx, err := openapi.NewLogEventContext(r, false, traits)
157+
require.NoError(t, err)
158+
l, _ := openapi.LogEventFromContext(ctx)
159+
err = sm.Push(l, traits)
152160
require.NoError(t, err)
153161
try.Handler(t,
154162
http.MethodGet,

engine/common/host.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ func (e *JobExecution) Title() string {
182182
return e.Tags["name"]
183183
}
184184

185-
func (e *JobExecution) Metadata() map[string]string {
186-
return nil
187-
}
188-
189185
func (e *JobExecution) Domain() string {
190186
return "Job Execution"
191187
}

mcp/run_events_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ func (t *testEvent) Title() string {
1919
return t.Name
2020
}
2121

22-
func (t *testEvent) Metadata() map[string]string { return nil }
23-
2422
func TestEvents(t *testing.T) {
2523
testcases := []struct {
2624
name string

mcp/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ op`},
246246
r, err := s.GetRunResponse(
247247
context.Background(),
248248
mcp.RunInput{
249-
Code: `const errors = mokapi.search('+api:"Petstore" +event.data.response.statusCode:>=400 +type:event')
249+
Code: `const errors = mokapi.search('+api:"Petstore" +response.statusCode:>=400 +type:event')
250250
errors.items.map(x => mokapi.getEvent(x.metadata.id))`,
251251
},
252252
)

providers/asyncapi3/kafka/store/log.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,45 @@ func (l *KafkaMessageLog) Title() string {
3939
}
4040
}
4141

42-
func (l *KafkaMessageLog) Metadata() map[string]string {
43-
return nil
42+
type headerIndex struct {
43+
Key string `json:"key"`
44+
Value string `json:"value"`
45+
}
46+
47+
func (l *KafkaMessageLog) IndexFields() map[string]any {
48+
m := map[string]any{
49+
"clientId": l.ClientId,
50+
"offset": l.Offset,
51+
"messageId": l.MessageId,
52+
"partition": l.Partition,
53+
}
54+
55+
if l.Key.Value != "" {
56+
m["key"] = l.Key.Value
57+
} else {
58+
m["key"] = string(l.Key.Binary)
59+
}
60+
61+
if l.Message.Value != "" {
62+
m["message"] = l.Message.Value
63+
} else {
64+
m["message"] = string(l.Message.Binary)
65+
}
66+
67+
var headers []headerIndex
68+
for k, h := range l.Headers {
69+
val := h.Value
70+
if val == "" {
71+
val = string(h.Binary)
72+
}
73+
headers = append(headers, headerIndex{
74+
Key: k,
75+
Value: val,
76+
})
77+
}
78+
m["headers"] = headers
79+
80+
return m
4481
}
4582

4683
func newKafkaLog(record *kafka.Record) *KafkaMessageLog {
@@ -83,13 +120,8 @@ func (l *KafkaRequestLogEvent) Title() string {
83120
return l.Request.Title()
84121
}
85122

86-
func (l *KafkaRequestLogEvent) Metadata() map[string]string {
87-
return nil
88-
}
89-
90123
type KafkaRequest interface {
91124
Title() string
92-
Metadata() map[string]string
93125
}
94126

95127
type KafkaRequestHeader struct {
@@ -120,10 +152,6 @@ func (r *KafkaJoinGroupRequest) Title() string {
120152
return fmt.Sprintf("JoinGroup %s", r.GroupName)
121153
}
122154

123-
func (l *KafkaJoinGroupRequest) Metadata() map[string]string {
124-
return nil
125-
}
126-
127155
type KafkaJoinGroupResponse struct {
128156
KafkaResponseError
129157
GenerationId int32 `json:"generationId"`
@@ -151,10 +179,6 @@ func (r *KafkaSyncGroupRequest) Title() string {
151179
return fmt.Sprintf("SyncGroup %s", r.GroupName)
152180
}
153181

154-
func (r *KafkaSyncGroupRequest) Metadata() map[string]string {
155-
return nil
156-
}
157-
158182
type KafkaSyncGroupResponse struct {
159183
KafkaResponseError
160184
ProtocolType string `json:"protocolType"`
@@ -170,10 +194,6 @@ func (r *KafkaListOffsetsRequest) Title() string {
170194
return "ListOffsets"
171195
}
172196

173-
func (r *KafkaListOffsetsRequest) Metadata() map[string]string {
174-
return nil
175-
}
176-
177197
type KafkaListOffsetsRequestPartition struct {
178198
Partition int `json:"partition"`
179199
Timestamp int64 `json:"timestamp"`
@@ -204,10 +224,6 @@ func (r *KafkaFindCoordinatorRequest) Title() string {
204224
return "FindCoordinator"
205225
}
206226

207-
func (r *KafkaFindCoordinatorRequest) Metadata() map[string]string {
208-
return nil
209-
}
210-
211227
type KafkaFindCoordinatorResponse struct {
212228
KafkaResponseError
213229
Host string `json:"host"`
@@ -226,10 +242,6 @@ func (r *KafkaInitProducerIdRequest) Title() string {
226242
return "InitProducerId"
227243
}
228244

229-
func (r *KafkaInitProducerIdRequest) Metadata() map[string]string {
230-
return nil
231-
}
232-
233245
type KafkaInitProducerIdResponse struct {
234246
KafkaResponseError
235247
ProducerId int64 `json:"producerId"`

providers/directory/log.go

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -314,73 +314,47 @@ func NewCompareLogEvent(req *ldap.CompareRequest, res *ldap.CompareResponse, eh
314314
_ = eh.Push(l, traits.WithNamespace("ldap").With("operation", "compare"))
315315

316316
return l
317+
317318
}
318319

319320
func (l *BindLog) Title() string {
320321
return fmt.Sprintf("%s %s %s", l.Request.Auth, l.Request.Name, l.Request.Password)
321322
}
322323

323-
func (l *BindLog) Metadata() map[string]string {
324-
return nil
325-
}
326-
327324
func (l *UnbindLog) Title() string {
328325
return ""
329326
}
330327

331-
func (l *UnbindLog) Metadata() map[string]string {
332-
return nil
333-
}
334-
335328
func (l *SearchLog) Title() string {
336329
return l.Request.Filter
337330
}
338331

339-
func (l *SearchLog) Metadata() map[string]string {
340-
if l.Request.BaseDN != "" {
341-
return map[string]string{
342-
"baseDn": l.Request.BaseDN,
343-
}
332+
func (l *SearchLog) IndexFields() map[string]any {
333+
m := map[string]any{
334+
"operation": l.Request.Operation,
335+
"request": l.Request,
336+
"response": l.Response,
337+
"metadata.baseDN": l.Request.BaseDN,
344338
}
345-
return nil
339+
return m
346340
}
347341

348342
func (l *CompareLog) Title() string {
349343
return fmt.Sprintf("%s %s", l.Request.Operation, l.Request.Value)
350344
}
351345

352-
func (l *CompareLog) Metadata() map[string]string {
353-
return nil
354-
}
355-
356346
func (l *ModifyLog) Title() string {
357347
return fmt.Sprintf("%s %s", l.Request.Operation, l.Request.Dn)
358348
}
359349

360-
func (l *ModifyLog) Metadata() map[string]string {
361-
return nil
362-
}
363-
364350
func (l *ModifyDNLog) Title() string {
365351
return fmt.Sprintf("%s %s", l.Request.Operation, l.Request.Dn)
366352
}
367353

368-
func (l *ModifyDNLog) Metadata() map[string]string {
369-
return nil
370-
}
371-
372354
func (l *DeleteLog) Title() string {
373355
return fmt.Sprintf("%s %s", l.Request.Operation, l.Request.Dn)
374356
}
375357

376-
func (l *DeleteLog) Metadata() map[string]string {
377-
return nil
378-
}
379-
380358
func (l *AddLog) Title() string {
381359
return fmt.Sprintf("%s %s", l.Request.Operation, l.Request.Dn)
382360
}
383-
384-
func (l *AddLog) Metadata() map[string]string {
385-
return nil
386-
}

providers/mail/log.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ func (l *Log) Title() string {
4646
return fmt.Sprintf("%s", l.Subject)
4747
}
4848

49-
func (l *Log) Metadata() map[string]string {
50-
return nil
49+
func (l *Log) IndexFields() map[string]any {
50+
m := map[string]any{
51+
"from": l.From,
52+
"to": l.To,
53+
"messageId": l.MessageId,
54+
"subject": l.Subject,
55+
}
56+
return m
5157
}

providers/openapi/handler.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,21 @@ func (h *operationHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) *H
276276
m.RequestCounter.WithLabel(h.config.Info.Name, op.Path.Path, r.Method).Add(1)
277277
}
278278

279+
traits := events.NewTraits().WithNamespace("http").WithName(h.config.Info.Name).With("path", op.Path.Path).With("method", r.Method)
279280
if ctx, err := NewLogEventContext(
280281
r,
281282
op.Deprecated,
282-
h.eh,
283-
events.NewTraits().WithName(h.config.Info.Name).With("path", op.Path.Path).With("method", r.Method),
283+
traits,
284284
); err != nil {
285285
log.Errorf("unable to log http event: %v", err)
286286
} else {
287+
defer func() {
288+
l, _ := LogEventFromContext(ctx)
289+
err = h.eh.Push(l, traits)
290+
if err != nil {
291+
log.Errorf("unable to log http event: %v", err)
292+
}
293+
}()
287294
r = r.WithContext(ctx)
288295
}
289296

providers/openapi/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ func TestHandler_Event(t *testing.T) {
11671167

11681168
tc.test(t, func(rw http.ResponseWriter, r *http.Request) {
11691169
h := openapi.NewHandler(config, eh, sm)
1170-
ctx, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits())
1170+
ctx, err := openapi.NewLogEventContext(r, false, events.NewTraits())
11711171
require.NoError(t, err)
11721172
r = r.WithContext(ctx)
11731173
httpErr := h.ServeHTTP(rw, r)

0 commit comments

Comments
 (0)