EXAMPLE #09
PHP
Bash
Fan-out to multiple DataStreams
Post the same event to several streams in one run.
09-multiple-datastreams.php
#!/usr/bin/env php
<?php
/**
* Example: Send events to multiple DataStreams
*
* Demonstrates sending related metrics to different DataStreams using the
* same Token. This is useful when you have a single application reporting
* different categories of events (e.g. performance, errors, usage).
*
* Usage:
* php 09-multiple-datastreams.php
*/
$baseUrl = 'https://www.spaceplace.app';
$tokenValue = 'YOUR-TOKEN-VALUE-HERE';
// Different DataStreams for different metric categories
$streams = [
'performance' => 'YOUR-PERFORMANCE-DATASTREAM-UUID',
'errors' => 'YOUR-ERRORS-DATASTREAM-UUID',
'usage' => 'YOUR-USAGE-DATASTREAM-UUID',
];
/**
* Send an event to a specific DataStream.
*/
function sendTo(string $baseUrl, string $dataStreamId, string $tokenValue, array $event): bool
{
$ch = curl_init("{$baseUrl}/api/events");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
"X-DataStream-ID: {$dataStreamId}",
"X-Token: {$tokenValue}",
],
CURLOPT_POSTFIELDS => json_encode($event),
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode === 201;
}
// ── Simulate collecting and reporting different metric types ─────────────────
// Performance metric
$ok = sendTo($baseUrl, $streams['performance'], $tokenValue, [
'number' => 234,
'source' => 'app-server-01',
'key' => 'response-time-ms',
'value' => '234',
]);
echo "Performance event: " . ($ok ? 'sent' : 'FAILED') . "\n";
// Error metric
$ok = sendTo($baseUrl, $streams['errors'], $tokenValue, [
'number' => 1,
'source' => 'app-server-01',
'key' => 'exception',
'value' => 'PDOException: Connection refused',
]);
echo "Error event: " . ($ok ? 'sent' : 'FAILED') . "\n";
// Usage metric
$ok = sendTo($baseUrl, $streams['usage'], $tokenValue, [
'number' => 47,
'source' => 'app-server-01',
'key' => 'active-users',
'value' => '47',
]);
echo "Usage event: " . ($ok ? 'sent' : 'FAILED') . "\n";
09-multiple-datastreams.sh
#!/usr/bin/env bash
#
# Example: Send events to multiple DataStreams
#
# Demonstrates sending related metrics to different DataStreams using the
# same Token.
#
# Usage:
# bash 09-multiple-datastreams.sh
BASE_URL="https://www.spaceplace.app"
TOKEN_VALUE="YOUR-TOKEN-VALUE-HERE"
DS_PERFORMANCE="YOUR-PERFORMANCE-DATASTREAM-UUID"
DS_ERRORS="YOUR-ERRORS-DATASTREAM-UUID"
DS_USAGE="YOUR-USAGE-DATASTREAM-UUID"
send_to() {
local ds_id="$1"
local payload="$2"
response=$(curl -s -w "\n%{http_code}" \
-X POST "${BASE_URL}/api/events" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "X-DataStream-ID: ${ds_id}" \
-H "X-Token: ${TOKEN_VALUE}" \
-d "$payload")
http_code=$(echo "$response" | tail -n1)
[ "$http_code" = "201" ] && return 0 || return 1
}
# Performance metric
if send_to "$DS_PERFORMANCE" '{"number":234,"source":"app-server-01","key":"response-time-ms","value":"234"}'; then
echo "Performance event: sent"
else
echo "Performance event: FAILED"
fi
# Error metric
if send_to "$DS_ERRORS" '{"number":1,"source":"app-server-01","key":"exception","value":"PDOException: Connection refused"}'; then
echo "Error event: sent"
else
echo "Error event: FAILED"
fi
# Usage metric
if send_to "$DS_USAGE" '{"number":47,"source":"app-server-01","key":"active-users","value":"47"}'; then
echo "Usage event: sent"
else
echo "Usage event: FAILED"
fi