Work with notifications

The Registry supports sending webhook notifications in response to events happening within the registry. Notifications are sent in response to manifest pushes and pulls and layer pushes and pulls. These actions are serialized into events. The events are queued into a registry-internal broadcast system which queues and dispatches events to Endpoints.

Workflow of registry notifications

Endpoints

Notifications are sent to endpoints via HTTP requests. Each configured endpoint has isolated queues, retry configuration and http targets within each instance of a registry. When an action happens within the registry, it is converted into an event which is dropped into an inmemory queue. When the event reaches the end of the queue, an http request is made to the endpoint until the request succeeds. The events are sent serially to each endpoint but order is not guaranteed.

Configuration

To set up a registry instance to send notifications to endpoints, one must add them to the configuration. A simple example follows:

  1. notifications:
  2. endpoints:
  3. - name: alistener
  4. url: https://mylistener.example.com/event
  5. headers:
  6. Authorization: [Bearer <your token, if needed>]
  7. timeout: 500ms
  8. threshold: 5
  9. backoff: 1s

The above would configure the registry with an endpoint to send events to https://mylistener.example.com/event, with the header “Authorization: Bearer <your token, if needed>”. The request would timeout after 500 milliseconds. If 5 failures happen consecutively, the registry backs off for 1 second before trying again.

For details on the fields, see the configuration documentation.

