Skip to content

Commit

Permalink
SDK updates
Browse files Browse the repository at this point in the history
  • Loading branch information
“nico-shishkin” committed Sep 25, 2024
1 parent 50cd26e commit 174ae1b
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 1 deletion.
111 changes: 110 additions & 1 deletion docs/shipping/Code/node-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ tsc --project tsconfig.json
```

</TabItem>
<TabItem value="OpenTelemetry" label="OpenTelemetry">
<TabItem value="OpenTelemetry" label="OpenTelemetry JavaScript">

This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

Expand Down Expand Up @@ -417,6 +417,115 @@ Allow some time for data ingestion, then open [Open Search Dashboards](https://a
Encounter an issue? See our [log shipping troubleshooting](https://docs.logz.io/docs/user-guide/log-management/troubleshooting/log-shipping-troubleshooting/) guide.


</TabItem>
<TabItem value="OpenTelemetry" label="OpenTelemetry Typescript">

This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

### Prerequisites

- Node
- A Node application
- An active account with Logz.io



### Configure the instrumentation


1. Install the dependencies:

```shell
npm install --save @opentelemetry/api-logs
npm install --save @opentelemetry/sdk-logs
npm install --save @opentelemetry/exporter-logs-otlp-proto
npm install --save @opentelemetry/resources
```

2. Configure the Opentelemetry Collector. Create a `logger.ts` file in your project (or add it to your existing configuration) with the following configuration:



```typescript
import { LoggerProvider, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-proto';
import { Resource } from '@opentelemetry/resources';

const resource = new Resource({ 'service.name': 'YOUR-SERVICE-NAME' });
const loggerProvider = new LoggerProvider({ resource });

const otlpExporter = new OTLPLogExporter({
url: 'https://otlp-listener.logz.io/v1/logs',
headers: {
Authorization: 'Bearer <LOG-SHIPPING-TOKEN>',
'user-agent': 'logzio-nodejs-logs-otlp'
}
});

loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(otlpExporter));

const logger = loggerProvider.getLogger('example_logger');
export { logger };
```

Replace `YOUR-SERVICE-NAME` with the required service name.


{@include: ../../_include/log-shipping/log-shipping-token.md}


Update the `listener.logz.io` part in `https://otlp-listener.logz.io/v1/logs` with the URL for [your hosting region](https://docs.logz.io/docs/user-guide/admin/hosting-regions/account-region).


