Resources

Using OpenTelemetry for JavaScript Development

September 30, 2024 by OpenObserve Team
javascript opentelemetry

Having logs in a structured format like JSON can make a world of difference when it comes to monitoring and observability. JSON is not just human-readable; it's also incredibly versatile, allowing easy parsing and analysis across various tools. 

In this guide, we'll explore how to configure your OpenTelemetry Collector to output logs in JSON format, streamlining your log management process and making data easier to work with.

Whether you’re managing complex microservices or looking to streamline your log analysis, configuring OpenTelemetry JSON logs can significantly enhance your workflow. This guide will walk you through the steps to format your logs in JSON, provide configuration examples, and demonstrate how tools like OpenObserve can further elevate your monitoring capabilities by efficiently storing and visualizing these JSON-formatted logs.

Configure OpenTelemetry Collector for JSON Logs

A highly effective approach to manage and analyze your telemetry data is to configure the OpenTelemetry Collector to output logs in JSON format. Utilizing the opentelemetry json format not only standardizes your logs but also enhances their processability and visualization across different platforms, particularly when integrating with tools like OpenObserve. JSON’s structured format simplifies parsing, indexing, and querying, making your telemetry data more accessible and actionable.

Step 1: Set Encoding to JSON

The first step in this process is to configure the telemetry logs to use JSON encoding. 

This ensures that all logs generated by the OpenTelemetry Collector are structured in a consistent, easy-to-parse format.

Step 2: Configuration Snippet

Here’s a simple configuration snippet that you can use to set the log encoding to JSON:

receivers:
  otlp:
    protocols:
      grpc:

exporters:
  logging:
    loglevel: debug
    encoding: json

service:
  pipelines:
    logs:
      receivers: \[otlp]
      exporters: \[logging]

In this configuration:

  • Receivers define the input sources.
  • Exporters determine how and where the logs are sent out.
  • The encoding: json setting ensures that the logs are output in JSON format.

Step 3: Reference Documentation

For more advanced configurations and a deeper understanding of all available options, refer to the official OpenTelemetry documentation

This will help you tailor the setup to meet your specific needs.

Integrating OpenTelemetry JSON Logs with OpenObserve

OpenObserve can revolutionize how you handle JSON-formatted logs. By using It as the backend for your OpenTelemetry setup, you gain access to powerful storage, querying, and visualization capabilities. 

The straightforward integration can significantly enhance your ability to monitor and analyze your telemetry data. Explore more on our website or dive into our GitHub repository for detailed insights and resources.

With OpenObserve, the JSON logs you configure in OpenTelemetry can be stored efficiently, indexed for fast retrieval, and visualized in customizable dashboards. This integration offers more profound insights and comprehensive analysis, making your observability stack more robust.

Next, let’s look at an example configuration to see this setup in action.

Read more on How OpenTelemetry Works and Its Use Cases

Example Configuration

Setting up the OpenTelemetry Collector to process logs in the JSON format requires careful configuration of each component to prevent data loss or misconfiguration. Properly implementing the OpenTelemetry JSON settings is key to maintaining the integrity and usability of your telemetry data.

Below, we’ll walk through a basic configuration that defines receivers, sets up exporters with appropriate log-level settings, and configures service pipelines to handle both metrics and traces. 

We’ll also cover how to configure OpenObserve as a destination for these logs.

Step 1: Define Receivers

First, you need to define the receivers in your configuration. Receivers are responsible for receiving data from different sources, such as applications or other telemetry sources. Here’s an example using the OpenTelemetry Protocol (OTLP) with an endpoint:

receivers:
  otlp:
    protocols:
      grpc:
      http:

This configuration enables both gRPC and HTTP protocols for the OTLP receiver.

Step 2: Set Up Exporters with Log-Level Settings

Next, configure the exporters. Exporters determine where the collected telemetry data will be sent. In this case, you will set up the exporter to send logs to OpenObserve and ensure that logs are formatted as JSON:

exporters:
  logging:
    loglevel: debug
    encoding: json
 
  openobserve:
    endpoint: "https://your-openobserve-endpoint/api/v1/logs"
    api_key: "your-api-key"
    encoding: json

Here, we’ve added an exporter specifically for OpenObserve, ensuring that logs are encoded in JSON format.

Step 3: Configure Service Pipelines

Service pipelines define the flow of data from receivers to exporters. You’ll want to set up separate pipelines for metrics and traces:

service:
  pipelines:
    logs:
      receivers: \[otlp]
      exporters: [logging, openobserve]
    metrics:
      receivers: \[otlp]
      exporters: \[openobserve]
    traces:
      receivers: \[otlp]
      exporters: \[openobserve]

This configuration ensures that logs, metrics, and traces are all handled appropriately and sent to OpenObserve.

Step 4: Specify Telemetry Logs Encoding as JSON

To ensure that your logs are encoded in JSON, make sure the encoding: json setting is present in your exporter configuration, as shown in the previous step.

Step 5: Running the Configuration

Once your configuration file is ready, you can run the OpenTelemetry Collector with the following command:

otelcol --config=config.yaml

This command starts the OpenTelemetry Collector using your configuration file.

After configuring and running the OpenTelemetry Collector, it's crucial to ensure that your JSON logs are being correctly ingested by OpenObserve. With OpenObserve set as your destination, these logs will be stored, indexed, and accessible via OpenObserve’s intuitive dashboard. 

