Use DataStream 2 to deliver JavaScript logs

In this tutorial you'll learn how to use DataStream 2 to deliver JavaScript logs to an external endpoint. JavaScript logging captures log messages generated by your EdgeWorkers functions during the current request.

Before configuring your logs, review the limits for JavaScript logs delivered via DataStream 2.

Before you start

You need EdgeWorkers and DataStream 2 enabled in your contract.

If you are a new EdgeWorkers customer, DataStream 2 is automatically included in your contract.
If you are an existing EdgeWorkers customer, you might need to add DataStream to your contract.

  1. To make sure that both products are present, go to Akamai Control Center.
  2. From the services menu find Account Admin > Contracts.
  3. In the list of products search for EdgeWorkers and then search for DataStream.

Contact your Akamai account representative if you need help or more information.

📘

Data in transit is not localized. For more information, please refer to the Akamai EdgeWorkers and EdgeKV Supplemental Product Policy.

1. Set up a log destination

For this example we'll use an S3-Compatible storage destination for the logs.

Follow the steps in Stream logs to an S3-compatible destination.

📘

There are several different storage options available. To learn more about the other supported log streaming destinations, go to the DataStream 2 guide.

2. Create a DataStream 2 stream

To deliver JavaScripts logs via DataStream 2, you need to create and configure a stream and specify where you would like to deliver the logs.

  1. In Akamai Control Center, go to ☰ > COMMON SERVICES > DataStream.
  2. Select EdgeWorkers from the Create stream button on the top right hand side of the page.

Configure the stream

  1. Enter a Display Name for the stream.
  2. Select the Group and Contract ID you want to use to create the stream and click Next.

Select the log format

  1. Select the format of the log files.

Structured logs let you select whether you want to use a space or a tab delimiter to separate text strings in the logs.

JSON logs stream logs as key-value pairs.

📘

For more information about the log format, go to JavaScript Logging details.

  1. Click the Next button.

Configure the destination for the JavaScript logs

Follow the steps in Stream logs to an S3-compatible destination.

Review the log summary

  1. Review the stream information, then click the Save stream button.
  2. In the pop up window that appears, click the Go to streams button and select EdgeWorkers from the Log type dropdown.
  3. Select your stream and note the stream id.

You can also find the streamid information later by going to DataStream in Control Center. Select EdgeWorkers under Delivery type. You'll see a Stream list that you can search. When you find your stream make sure you note the ID.

3. Create an EdgeWorkers code bundle

For more information see, Create a code bundle.

  1. Import the log module into the main.js file. The log built-in module logs messages generated during the current request.
// Import logging module
import { logger } from 'log';

export function onClientRequest (request) {
  logger.trace("onClientRequest test trace level");
  logger.debug("onClientRequest test  debug level");
  logger.info("onClientRequest test  info level");
  logger.warn("onClientRequest test  warn level");
  logger.error("onClientRequest test  error level");


  request.respondWith(
    200, {},
    '<html><body><h1>Hello World From Akamai EdgeWorkers</h1></body></html>');
}

export function onClientResponse (request, response) {
  logger.trace("onClientResponse test  trace level");
  logger.debug("onClientRequest test  debug level");
  logger.info("onClientResponse test  info level");
  logger.warn("onClientResponse test  warn level");
  logger.error("onClientResponse test  error level");

  response.setHeader('X-Hello-World', 'From Akamai EdgeWorkers');
}
  1. Add the logging parameter to the bundle.json file. You can set the EdgeWorkers log level in the code bundle. By default, EdgeWorkers logs are set to ERROR.

👍

Make sure you change the ds2id to the stream ID you created in step 2 of this tutorial. Ads2id value is required when you add the logging parameter to your code bundle.

{
    "edgeworker-version": "0.3",
    "description" : "Hello World Observability Example",
    "config": {
    "logging": {
        "level":"warn",
        "schema": "v1",
        "ds2id": 12345
    }
  }
}

👍

You can also use EdgeWorkers CLI to change the log level.

4. Create an EdgeWorker ID

  1. Go to the EdgeWorkers Management application and create an EdgeWorker ID.
    Note the EdgeWorker ID number.
  2. Create a version and upload the code bundle you created in the previous step.
  3. Next, Activate the version.

📘

Make sure your ds2id is valid and active before you activate the code bundle. An error will appear if you try to activate a bundle with an inactive or invalid ds2id. For more information see, Error codes - EdgeWorkers Management Application.

📘

For an EdgeWorker to use a Data Stream 2 object, it has to be in the same group or in a sub-group related to the stream object. For more information about the EdgeWorkers Access Model go to, the Identity and Access Management guide.

5. Visit the log destination

To see the JavaScript logs, go to the log destination that you configured in step1. The log lines are typically available after two minutes.

In this example you can find the JavaScript logs by opening the S3-Compatible bucket.

👍

For more information about the logs go to JavaScript Logging details.