:::note
If your Logz.io account region is not `us-east-1`, add your [region code](https://docs.logz.io/docs/user-guide/admin/hosting-regions/ account-region/#available-regions) to the `url` like so `https://otlp-listener-<<REGION-CODE>>.logz.io/v1/logs`.
:::

3. Once you have configured the logger, import it into your application (e.g., `server.ts`) and start logging.

Example of logging from your `server.ts` file:

```typescript
import express, { Request, Response } from 'express';
import { logger } from './logger/logger'; // Assuming logger.ts is in the logger folder

const app = express();
const PORT = process.env.PORT || 8080;

app.get('/rolldice', (req: Request, res: Response) => {
logger.emit({
body: 'Received a request to roll the dice',
attributes: { severityText: 'info' },
});

const rolls = parseInt(req.query.rolls as string) || 1; // Default to 1 roll if not specified
const result = [Math.floor(Math.random() * 6) + 1]; // Example dice roll

logger.emit({
body: `Rolled dice result: ${result}`,
attributes: { severityText: 'info' },
});

res.send(JSON.stringify(result));
});

app.listen(PORT, () => {
logger.emit({
body: `Server is running on http://localhost:${PORT}`,
attributes: { severityText: 'info' },
});
});
```


### Check Logz.io for your logs


Allow some time for data ingestion, then open [Open Search Dashboards](https://app.logz.io/#/dashboard/osd).

Encounter an issue? See our [log shipping troubleshooting](https://docs.logz.io/docs/user-guide/log-management/troubleshooting/log-shipping-troubleshooting/) guide.


</TabItem>
</Tabs>

Expand Down
122 changes: 122 additions & 0 deletions docs/shipping/Code/php.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
id: php
title: PHP
overview: Deploy this integration to enable automatic instrumentation of your PHP application using OpenTelemetry.
product: ['logs']
os: ['windows', 'linux']
filters: ['Code']
logo: https://logzbucket.s3.eu-west-1.amazonaws.com/logz-docs/shipper-logos/php.svg
logs_dashboards: []
logs_alerts: []
logs2metrics: []
metrics_dashboards: []
metrics_alerts: []
drop_filter: []
---

This integration uses the OpenTelemetry logging exporter to send logs to Logz.io via the OpenTelemetry Protocol (OTLP) listener.

### Prerequisites

- PHP 7.4+ for manual instrumentation or PHP 8.0+ for auto-instrumentation.

:::note
If you need an example aplication to test this integration, please refer to our [PHP OpenTelemetry repository](https://github.com/logzio/opentelemetry-examples/tree/main/php/logs).
:::

### Configure the instrumentation

#### Install the necessary extensions and dependencies

To enable OpenTelemetry logging in PHP, follow these steps:

1. Install the OpenTelemetry PHP extension using PECL:

```bash
pecl install opentelemetry
```

2. Add the OpenTelemetry extension to your `php.ini` file:

```ini
[opentelemetry]
extension=opentelemetry.so
```

3. Install the necessary Composer packages:

```bash
composer require \
monolog/monolog \
open-telemetry/opentelemetry-logger-monolog
```

4. Set OpenTelemetry environment variables:

Add the following OpenTelemetry environment variables in your PHP environment or programmatically in your PHP code:

```php
// Set OpenTelemetry environment variables programmatically
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
putenv('OTEL_LOGS_EXPORTER=otlp');
putenv('OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=http/protobuf');
putenv('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://otlp-listener.logz.io/v1/logs');
putenv('OTEL_EXPORTER_OTLP_LOGS_HEADERS=Authorization=Bearer <<LOG-SHIPPING-TOKEN>>,user-agent=logzio-php-logs-otlp');
putenv('OTEL_RESOURCE_ATTRIBUTES=service.name=<YOUR-SERVICE-NAME>');
```

Replace `<YOUR-SERVICE-NAME>` with the required service name.

{@include: ../../_include/log-shipping/log-shipping-token.md}

5. Logging integration with Monolog:

After setting up the environment variables, you can integrate OpenTelemetry with Monolog for logging. Below is an example that demonstrates how to configure and use Monolog with OpenTelemetry:

```php
<?php

use Monolog\Logger;
use OpenTelemetry\Contrib\Logs\Monolog\Handler as MonologHandler;
use OpenTelemetry\SDK\Logs\LoggerProviderFactory;
use Psr\Log\LogLevel;

require __DIR__ . '/vendor/autoload.php';

// Initialize LoggerProviderFactory
$loggerFactory = new LoggerProviderFactory();
$loggerProvider = $loggerFactory->create();
$handler = new MonologHandler(
$loggerProvider,
LogLevel::DEBUG,
);

// Initialize Monolog
$monolog = new Logger('otel-logger', [$handler]);

// Example logging with Monolog
$monolog->info('This is an informational log message');
$monolog->error('This is an error log message');

// Shutdown the logger provider after logging
$loggerProvider->shutdown();
```

6. Run the application:

You can now run the PHP application and generate logs:

```bash
php your_script.php
```

Replace your_script.php with the name of your PHP script. This will send the logs to Logz.io via the OpenTelemetry OTLP endpoint.


### Check Logz.io for your logs


Allow some time for data ingestion, then open [Open Search Dashboards](https://app.logz.io/#/dashboard/osd).

Encounter an issue? See our [log shipping troubleshooting](https://docs.logz.io/docs/user-guide/log-management/troubleshooting/log-shipping-troubleshooting/) guide.

0 comments on commit 174ae1b

Please sign in to comment.