Skip to main content

Logging with Dynatrace

You can implement an solution based on this recipe that explains how you can log information to Dynatrace.

Overall Orchestration Flow

Clients that invoke this orchestration flow can pass a logging message, a logging session identifier (an arbitrary identifier you can use to locate related log entries in Dynatrace), and a Dynatrace log status (TRACE, DEBUG, INFO, WARNING or WARN, ERROR, FATAL or CRITICAL, or OFF) that defaults to INFO.

In general, generic Components such as logging should not establish dependencies. Therefore, a common pattern is for a component that has something to log to use the logging component as a Sub Component.

Mapping Out DX Engine Elements

This flow consists of three DX Engine elements:

  1. Dynatrace Logging (dynatrace-logging): A DX Engine Connection to Dynatrace.
  2. Dynatrace Logging Authorization (dynatrace-logging-authorization): A Secret to store the Dynatrace token used by that Connection.
  3. Dynatrace Logger (dynatrace-logger): An Orchestration Component that uses that Connection to log messages to Dynatrace.

DX Engine Configuration Details

We can store the Dynatrace token as a secret in the DX Engine. To create the secret, in the Conscia UI:

  1. Click Settings in the top-nav, and then select Secrets.
  2. Click Add Secret.
  3. Set Secret Code to a value such as dynatrace-logging-authorization.
  4. Set Secret Name to a value such as Dynatrace Logging Authorization.
  5. Set Secret Value to Api-Token {Your Dynatrace token}, being sure to include the Api-Token prefix and replacing {Your Dynatrace token} with your Dynatrace token, which must have at least "Ingest logs" scope.
  6. Optionally, enter a Description for the secret.
  7. Click Submit.

To create the Dynatrace Connection, in the Conscia UI:

  1. Click Settings in the top-nav, and then select Connections.
  2. Click Add Connection.
  3. Set Connection Code to a value such as dynatrace-logging.
  4. Set Connection Name to a value such as Dynatrace Logging.
  5. Optionally, enter a Connection Description for the connection.
  6. Select the Universal API Connector.
  7. Click Submit.

To configure the Dynatrace Connection, in the Conscia UI:

  1. Click the Edit button for the Dynatrace Logging Connection.
  2. Under Base URL, for Get value from, select Literal.
  3. Set the value of Base URL to the Dynatrace API base URL, such as https://{your-dynatrace-domain}/e/{your-dynatrace-environment-id}/api/v2, replacing {your-dynatrace-domain} with your Dynatrace domain and {your-dynatrace-environment-id} with your Dynatrace environment ID (such as https://identifier.live.dynatrace.com/e/identifier/api/v2, where the identifier(s) may be in the URL that you see in the browser's address bar when you access Dynatrace).
  4. Under Base Headers, click Add another item.
  5. For the new header, set Header to Authorization.
  6. For the new header, set Value to Secret.
  7. For the value of the new header, select the secret created previously, such as Dynatrace Logging Authorization.
  8. Under Base Headers, click Add another item.
  9. For the new header, set Header to Content-Type.
  10. For the new header, set Value to Literal.
  11. For the value of the new header, enter application/json; charset=utf-8.
  12. Click Submit.

To create the Dynatrace Logger Orchestration Component, in the Conscia DX UI:

  1. In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
  2. Click Add Component. The Create Component wizard appears.
  3. For Component Code, enter an identifier for the component, such as dynatrace-logger.
  4. For Name, enter a name for the component, such as Dynatrace Logger.
  5. Optionally, enter a Component Description.
  6. Select the No Rules checkbox. Experience Rules are not relevant to this Component.
  7. For Component Type, select Conscia - Universal API Connector. This Component Type can connect to almost any Webservice API.
  8. Optionally, select the Is Asynchronous checkbox.
  9. Click Submit.

The advantage of selecting Is Asynchronous is that the orchestration flow will not block until the Webservice call to Dynatrace completes. The disadvantage is that if logging fails, you will not receive notification.

Note that the Dynatrace API returns HTTP 204 "No Content" when it successfully ingests a log entry.

To configure the Dynatrace Logger Orchestration Component, on the Manage Components page:

  1. Next to the Dynatrace Logger component, click the Edit button. The Edit Component wizard appears.
  2. For Connection, select the Dynatrace Logging Connection.
  3. For Webservice Path, select Literal, and enter /logs/ingest for the value. This is the relative path to the Dynatrace Webservice API for ingesting log entries.
  4. For Method, select POST.
  5. Under Body, for Get value from, select JS Expression, and then set the value to an expression such as the following:
[
{
"content": contextField('logEntry').logMessage,
"logSession": contextField('logEntry').logSession,
"severity": contextField('logEntry').logStatus ? contextField('logEntry').logStatus : "INFO"
}
]

Note that this body is an array; you could pass mulitple log entries in a single call.

You can pass context such as the following to the Dynatrace Logger Component:

{
logEntry : {
"logMessage": "This is the message to log.",
"logSession": "{YOUR_INITIALS}",
"logStatus": "DEBUG"
}
}

Using Dynatrace Logger as a Sub Component

To use the Dynatrace Logger Component as a Subcomponent, in DX Studio:

  1. In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
  2. Next to the component that will use the Dynatrace Logger Component as a Subcomponent, click the Edit button. The Edit Component wizard appears.
  3. From the drop-down at the top right, select Sub Components. Note: the drop-down may have a scrollbar.
  4. Under Sub Components, click Add another item.
  5. For Property Name, enter a key in which the response from the Sub Component will appear in the response from the invoking Component.
  6. Under Component Codes, add the Dynatrace Logger (dynatrace-logger) Component.
  7. Under Context Field for Sub Component, click Add another item.
  8. For Context Field, enter logMessage.
  9. For Expression, enter a JavaScript expression that returns the data to log.
  10. Click Submit.

You can set Expression to a value such as response or a key within that value. Note that DX Engine invokes Sub Commponents for a Component before invoking Response Transformation.

References