Skip to content

Adds V1 event support#25

Merged
rafusel merged 20 commits intomainfrom
add-v1-event-support
Jul 12, 2021
Merged

Adds V1 event support#25
rafusel merged 20 commits intomainfrom
add-v1-event-support

Conversation

@rafusel
Copy link
Copy Markdown
Contributor

@rafusel rafusel commented Jun 29, 2021

Description

This PR adds V1 events API support to the agent using pdagent send, which previously sent a V2 event using backwards compatible flags.

Changes

  • adds 2 new methods to the Event interface, Version() and AddCustomDetail(k string, v interface{})
  • AddCustomDetail allows cmdutil.RunSendCommand to insert custom details without knowing the specific fields custom details resides in V1 and V2 events
  • adds a new EventContainer that implements an UnmarshalEvent method which returns an Event, the event must be stored in EventContainer so that the persistent queue can store both V1 and V2 events
  • adds a new EventVersion type
  • EventContainer has fields EventVersion (which is the event version as the EventVersion type) and EventData (which holds the event's data in a json.RawMessage)
  • passes the event version as a header from the client to the server on a send request, using the Version method in the Event interface
  • commands like send and enqueue construct version specific events, and when they get sent to the server they are stored in the EventVersion type
  • the agent then uses EventContainer throughout, until a request must be made to PagerDuty, which then unmarshals the Event and gets the underlying EventV1 or EventV2 type

@rafusel rafusel force-pushed the add-v1-event-support branch from 14150f9 to f265ad5 Compare June 30, 2021 07:24
@rafusel rafusel marked this pull request as ready for review June 30, 2021 16:32
@rafusel rafusel force-pushed the add-v1-event-support branch from f265ad5 to 3789cda Compare July 6, 2021 15:40
Comment thread pkg/client/client.go Outdated
Comment thread pkg/eventsapi/event_versions.go Outdated
Comment thread pkg/eventsapi/generic_event.go Outdated
Comment thread pkg/eventsapi/generic_event.go Outdated
Copy link
Copy Markdown

@ChezCrawford ChezCrawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking better! Left a few comments.

Comment thread pkg/eventsapi/v2.go
}

func (e EventV2) GetRoutingKey() string {
func (e *EventV2) GetRoutingKey() string {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason we made these pointer receivers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought process was this: AddCustomDetail needs to be passed a pointer reference because it's mutating one of the fields in the underlying struct (which I think is correct). My two options were to either make AddCustomDetail return a new event and keep the data immutable, which I didn't like as much as passing the pointer reference, and then from there I changed the rest of of the methods to take a pointer reference for consistency.

Comment thread pkg/eventsapi/event_versions.go
Comment thread test/helpers.go Outdated
Comment thread test/helpers.go
Comment thread pkg/eventsapi/event_container.go Outdated
Comment thread pkg/eventsapi/event_container.go Outdated
Comment thread pkg/eventsapi/event_container.go Outdated
Comment thread test/helpers.go
EventVersion: eventsapi.EventVersion2,
}

eventV2 := eventsapi.EventV2{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how much this is used by we might consider making a constructor inside eventsapi like so:

func NewEventContainer(event Event) *EventContainer {
	jsonEvent, _ := json.Marshal(event)

	return &EventContainer{
		EventVersion: event.Version(),
		EventData:    jsonEvent,
	}
}

if we're ever actually doing this in production code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of right now this is only done here in the test helper, should I write the constructor anyways or leave as is?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries for now.

Comment thread cmd/send.go Outdated
cmd.Flags().StringVarP(&sendEvent.ClientURL, "client-url", "u", "", "Client URL")
cmd.Flags().StringToStringVarP(&customDetails, "field", "f", map[string]string{}, "Add given KEY=VALUE pair to the event details")

cmd.MarkFlagRequired("routing-key")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be service-key right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Fixed here: 90731c3

Comment thread cmd/send.go Outdated
Long: `Queue up a trigger, acknowledge, or resolve V1 event to PagerDuty
using a backwards-compatible set of flags.

Required flags: "routing-key", "event-type"`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service-key here as well.

Copy link
Copy Markdown

@ChezCrawford ChezCrawford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work @rafusel . Take a look at the final comments in cmd/send.go but this should be good to go after addressing those.

@rafusel rafusel merged commit 3e2e587 into main Jul 12, 2021
@rafusel rafusel deleted the add-v1-event-support branch July 12, 2021 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants