Friday, March 29, 2024
HomeJavaCloudEvent Fundamentals - Java Code Geeks

CloudEvent Fundamentals – Java Code Geeks


CloudEvent is a means of describing occasions in a standard means. This specification is beginning to be adopted throughout completely different occasion producers throughout Cloud Suppliers, which over time will present these advantages:

  • Consistency: The format of an occasion seems the identical no matter the supply producing the occasion, methods which transmit the occasion and methods consuming the occasion. 
  • Tooling: Since there’s a consistency in format, tooling and libraries can rely upon this frequent format

Cloud Occasion Pattern

One of many methods I’ve bought my head round CloudEvent is to have a look at samples. Here’s a pattern Cloud Occasion printed by a
Google Cloud Pub/Sub subject, that is in a json format(there are different codecs to signify a CloudEvent, for eg, avro or protobuf):

{
  "knowledge": {
    "subscription": "initiatives/test-project/subscriptions/my-subscription",
    "message": {
      "attributes": {
        "attr1": "attr1-value"
      },
      "knowledge": "dGVzdCBtZXNzYWdlIDM=",
      "messageId": "message-id",
      "publishTime": "2021-02-05T04:06:14.109Z",
      "orderingKey": "ordering-key"
    }
  },
  "dataContenttype": "utility/json",
  "id": "3103425958877813",
  "supply": "//pubsub.googleapis.com/initiatives/test-project/matters/my-topic",
  "specversion": "1.0",
  "time": "2021-02-05T04:06:14.109Z",
  "sort": "google.cloud.pubsub.subject.v1.messagePublished"
}

A few of the components on this occasion are:

  1. “id” which uniquely identifies the occasion
  2. “supply” which identifies the system producing the occasion
  3. “specversion” identifies the CloudEvent specificiation that this occasion complies with
  4. “sort” defining the kind of occasion produced by the supply system
  5. “dataContenttype” which describes the Content material sort of the info
  6. “knowledge”, which is the precise occasion payload, the construction of this particularly can change based mostly on the “sort” of occasion.

The “id”, “supply”, “specversion” and “sort” fields are necessary

Cloud Occasion Extensions

In sure instances there can be extra attributes that could be wanted to be understood throughout methods which produce and devour messages. A very good instance is distributed tracing the place tracing attributes could should be current in occasion knowledge, to help these instances, occasions can have extension attributes. An instance is the next:

{
  "knowledge": {
    "subscription": "initiatives/test-project/subscriptions/my-subscription",
    "message": {
      "attributes": {
        "attr1": "attr1-value"
      },
      "knowledge": "dGVzdCBtZXNzYWdlIDM=",
      "messageId": "message-id",
      "publishTime": "2021-02-05T04:06:14.109Z",
      "orderingKey": "ordering-key"
    }
  },
  "dataContenttype": "utility/json",
  "id": "3103425958877813",
  "supply": "//pubsub.googleapis.com/initiatives/test-project/matters/my-topic",
  "specversion": "1.0",
  "time": "2021-02-05T04:06:14.109Z",
  "sort": "google.cloud.pubsub.subject.v1.messagePublished",
  "traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
  "tracestate": "rojo=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01,congo=lZWRzIHRoNhcm5hbCBwbGVhc3VyZS4"
}

the place “traceparent” and “tracestate” seize the distribution tracing associated attributes. A few of the different extension sorts are documented
right here.

Knowledge Attribute

The occasion payload is contained within the “knowledge” attribute (or might be base 64 encoded right into a “data_base64” attribute). The construction of the info attribute is totally depends upon the occasion sort. There’s a stage of specification that may be specified by the occasion sort utilizing an extra attribute known as “dataschema”.

Think about one other pattern for a log entry knowledge associated occasion in Google Cloud:

{
  "knowledge": {
    "insertId": "1234567",
    "logName": "initiatives/test-project/logs/cloudaudit.googleapis.compercent2Fdata_access",
    "protoPayload": {
      "authenticationInfo": {
        "principalEmail": "robotic@test-project.iam.gserviceaccount.com"
      },
      "methodName": "jobservice.jobcompleted",
      "requestMetadata": {
        "callerIp": "2620:15c:0:200:1a75:e914:115b:e970",
        "callerSuppliedUserAgent": "google-cloud-sdk357.0.0 (gzip),gzip(gfe)",
        "destinationAttributes": {
          
        },
        "requestAttributes": {
          
        }
      },
      "resourceName": "initiatives/test-project/jobs/sample-job",
      "serviceData": {
        "jobCompletedEvent": {
          "eventName": "query_job_completed",
          "job": {
            "jobConfiguration": {
              "question": {
                "createDisposition": "CREATE_IF_NEEDED",
                "defaultDataset": {
                  
                },
                "destinationTable": {
                  "datasetId": "sample-dataset",
                  "projectId": "test-project",
                  "tableId": "sample-table"
                },
                "question": "sample-query",
                "queryPriority": "QUERY_INTERACTIVE",
                "statementType": "SELECT",
                "writeDisposition": "WRITE_TRUNCATE"
              }
            }
          }
        }
      },
      "serviceName": "bigquery.googleapis.com",
      "standing": {
        
      }
    },
    "receiveTimestamp": "2021-11-25T21:56:00.653866570Z",
    "useful resource": {
      "labels": {
        "project_id": "test-project"
      },
      "sort": "bigquery_resource"
    },
    "severity": "INFO",
    "timestamp": "2021-11-25T21:56:00.276607Z"
  },
  "dataContenttype": "utility/json; charset=utf-8",
  "dataschema": "https://googleapis.github.io/google-cloudevents/jsonschema/google/occasions/cloud/audit/v1/LogEntryData.json",
  "id": "initiatives/test-project/logs/cloudaudit.googleapis.compercent2Fdata_access1234567123456789",
  "methodName": "jobservice.jobcompleted",
  "recordedTime": "2021-11-25T21:56:00.276607Z",
  "resourceName": "initiatives/test-project/jobs/sample-job",
  "serviceName": "bigquery.googleapis.com",
  "supply": "//cloudaudit.googleapis.com/initiatives/test-project/logs/data_access",
  "specversion": "1.0",
  "topic": "bigquery.googleapis.com/initiatives/test-project/jobs/sample-job",
  "time": "2021-11-25T21:56:00.653866570Z",
  "sort": "google.cloud.audit.log.v1.written"
}

The “knowledge” area is pretty sophisticated right here, nevertheless see how there’s a reference to a “dataschema” pointing to this doc —
https://googleapis.github.io/google-cloudevents/jsonschema/google/occasions/cloud/audit/v1/LogEntryData.json

which describes the weather within the “knowledge”, utilizing json schema specification

Conclusion

CloudEvents makes an attempt to unravel the problem of various occasion sources utilizing alternative ways to signify an occasion, by offering a standard specification.

This weblog submit offers a fast overview of the specification, in a future submit I’ll go over how that is helpful for writing eventing methods on Google Cloud.

Revealed on Java Code Geeks with permission by Biju Kunjummen, associate at our JCG program. See the unique article right here: CloudEvent Fundamentals

Opinions expressed by Java Code Geeks contributors are their very own.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments