-
Notifications
You must be signed in to change notification settings - Fork 6
Support for creating Custom Event Types with schemas validation #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rjalander
wants to merge
26
commits into
cdevents:main
Choose a base branch
from
Nordix:custom_cdevents_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
ddd02fb
updating spec gitmodule to v0.4.0
0b345f8
Generate SDK with spec 0.4.1
5a2f5fc
Add module preprocessor and fix linter issues
0d0c1c0
preprocessor fix linter issues
0f591e6
adding preprocessor package-info
e32f8c5
fixing review comments
51b3ffc
Update README and cleanup
88d3bdd
Update with review comments
bbd4279
Update PreprocessSchemas with interface implementation
5309a0f
Fix PreprocessSchemas linting issues
03f986b
Update README to locate PreprocessSchemas
9ff4519
adding custom event APIs support and validation
b2c37fd
correcting setChainId method argument
aab785a
merge from main branch
67d2b79
fix linter issues
0bb99f2
fix linter issues
65856c9
addressing review comments
220cb9f
creating a baseEvent for custom events
cadb9a7
fix linter issues
d737489
fix linter and add isCustomEvent method
c8ffb25
removing unused file
6ce4bad
fix linter issues in SchemaData.java
495c0b3
fix linter issues in SchemaData.java
32846cc
adding doc CUSTOM_CDEVENTS
dff52e9
update with review comments
2227b6e
updating isCustomEvent check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Custom CDEvents | ||
If a tool wants to emit events that are not supported by the CDEvents specification, | ||
they can do so via [custom events](https://github.com/cdevents/spec/tree/main/custom). | ||
|
||
Custom events follow the CDEvents format and can be defined via the | ||
`CustomTypeEvent` class, available since v0.4. | ||
|
||
Let's consider the following scenario: a tool called "MyRegistry" has a concept of "Quota" | ||
which can be "exceeded" by users of the system. We want to use events to notify when that | ||
happens, but CDEvents does not define any quota related subject. | ||
|
||
## Steps involved to create a custom CDEvent | ||
|
||
### Add SDK dependency to your project | ||
|
||
```xml | ||
<dependency> | ||
<groupId>dev.cdevents</groupId> | ||
<artifactId>cdevents-sdk-java</artifactId> | ||
<version>${cdevents.version}</version> | ||
</dependency> | ||
``` | ||
### Create a Custom CDEvent | ||
In this example, we will create a custom event for our tool utilizing the new `CustomTypeEvent` | ||
|
||
```java | ||
public class QuotaExceededCustomEvent { | ||
|
||
public static void main(String[] args) { | ||
|
||
CustomTypeEvent cdEvent = new CustomTypeEvent(); | ||
// Set the event type in the format dev.cdeventsx.<tool-name>-<subject-name>.<predicate-name>.<major.minor.patch> | ||
cdEvent.setType("dev.cdeventsx.myregistry-quota.exceeded.0.1.0"); | ||
|
||
// Set the required context fields | ||
cdEvent.setSource(URI.create("http://myregistry/region/staging")); | ||
cdEvent.setSubjectId("quotaRule123"); | ||
|
||
// Set the subject type in the format <tool-name>-<subject-name> | ||
cdEvent.setSubjectType("myregistry-quota"); | ||
|
||
// Define a map with the content properties | ||
Map<String, Object> contentQuota = new HashMap<>(); | ||
contentQuota.put("user", "heavy_user"); | ||
contentQuota.put("limit", "50Tb"); | ||
contentQuota.put("current", 90); | ||
contentQuota.put("threshold", 85); | ||
contentQuota.put("level", "WARNING"); | ||
|
||
// Set the required subject content | ||
cdEvent.setSubjectContentProperty(contentQuota); | ||
|
||
// If we host a schema for the overall custom CDEvent, we can add it | ||
// to the event so that the receiver may validate custom fields like | ||
// the event type and subject content | ||
cdEvent.setContextSchemaUri(URI.create("https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0")); | ||
|
||
// Create event as JSON to print | ||
String eventJson = CDEvents.cdEventAsJson(cdEvent); | ||
System.out.println(eventJson); | ||
|
||
// Create event as CloudEvent, this validates event against official spec/custom/schema.json | ||
CloudEvent ceEvent = CDEvents.cdEventAsCloudEvent(cdEvent); | ||
// This ceEvent can be sent using HTTP Protocol Binding | ||
// Refer : https://cloudevents.github.io/sdk-java/http-basic.html | ||
} | ||
} | ||
|
||
``` | ||
The resulting CDEvents JSON will look like: | ||
|
||
````json | ||
{"context":{"version":"0.4.1","id":"587b646c-5dd5-4347-aa70-7a624a05120c","source":"http://myregistry/region/staging","type":"dev.cdeventsx.myregistry-quota.exceeded.0.1.0","timestamp":"2024-07-16T16:00:28Z","schemaUri":"https://myregistry.dev/schemas/cdevents/quota-exceeded/0_1_0","links":[]},"subject":{"id":"quotaRule123","type":"myregistry-quota","content":{"current":90,"level":"WARNING","limit":"50Tb","threshold":85,"user":"heavy_user"}},"customData":{},"customDataContentType":"application/json"} | ||
|
||
```` | ||
|
||
The test code is available at [QuotaExceededCustomEvent.java](../sdk/src/test/java/dev/cdevents/QuotaExceededCustomEvent.java) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.