Here, you can query, visualize, and analyze the logs in real-time, making it easy to spot any issues in the logging pipeline and monitor system performance.

This integration allows you to utilize OpenObserve’s advanced querying features to validate that the log data is accurately formatted in JSON. Whether through real-time dashboards or custom queries, OpenObserve provides immediate insights into your system’s health, ensuring that your observability stack is functioning optimally. 

Explore more on our website or dive into our GitHub repository for detailed insights and resources.

By integrating OpenTelemetry with OpenObserve, you’re enhancing your observability stack, making it easier to capture, store, and analyze telemetry data in JSON format.

Read more on Unifying Observability and Troubleshooting: The Power of Observability Dashboards

Sample Output

One key outcome of working with the OpenTelemetry Collector is generating logs in JSON format. This format is handy because it structures data in a way that is both machine-readable and easy to parse. 

Below, we'll examine the typical format of these output logs and provide some sample JSON entries.

Format of JSON Output Logs

The logs generated by OpenTelemetry in JSON format include key-value pairs that represent various attributes of the telemetry data, such as timestamps, log levels, and specific events. 

Here’s a simple example of what a JSON-formatted log might look like:

{
  "timestamp": "2024-08-30T14:23:45Z",
  "logLevel": "INFO",
  "serviceName": "example-service",
  "message": "User login successful",
  "attributes": {
    "userId": "12345",
    "region": "us-west-2"
  }
}

This structure ensures that each log entry contains all the necessary information, making it easy to filter, search, and analyze logs based on various criteria.

Sample Log Entries in JSON Format

Here’s another example of a more detailed log entry:

{
  "timestamp": "2024-08-30T15:35:21Z",
  "logLevel": "ERROR",
  "serviceName": "auth-service",
  "message": "Failed to authenticate user",
  "attributes": {
    "userId": "67890",
    "error": "Invalid credentials",
    "region": "eu-central-1"
  }
}

This example highlights how JSON logs can capture not only the occurrence of an event but also additional contextual data, such as user information and the specific error encountered.

Viewing Logs in OpenObserve

To maximize the use of these JSON logs, OpenObserve provides an intuitive interface for viewing and managing them. 

Integrating OpenTelemetry with OpenObserve allows you to easily ingest and visualize these logs in a structured and searchable format. OpenObserve’s dashboard allows you to filter logs based on any attribute, enabling rapid troubleshooting and deeper analysis.

Explore more on our website or dive into our GitHub repository for detailed insights and resources.

Use OpenObserve’s real-time dashboard and advanced querying features to validate your JSON logs. This approach ensures that your logs are not only correctly formatted but also easily accessible for operational and debugging purposes.

Read more on Top 10 Observability Tools for 2024

Common Settings and Considerations

When setting up the OpenTelemetry Collector to output logs in JSON format, it's crucial to understand the common settings and considerations involved in the OpenTelemetry JSON configuration.

Deprecated Components and Settings

Certain components and settings within OpenTelemetry may become deprecated over time. These deprecated elements can create challenges when setting up your telemetry pipeline, especially if you rely on older configurations that may no longer be supported.

For example, if you’re using outdated log exporters or receivers, you might encounter compatibility issues that could disrupt your telemetry data flow. You should regularly check the OpenTelemetry documentation for any deprecated features and update your configurations accordingly.

Recommendation: OpenObserve is well-equipped to handle JSON-formatted logs, even when dealing with deprecated components. By utilizing OpenObserve’s robust support for JSON logs, you can mitigate potential issues that arise from outdated settings. 

It’s advisable to configure OpenTelemetry in a way that maximizes compatibility with OpenObserve, ensuring a smooth transition as you update your telemetry stack.

Default Settings and Their Implications

OpenTelemetry has default settings that govern how logs are collected, processed, and exported. While these defaults are designed to work out of the box, they may not always align with your specific needs, mainly when dealing with JSON-formatted logs.

For instance, the default log encoding might not be set to JSON, which could require additional configuration steps. Moreover, default buffer sizes and retry mechanisms may impact the performance of your logging pipeline, especially under high load conditions.

Recommendation: When integrating with OpenObserve, it’s beneficial to review and customize these default settings to optimize performance and ensure that your logs are efficiently processed and stored. 

OpenObserve’s ability to handle various log configurations allows you to tailor your telemetry pipeline to meet your specific requirements, ensuring that your JSON logs are ingested and visualized effectively.

Conclusion

Configuring OpenTelemetry to output logs in JSON format strengthens your observability stack. By adhering to this blog's guidelines and best practices, you ensure that your OpenTelemetry JSON data is accurately captured, stored, and analyzed for better insights.

Integrating with OpenObserve further amplifies these capabilities, offering robust support for JSON logs and providing advanced querying and visualization tools that make monitoring and debugging more efficient.

Whether you’re troubleshooting an issue or simply optimizing your system's performance, the combination of OpenTelemetry and OpenObserve offers a comprehensive solution for managing and analyzing telemetry data.

Ready to take your observability to the next level? 

Sign up for OpenObserve today, visit our website for more details, or check out our GitHub repository to explore more resources and join the community!

Author:

authorImage

The OpenObserve Team comprises dedicated professionals committed to revolutionizing system observability through their innovative platform, OpenObserve. Dedicated to streamlining data observation and system monitoring, offering high performance and cost-effective solutions for diverse use cases.

OpenObserve Inc. © 2024