rudderlabs / community-user-transformations

MIT License
1 stars 1 forks source link

Stream To RabbitMQ #6

Open redsquare opened 1 year ago

redsquare commented 1 year ago

Contact Details

steve.flitcroft@gmail.com

Language

Javascript

Category

Custom Integrations/Other

Description

Stream to RabbitMQ and onward into Clickhouse

Enable the streaming of events from Rudderstack to RabbitMQ using Rabbits http api, only sends events in the VALID_EVENTS array. RabbitMQ is not available in Rudderstack as an in-built destination.

Why

RabbitMQ is useful as the Rudderstack Clickhouse integration is batch only at the moment however Clickhouse has a RabbitMQ streaming engine to enable real-time ingestion.

Code Block

const RABBIT_URL = 'https://{host}/api/exchanges/{vhost}/{queue}/publish';
const VALID_EVENTS = ['search_result', 'advert_view'];
const BASIC_AUTH = 'Basic ZXNiZ2dxdmQ6a3NkVURZRDBBSFYxbjBNTXTwT1dsbEc2dXo4eUJPRlU=';

export async function transformEvent(event, metadata) {

  if (VALID_EVENTS.includes(event.event.toLowerCase()) > -1){
    var body = {
      'vhost': '{vhost}',
      'name': "vehicle_search_events",
      'properties': {
        'delivery_mode': 1,
        'headers': {}
      },
      'routing_key': event.event,
      'delivery_mode': '1',
      'payload': JSON.stringify(event),
      'payload_encoding': 'string',
      'headers': {},
        'props': {}
    };

    const response = await fetch(RABBIT_URL, {
      headers: {
        'Authorization': BASIC_AUTH
      },
      method: 'POST',
      body: JSON.stringify(body)
    });

  }

    return event;
}

Input Payload for testing

[
  {
    "anonymousId": "8d872292709c6fbe",
    "channel": "mobile",
    "context": {
      "app": {
        "build": "1",
        "name": "AMTestProject",
        "namespace": "com.rudderstack.android.rudderstack.sampleAndroidApp",
        "version": "1.0"
      },
      "device": {
        "id": "8d872292709c6fbe",
        "manufacturer": "Google",
        "model": "AOSPonIAEmulator",
        "name": "generic_x86_arm",
        "type": "android"
      },
      "library": {
        "name": "com.rudderstack.android.sdk.core",
        "version": "1.0.2"
      },
      "locale": "en-US",
      "network": {
        "carrier": "Android",
        "bluetooth": false,
        "cellular": true,
        "wifi": true
      },
      "os": {
        "name": "Android",
        "version": "9"
      },
      "screen": {
        "density": 420,
        "height": 1794,
        "width": 1080
      },
      "timezone": "Asia/Kolkata",
      "traits": {
        "address": {
          "city": "Kolkata",
          "country": "India",
          "postalcode": "700096",
          "state": "West bengal",
          "street": "Park Street"
        },
        "age": "30",
        "anonymousId": "8d872292709c6fbe",
        "birthday": "2020-05-26",
        "createdat": "18th March 2020",
        "description": "Premium User for 3 years",
        "email": "identify@test.com",
        "firstname": "John",
        "userId": "sample_user_id",
        "lastname": "Sparrow",
        "name": "John Sparrow",
        "id": "sample_user_id",
        "phone": "9112340345",
        "username": "john_sparrow"
      },
      "userAgent": "Dalvik/2.1.0 (Linux; U; Android 9; AOSP on IA Emulator Build/PSR1.180720.117)"
    },
    "event": "advert_view",
    "integrations": {
      "All": true
    },
    "messageId": "1590431830915-73bed370-5889-436d-9a9e-0c0e0c809d06",
    "properties": {
      "revenue": "30",
      "currency": "USD",
      "quantity": "5",
      "test_key_2": {
        "test_child_key_1": "test_child_value_1"
      },
      "price": "58.0"
    },
    "originalTimestamp": "2020-05-25T18:37:10.917Z",
    "type": "track",
    "userId": "sample_user_id"
  }
]

License