A properly configured endpoint should lead to a log message from the registry upon startup:

  1. INFO[0000] configuring endpoint alistener (https://mylistener.example.com/event), timeout=500ms, headers=map[Authorization:[Bearer <your token if needed>]] app.id=812bfeb2-62d6-43cf-b0c6-152f541618a3 environment=development service=registry

Events

Events have a well-defined JSON structure and are sent as the body of notification requests. One or more events are sent in a structure called an envelope. Each event has a unique ID that can be used to uniquely identify incoming requests, if required. Along with that, an action is provided with a target, identifying the object mutated during the event.

The fields available in an event are described below.

FieldTypeDescription
idstringID provides a unique identifier for the event.
timestampTimeTimestamp is the time at which the event occurred.
actionstringAction indicates what action encompasses the provided event.
targetdistribution.DescriptorTarget uniquely describes the target of the event.
lengthintLength in bytes of content. Same as Size field in Descriptor.
repositorystringRepository identifies the named repository.
fromRepositorystringFromRepository identifies the named repository which a blob was mounted from if appropriate.
urlstringURL provides a direct link to the content.
tagstringTag identifies a tag name in tag events.
requestRequestRecordRequest covers the request that generated the event.
actorActorRecord.Actor specifies the agent that initiated the event. For most situations, this could be from the authorization context of the request.
sourceSourceRecordSource identifies the registry node that generated the event. Put differently, while the actor “initiates” the event, the source “generates” it.

The following is an example of a JSON event, sent in response to the pull of a manifest:

  1. {
  2. "events": [
  3. {
  4. "id": "320678d8-ca14-430f-8bb6-4ca139cd83f7",
  5. "timestamp": "2016-03-09T14:44:26.402973972-08:00",
  6. "action": "pull",
  7. "target": {
  8. "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
  9. "digest": "sha256:fea8895f450959fa676bcc1df0611ea93823a735a01205fd8622846041d0c7cf",
  10. "size": 708,
  11. "length": 708,
  12. "repository": "hello-world",
  13. "url": "http://192.168.100.227:5000/v2/hello-world/manifests/sha256:fea8895f450959fa676bcc1df0611ea93823a735a01205fd8622846041d0c7cf",
  14. "tag": "latest"
  15. },
  16. "request": {
  17. "id": "6df24a34-0959-4923-81ca-14f09767db19",
  18. "addr": "192.168.64.11:42961",
  19. "host": "192.168.100.227:5000",
  20. "method": "GET",
  21. "useragent": "curl/7.38.0"
  22. },
  23. "actor": {},
  24. "source": {
  25. "addr": "xtal.local:5000",
  26. "instanceID": "a53db899-3b4b-4a62-a067-8dd013beaca4"
  27. }
  28. }
  29. ]
  30. }

The target struct of events which are sent when manifests and blobs are deleted contains a subset of the data contained in Get and Put events. Specifically, only the digest and repository are sent.

  1. {
  2. "target": {
  3. "digest": "sha256:d89e1bee20d9cb344674e213b581f14fbd8e70274ecf9d10c514bab78a307845",
  4. "repository": "library/test"
  5. }
  6. }

Note: As of version 2.1, the length field for event targets is being deprecated for the size field, bringing the target in line with common nomenclature. Both will continue to be set for the foreseeable future. Newer code should favor size but accept either.

Envelope

The envelope contains one or more events, with the following json structure:

  1. {
  2. "events": [ "..." ]
  3. }

While events may be sent in the same envelope, the set of events within that envelope have no implied relationship. For example, the registry may choose to group unrelated events and send them in the same envelope to reduce the total number of requests.

The full package has the mediatype “application/vnd.docker.distribution.events.v2+json”, which is set on the request coming to an endpoint.

An example of a full event may look as follows:

  1. POST /callback HTTP/1.1
  2. Host: application/vnd.docker.distribution.events.v2+json
  3. Authorization: Bearer <your token, if needed>
  4. Content-Type: application/vnd.docker.distribution.events.v2+json
  5. {
  6. "events": [
  7. {
  8. "id": "asdf-asdf-asdf-asdf-0",
  9. "timestamp": "2006-01-02T15:04:05Z",
  10. "action": "push",
  11. "target": {
  12. "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
  13. "digest": "sha256:fea8895f450959fa676bcc1df0611ea93823a735a01205fd8622846041d0c7cf",
  14. "length": 1,
  15. "repository": "library/test",
  16. "url": "https://example.com/v2/library/test/manifests/sha256:c3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"
  17. },
  18. "request": {
  19. "id": "asdfasdf",
  20. "addr": "client.local",
  21. "host": "registrycluster.local",
  22. "method": "PUT",
  23. "useragent": "test/0.1"
  24. },
  25. "actor": {
  26. "name": "test-actor"
  27. },
  28. "source": {
  29. "addr": "hostname.local:port"
  30. }
  31. },
  32. {
  33. "id": "asdf-asdf-asdf-asdf-1",
  34. "timestamp": "2006-01-02T15:04:05Z",
  35. "action": "push",
  36. "target": {
  37. "mediaType": "application/vnd.docker.container.image.rootfs.diff+x-gtar",
  38. "digest": "sha256:c3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5",
  39. "length": 2,
  40. "repository": "library/test",
  41. "url": "https://example.com/v2/library/test/blobs/sha256:c3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"
  42. },
  43. "request": {
  44. "id": "asdfasdf",
  45. "addr": "client.local",
  46. "host": "registrycluster.local",
  47. "method": "PUT",
  48. "useragent": "test/0.1"
  49. },
  50. "actor": {
  51. "name": "test-actor"
  52. },
  53. "source": {
  54. "addr": "hostname.local:port"
  55. }
  56. },
  57. {
  58. "id": "asdf-asdf-asdf-asdf-2",
  59. "timestamp": "2006-01-02T15:04:05Z",
  60. "action": "push",
  61. "target": {
  62. "mediaType": "application/vnd.docker.container.image.rootfs.diff+x-gtar",
  63. "digest": "sha256:c3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5",
  64. "length": 3,
  65. "repository": "library/test",
  66. "url": "https://example.com/v2/library/test/blobs/sha256:c3b3692957d439ac1928219a83fac91e7bf96c153725526874673ae1f2023f8d5"
  67. },
  68. "request": {
  69. "id": "asdfasdf",
  70. "addr": "client.local",
  71. "host": "registrycluster.local",
  72. "method": "PUT",
  73. "useragent": "test/0.1"
  74. },
  75. "actor": {
  76. "name": "test-actor"
  77. },
  78. "source": {
  79. "addr": "hostname.local:port"
  80. }
  81. }
  82. ]
  83. }

Responses

The registry is fairly accepting of the response codes from endpoints. If an endpoint responds with any 2xx or 3xx response code (after following redirects), the message is considered to have been delivered, and is discarded.

In turn, it is recommended that endpoints are accepting of incoming responses, as well. While the format of event envelopes are standardized by media type, any “pickyness” about validation may cause the queue to backup on the registry.

Monitoring

The state of the endpoints are reported via the debug/vars http interface, usually configured to http://localhost:5001/debug/vars. Information such as configuration and metrics are available by endpoint.

The following provides an example of a few endpoints that have experienced several failures and have since recovered:

  1. {
  2. "notifications": {
  3. "endpoints": [
  4. {
  5. "name": "local-5003",
  6. "url": "http://localhost:5003/callback",
  7. "Headers": {
  8. "Authorization": [
  9. "Bearer \u003can example token\u003e"
  10. ]
  11. },
  12. "Timeout": 1000000000,
  13. "Threshold": 10,
  14. "Backoff": 1000000000,
  15. "Metrics": {
  16. "Pending": 76,
  17. "Events": 76,
  18. "Successes": 0,
  19. "Failures": 0,
  20. "Errors": 46,
  21. "Statuses": {
  22. }
  23. }
  24. },
  25. {
  26. "name": "local-8083",
  27. "url": "http://localhost:8083/callback",
  28. "Headers": null,
  29. "Timeout": 1000000000,
  30. "Threshold": 10,
  31. "Backoff": 1000000000,
  32. "Metrics": {
  33. "Pending": 0,
  34. "Events": 76,
  35. "Successes": 76,
  36. "Failures": 0,
  37. "Errors": 28,
  38. "Statuses": {
  39. "202 Accepted": 76
  40. }
  41. }
  42. }
  43. ]
  44. }
  45. }

If using notification as part of a larger application, it is critical to monitor the size (“Pending” above) of the endpoint queues. If failures or queue sizes are increasing, it can indicate a larger problem.

The logs are also a valuable resource for monitoring problems. A failing endpoint leads to messages similar to the following:

  1. ERRO[0340] retryingsink: error writing events: httpSink{http://localhost:5003/callback}: error posting: Post http://localhost:5003/callback: dial tcp 127.0.0.1:5003: connection refused, retrying
  2. WARN[0340] httpSink{http://localhost:5003/callback} encountered too many errors, backing off

The above indicates that several errors caused a backoff and the registry waits before retrying.

Considerations

Currently, the queues are inmemory, so endpoints should be reasonably reliable. They are designed to make a best-effort to send the messages but if an instance is lost, messages may be dropped. If an endpoint goes down, care should be taken to ensure that the registry instance is not terminated before the endpoint comes back up or messages are lost.

This can be mitigated by running endpoints in close proximity to the registry instances. One could run an endpoint that pages to disk and then forwards a request to provide better durability.

The notification system is designed around a series of interchangeable sinks which can be wired up to achieve interesting behavior. If this system doesn’t provide acceptable guarantees, adding a transactional Sink to the registry is a possibility, although it may have an effect on request service time. See the godoc for more information.