Collecting Application Logs Using OTEL Java Agent

Collecting Application Logs Using OTEL Java Agent

You can directly send your application logs to SigNoz using Java Agent provided by OpenTelemetry. In this doc we will run a sample java application with the OpenTelemetry Java agent to send logs to SigNoz.

For collecting logs we will have to download the java agent from here.

To sends logs from a Java application you will have to add the agent and add the environment variables for the agent.

For Sending Logs To SigNoz Cloud

OTEL_LOGS_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.{region}.signoz.cloud:443" OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<SIGNOZ_INGESTION_KEY> OTEL_RESOURCE_ATTRIBUTES=service.name=<app_name> java -javaagent:/path/opentelemetry-javaagent.jar -jar  <myapp>.jar

You will have to add <SIGNOZ_INGESTION_KEY> and depending on the choice of your region for SigNoz cloud, the otlp endpoint will vary according to this table.

RegionEndpoint
USingest.us.signoz.cloud:443
INingest.in.signoz.cloud:443
EUingest.eu.signoz.cloud:443

For Sending Logs To SigNoz Hosted Locally

OTEL_LOGS_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT="http://<IP of SigNoz Backend>:4317" OTEL_RESOURCE_ATTRIBUTES=service.name=<app_name> java -javaagent:/path/opentelemetry-javaagent.jar -jar  <myapp>.jar

Settings for Appender instrumentation based on the logging library

You can use appender settings by passing it as an argument in the -D<property>=<value> format.

ex:- -Dotel.instrumentation.logback-appender.experimental-log-attributes=true

Logback

LINK - Logback

System propertyTypeDefault ValueDescription
otel.instrumentation.logback-appender.experimental-log-attributesBooleanfalseEnable the capture of experimental log attributes thread.name and thread.id.
otel.instrumentation.logback-appender.experimental.capture-code-attributesBooleanfalseEnable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead.
otel.instrumentation.logback-appender.experimental.capture-marker-attributeBooleanfalseEnable the capture of Logback markers as attributes.
otel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributesBooleanfalseEnable the capture of Logback key value pairs as attributes.
otel.instrumentation.logback-appender.experimental.capture-logger-context-attributesBooleanfalseEnable the capture of Logback logger context properties as attributes.
otel.instrumentation.logback-appender.experimental.capture-mdc-attributesStringNAComma separated list of MDC attributes to capture. Use the wildcard character * to capture all attributes.

Log4j

LINK - Log4j

System propertyTypeDefaultDescription
otel.instrumentation.log4j-appender.experimental-log-attributesBooleanfalseEnable the capture of experimental log attributes thread.name and thread.id.
otel.instrumentation.log4j-appender.experimental.capture-map-message-attributesBooleanfalseEnable the capture of MapMessage attributes.
otel.instrumentation.log4j-appender.experimental.capture-marker-attributeBooleanfalseEnable the capture of Log4j markers as attributes.
otel.instrumentation.log4j-appender.experimental.capture-mdc-attributesStringComma separated list of context data attributes to capture. Use the wildcard character * to capture all attributes.

In the below example we will configure a java application to send logs to SigNoz.

[Example] How to Collect Application Logs Using OTEL Java Agent?

  • Clone this repository
  • Build the application using ./mvnw package
  • Now run the application

For SigNoz Cloud

OTEL_LOGS_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.{region}.signoz.cloud:443" OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<SIGNOZ_INGESTION_KEY> OTEL_RESOURCE_ATTRIBUTES=service.name=myapp java -javaagent:/path/opentelemetry-javaagent.jar -jar target/*.jar

You will have to replace the value of {region} according to region of your cloud account and also add <SIGNOZ_INGESTION_KEY>

For SigNoz Hosted Locally

OTEL_LOGS_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT="http://<host>:4317" OTEL_RESOURCE_ATTRIBUTES=service.name=myapp java -javaagent:/path/opentelemetry-javaagent.jar -jar target/*.jar

You will have to replace your the value of host as 0.0.0.0 if SigNoz is running in the same host, for other configurations please check the troubleshooting guide.

  • Visit http://localhost:8090 to access the application.
  • Once you use the application logs will be visible on SigNoz UI.
  • If you want to enable settings here is how you do it.

Let's say we want to enable -Dotel.instrumentation.logback-appender.experimental-log-attributes=true

For SigNoz Cloud

OTEL_LOGS_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.{region}.signoz.cloud:443" OTEL_EXPORTER_OTLP_HEADERS=signoz-access-token=<SIGNOZ_INGESTION_KEY> OTEL_RESOURCE_ATTRIBUTES=service.name=myapp java -javaagent:/path/opentelemetry-javaagent.jar -Dotel.instrumentation.logback-appender.experimental-log-attributes=true -jar target/*.jar

You will have to replace the value of {region} according to the region of your cloud account and also replace <SIGNOZ_INGESTION_KEY> with your SigNoz Cloud Ingestion key.

For SigNoz Hosted Locally

OTEL_LOGS_EXPORTER=otlp OTEL_EXPORTER_OTLP_ENDPOINT="http://<host>:4317" OTEL_RESOURCE_ATTRIBUTES=service.name=myapp java -javaagent:/path/opentelemetry-javaagent.jar -Dotel.instrumentation.logback-appender.experimental-log-attributes=true -jar target/*.jar

Was this page helpful?