Skip to content

Commit

Permalink
feat: add WebhookSucceeded event
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Oct 23, 2023
1 parent 9c8a25e commit aa8c936
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions selfservice/hook/web_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ func (e *WebHook) execute(ctx context.Context, data *templateContext) error {
}
} else {
logger.Info("Webhook request succeeded")
if emitEvent {
span.AddEvent(events.NewWebhookSucceeded(ctx))
}
}
}(time.Now())

Expand Down
17 changes: 16 additions & 1 deletion selfservice/hook/web_hook_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ func TestWebhookEvents(t *testing.T) {
}))
t.Cleanup(webhookReceiver.Close)

t.Run("sucess", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
wh := hook.NewWebHook(&whDeps, json.RawMessage(fmt.Sprintf(`
{
"url": %q,
Expand Down Expand Up @@ -1195,6 +1195,11 @@ func TestWebhookEvents(t *testing.T) {
})
require.GreaterOrEqual(t, i, 0)

i = slices.IndexFunc(events, func(ev sdktrace.Event) bool {
return ev.Name == "WebhookSucceeded"
})
require.GreaterOrEqual(t, i, 0)

i = slices.IndexFunc(events, func(ev sdktrace.Event) bool {
return ev.Name == "WebhookFailed"
})
Expand Down Expand Up @@ -1239,6 +1244,11 @@ func TestWebhookEvents(t *testing.T) {
return ev.Name == "WebhookFailed"
})
require.GreaterOrEqual(t, i, 0)

i = slices.IndexFunc(events, func(ev sdktrace.Event) bool {
return ev.Name == "WebhookSucceeded"
})
require.Equal(t, i, -1)
})

t.Run("event disabled", func(t *testing.T) {
Expand Down Expand Up @@ -1280,5 +1290,10 @@ func TestWebhookEvents(t *testing.T) {
return ev.Name == "WebhookFailed"
})
require.Equal(t, -1, i)

i = slices.IndexFunc(events, func(ev sdktrace.Event) bool {
return ev.Name == "WebhookSucceeded"
})
require.Equal(t, i, -1)
})
}
6 changes: 6 additions & 0 deletions x/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
IdentityCreated semconv.Event = "IdentityCreated"
IdentityUpdated semconv.Event = "IdentityUpdated"
WebhookDelivered semconv.Event = "WebhookDelivered"
WebhookSucceeded semconv.Event = "WebhookSucceeded"
WebhookFailed semconv.Event = "WebhookFailed"
)

Expand Down Expand Up @@ -312,6 +313,11 @@ func NewWebhookDelivered(ctx context.Context, URL *url.URL, reqBody []byte, stat
)
}

func NewWebhookSucceeded(ctx context.Context) (string, trace.EventOption) {
return WebhookSucceeded.String(),
trace.WithAttributes(semconv.AttributesFromContext(ctx)...)
}

func NewWebhookFailed(ctx context.Context, err error) (string, trace.EventOption) {
return WebhookFailed.String(),
trace.WithAttributes(
Expand Down

0 comments on commit aa8c936

Please sign in to comment.