8000 proton/odpf/raccoon at main · irainia/proton · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
 
 

raccoon

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

readme.md

This folder contains the contract between Raccoon and the client.

High level view

The client sends the request in form of bytes serialized EventRequest proto. EventRequest contains repeated Event field. Event is where you put your bytes serialized event. Because the event is serialized as bytes, you can put any kind of event structured in any kind of schema as per your requirement. This makes Raccoon event-agnostic. You can send any form of events. After the request is sent, Raccoon will process it and give back the response in form of EventResponse. The client can handle the response such as retry the request in case of failure. See the illustration below for more clarity.

For details of each field in each proto, you can open the proto files and read the comment.

How to use

Since proton only provides the proto files. You need to compile the proto files yourself according to your language of choice. Then, use the generated code to build the request or parse the response.

Javascript Example

There are 2 packaging options provided by the default protoc to compile the proto to javascript code. For this example we are using commonjs. To compile the proto files you can go to proton root directory and enter:

protoc --proto_path=src --js_out=import_style=commonjs,binary:. odpf/raccoon/Event.proto odpf/raccoon/EventRequest.proto odpf/raccoon/EventResponse.proto

The command will generate the javascript package under odpf/raccoon. You'll see 3 files with _pb.js suffix. You can move those generated codes to your project and import them.

Javascript generated code requires google-protobuf as dependency. Make sure to include it in your package.json.

Now, you got Javascript generated code in your project. You only need to import it and build the request or parse the response. See example below:

const { Timestamp } = require('google-protobuf/google/protobuf/timestamp_pb.js');
const { EventRequest } = require('./EventRequest_pb');
const { Event } = require('./Event_pb');
const { MyClickEvent } = require('./MyClickEvent_pb');

const userClickEvent = new MyClickEvent();
userClickEvent.setUserId('user-12783');
userClickEvent.setEvent('order_clicked');
userClickEvent.setTimestamp(1617092381);

const event = new Event();
event.setEventbytes(userClickEvent.serializeBinary());
event.setType('click_event');

const sentTime = new Timestamp();
sentTime.fromDate(new Date());

const request = new EventRequest();
request.setReqGuid('123123');
request.setSentTime(sentTime);
request.setEventsList([event]);

sendRequestToRaccoon(request.serializeBinary());

You can use the same generated package to deserialize the response from Raccoon.

const { EventResponse } = require('./EventResponse_pb');

const binaryResponse = getResonseFromRaccoon();
const response = new EventResponse();
response.deserializeBinary(binaryResponse);

Raccoon Example

We also compile the proto in GO for Raccoon. You can check how the code is generated by looking at the Makefile. In general, it does the following:

  1. Pull the proto from proton
  2. Generate the GO code and place it internal package
  3. Packages that require the proto can import it from the internal package
0