PHP code example of minhyung / openobserve

1. Go to this page and download the library: Download minhyung/openobserve library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

minhyung / openobserve example snippets


use Minhyung\OpenObserve\Client;

$client = new Client(
    baseUrl: 'http://localhost:5080',
    email: '[email protected]',
    password: 'Complexpass#123',
    organization: 'default',
);

$client->logs()->json('app-logs', [
    ['level' => 'info', 'message' => 'hello, world'],
]);

$client = Client::withToken(
    baseUrl: 'http://localhost:5080',
    token: 'YWRtaW46Q29tcGxleHBhc3MjMTIz',
    organization: 'default',
);

// _json: array of records into one stream
$client->logs()->json('app-logs', [
    ['_timestamp' => 1_700_000_000_000_000, 'level' => 'info', 'message' => 'one'],
    ['_timestamp' => 1_700_000_001_000_000, 'level' => 'warn', 'message' => 'two'],
]);

// _multi: same shape but sent as NDJSON
$client->logs()->multi('app-logs', $records);

// _bulk: Elasticsearch-compatible, target many streams at once
$client->logs()->bulk([
    'app-logs' => [['level' => 'info', 'message' => 'hi']],
    'audit'    => [['action' => 'login', 'user' => 'alice']],
]);

$client->otlp()->logs([
    'resourceLogs' => [/* OTLP/JSON */],
], streamName: 'app-logs');

$client->otlp()->metrics(['resourceMetrics' => [/* ... */]]);
$client->otlp()->traces(['resourceSpans' => [/* ... */]]);

$result = $client->search()->sql(
    sql: 'SELECT * FROM "app-logs" WHERE level = \'error\'',
    startTime: 1_700_000_000_000_000, // unix microseconds
    endTime:   1_700_000_060_000_000,
    size: 100,
);

foreach ($result['hits'] ?? [] as $row) {
    // ...
}

use Minhyung\OpenObserve\Resource\Streams;

$client->streams()->list(Streams::TYPE_LOGS, fetchSchema: true);
$client->streams()->get('app-logs');
$client->streams()->updateSettings('app-logs', [
    'data_retention' => 30,
    'full_text_search_keys' => ['set' => ['body']],
]);
$client->streams()->delete('app-logs', Streams::TYPE_LOGS, deleteAll: true);

$client->functions()->create([
    'function' => 'function(row) return row end',
    'order' => 1,
]);

$client->users()->create([
    'email' => '[email protected]',
    'first_name' => 'ming', 'last_name' => 'xing',
    'password' => 'complex#pass',
    'role' => 'admin',
]);

$client->alerts()->setEnabled('high-error-rate', false);
$client->alerts()->trigger('high-error-rate');

$client->dashboards()->list(folder: 'production');

$client->organizations()->list();          // server-global
$client->organizations()->summary('acme'); // org-scoped stats

use Minhyung\OpenObserve\Monolog\Handler;
use Monolog\Level;
use Monolog\Logger;

$logger = new Logger('app');
$logger->pushHandler(new Handler($client, stream: 'app-logs', level: Level::Info));

$logger->info('hello world', ['user_id' => 42]);

$client->request('POST', '/api/default/_settings', ['retention' => 30]);
$client->requestRaw('POST', '/api/upload', "row1\nrow2\n", 'text/csv');

use Minhyung\OpenObserve\Exception\OpenObserveException;

try {
    $client->logs()->json('app-logs', $records);
} catch (OpenObserveException $e) {
    // logging, retry, etc.
}