Logging with Dynatrace
Logging is a critical piece in any digital system. It enhances visibility, facilitates troubleshooting, ensures compliance, and supports system reliability. With this recipe, you can implement a solution with Conscia that sends log entries 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:
- Dynatrace Logging (
dynatrace-logging
): A DX Engine Connection to Dynatrace. - Dynatrace Logging Authorization (
dynatrace-logging-authorization
): A Secret to store the Dynatrace token used by that Connection. - 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:
- Click Settings in the top-nav, and then select Secrets.
- Click Add Secret.
- Set Secret Code to a value such as dynatrace-logging-authorization.
- Set Secret Name to a value such as Dynatrace Logging Authorization.
- Set Secret Value to
Api-Token {Your Dynatrace token}
, being sure to include theApi-Token
prefix and replacing{Your Dynatrace token}
with your Dynatrace token, which must have at least "Ingest logs" scope. - Optionally, enter a Description for the secret.
- Click Submit.
To create the Dynatrace Connection, in the Conscia UI:
- Click Settings in the top-nav, and then select Connections.
- Click Add Connection.
- Set Connection Code to a value such as dynatrace-logging.
- Set Connection Name to a value such as Dynatrace Logging.
- Optionally, enter a Connection Description for the connection.
- Select the Universal API Connector.
- Click Submit.
To configure the Dynatrace Connection, in the Conscia UI:
- Click the Edit button for the Dynatrace Logging Connection.
- Under Base URL, for Get value from, select Literal.
- 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 ashttps://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). - Under Base Headers, click Add another item.
- For the new header, set Header to Authorization.
- For the new header, set Value to Secret.
- For the value of the new header, select the secret created previously, such as Dynatrace Logging Authorization.
- Under Base Headers, click Add another item.
- For the new header, set Header to Content-Type.
- For the new header, set Value to Literal.
- For the value of the new header, enter
application/json; charset=utf-8
. - Click Submit.
To create the Dynatrace Logger Orchestration Component, in the Conscia DX UI:
- In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
- Click Add Component. The Create Component wizard appears.
- For Component Code, enter an identifier for the component, such as dynatrace-logger.
- For Name, enter a name for the component, such as Dynatrace Logger.
- Optionally, enter a Component Description.
- Select the No Rules checkbox. Experience Rules are not relevant to this Component.
- For Component Type, select Conscia - Universal API Connector. This Component Type can connect to almost any Webservice API.
- Optionally, select the Is Asynchronous checkbox.
- 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:
- Next to the Dynatrace Logger component, click the Edit button. The Edit Component wizard appears.
- For Connection, select the Dynatrace Logging Connection.
- 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. - For Method, select POST.
- 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 Sub Component, in the DX Engine UI:
- In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
- Next to the component that will use the Dynatrace Logger Component as a Sub Component, click the Edit button. The Edit Component wizard appears.
- From the drop-down at the top right, select Sub Components. Note: the drop-down may have a scrollbar.
- Under Sub Components, click Add another item.
- For Property Name, enter a key in which the response from the Sub Component will appear in the response from the invoking Component.
- Under Component Codes, add the Dynatrace Logger (
dynatrace-logger
) Component. - Under Context Field for Sub Component, click Add another item.
- For Context Field, enter logMessage.
- For Expression, enter a JavaScript expression that returns the data to log.
- Click Submit.
You can set Expression to a value such as response
or a key within that value.