Dynatrace OpenKit offers a number of API methods that enable you to integrate OpenKit into your application. The sections below describe how to use each of these OpenKit methods.
Obtain an OpenKit instance
To obtain a new OpenKit instance, use DynatraceOpenKitBuilder.
endpointURL is the Dynatrace endpoint that OpenKit communicates with. You can find the endpoint URL in your custom application settings. See Instrument your application using Dynatrace OpenKit for more information.
applicationID is the unique identifier of the application. You can find the application ID in your custom application settings. See Instrument your application using Dynatrace OpenKit for more information.
deviceID is a unique identifier, which can be used to uniquely identify a device.
In addition to the mandatory parameters described above, the builder provides additional methods to further customize OpenKit. These include device-specific information like the operating system, manufacturer, or model ID.
Method name
Description
Default value
Since version
withApplicationVersion
Sets the application version
OpenKit version
1.0.1
withOperatingSystem
Sets the operating system name
OpenKit <OpenKit version>
1.0.1
withManufacturer
Sets the manufacturer
Dynatrace
1.0.1
withModelID
Sets the model ID
OpenKitDevice
1.0.1
withBeaconCacheMaxRecordAge
Sets the maximum age of an entry in the beacon cache in milliseconds
1 h 45 min
1.0.1
withBeaconCacheLowerMemoryBoundary
Sets the lower memory boundary of the beacon cache in bytes
80 MB
1.0.1
withBeaconCacheUpperMemoryBoundary
Sets the upper memory boundary of the beacon cache in bytes
100 MB
1.0.1
withLogger
Sets a custom Logger replacing the currently set one
DefaultLogger
1.0.1
withTrustManager
Sets a custom SSLTrustManager replacing the currently set one
SSLStrictTrustManager
1.0.1
withDataCollectionLevel
Sets the data collection level
DataCollectionLevel.USER_BEHAVIOR
1.1.0
withCrashReportingLevel
Sets the crash reporting level
CrashReportingLevel.OPT_IN_CRASHES
1.1.0
withLogLevel
Sets the default log level when the built-in logger is used
LogLevel.WARN
2.0.0
withHttpRequestInterceptor
Sets the Interceptor for requests to Dynatrace backends
NullHttpRequestInterceptor
2.2.0
withHttpResponseInterceptor
Sets the Interceptor for responses received from Dynatrace backends
NullHttpResponseInterceptor
2.2.0
Method name
Description
Default value
Since version
WithApplicationVersion
Sets the application version
OpenKit version
1.0.1
WithOperatingSystem
Sets the operating system name
OpenKit <OpenKit version>
1.0.1
WithManufacturer
Sets the manufacturer
Dynatrace
1.0.1
WithModelID
Sets the model ID
OpenKitDevice
1.0.1
WithBeaconCacheMaxRecordAge
Sets the maximum age of an entry in the beacon cache in milliseconds
1 h 45 min
1.0.1
WithBeaconCacheLowerMemoryBoundary
Sets the lower memory boundary of the beacon cache in bytes
80 MB
1.0.1
WithBeaconCacheUpperMemoryBoundary
Sets the upper memory boundary of the beacon cache in bytes
100 MB
1.0.1
WithLogger
Sets a custom ILogger replacing the currently set one
DefaultLogger
1.0.1
WithTrustManager
Sets a custom ISSLTrustManager replacing the currently set one
SSLStrictTrustManager
1.0.1
WithDataCollectionLevel
Sets the data collection level
DataCollectionLevel.USER_BEHAVIOR
1.1.0
WithCrashReportingLevel
Sets the crash reporting level
CrashReportingLevel.OPT_IN_CRASHES
1.1.0
WithLogLevel
Sets the default log level when the built-in logger is used
LogLevel.WARN
2.0.0
WithHttpRequestInterceptor
Sets the Interceptor for requests to Dynatrace backends
NullHttpRequestInterceptor
2.2.0
WithHttpResponseInterceptor
Sets the Interceptor for responses received from Dynatrace backends
NullHttpResponseInterceptor
2.2.0
Method name
Description
Default value
Since version
withApplicationVersion
Sets the application version
OpenKit version
1.0.0
withOperatingSystem
Sets the operating system name
OpenKit <OpenKit version>
1.0.0
withManufacturer
Sets the manufacturer
Dynatrace
1.0.0
withModelID
Sets the model ID
OpenKitDevice
1.0.0
withBeaconCacheMaxRecordAge
Sets the maximum age of an entry in the beacon cache in milliseconds
1 h 45 min
1.0.0
withBeaconCacheLowerMemoryBoundary
Sets the lower memory boundary of the beacon cache in bytes
80 MB
1.0.0
withBeaconCacheUpperMemoryBoundary
Sets the upper memory boundary of the beacon cache in bytes
100 MB
1.0.0
withLogger
Sets a custom ILogger replacing the currently set one
DefaultLogger
1.0.0
withTrustManager
Sets a custom ISSLTrustManager replacing the currently set one
SSLStrictTrustManager
1.0.0
withDataCollectionLevel
Sets the data collection level
DataCollectionLevel::USER_BEHAVIOR
1.0.0
withCrashReportingLevel
Sets the crash reporting level
CrashReportingLevel::OPT_IN_CRASHES
1.0.0
withLogLevel
Sets the default log level when the built-in logger is used
LogLevel.WARN
2.0.0
withHttpRequestInterceptor
Sets the Interceptor for requests to Dynatrace backends
NullHttpRequestInterceptor
2.2.0
withHttpResponseInterceptor
Sets the Interceptor for responses received from Dynatrace backends
NullHttpResponseInterceptor
2.2.0
Method name
Description
Default value
Since version
useApplicationVersionForConfiguration
Sets the application version
OpenKit version
1.0.0
useOperatingSystemForConfiguration
Sets the operating system name
OpenKit <OpenKit version>
1.0.0
useManufacturerForConfiguration
Sets the manufacturer
Dynatrace
1.0.0
useModelIDForConfiguration
Sets the model ID
OpenKitDevice
1.0.0
useBeaconCacheBehaviorForConfiguration
Sets caching behavior for the beacon cache
1 h 45 min retention, 80 MB lower memory boundary, 100 MB upper memory boundary
1.0.0
useLoggerForConfiguration
Sets a custom ILogger replacing the currently set one
A default logger, logging to stdout, is used as fallback
1.0.0
useTrustModeForConfiguration
Sets a custom ISSLTrustManager replacing the currently set one
STRICT_TRUST
1.0.0
useDataCollectionLevelForConfiguration
Sets the data collection level
DATA_COLLECTION_LEVEL_USER_BEHAVIOR
1.0.0
useCrashReportingLevelForConfiguration
Sets the crash reporting level
CRASH_REPORTING_LEVEL_OPT_IN_CRASHES
1.0.0
useDefaultLogLevelForConfiguration
Sets the default log level when the built-in logger is used
LogLevel.WARN
2.0.0
useHttpRequestInterceptorForConfiguration
Sets the Interceptor for requests to Dynatrace backends
NullHttpRequestInterceptor
2.2.0
useHttpResponseInterceptorForConfiguration
Sets the Interceptor for responses received from Dynatrace backends
NullHttpResponseInterceptor
2.2.0
Method name
Description
Default value
Since version
withApplicationVersion
Sets the application version
OpenKit version
1.0.0
withOperatingSystem
Sets the operating system name
OpenKit <OpenKit version>
1.0.0
withManufacturer
Sets the manufacturer
Dynatrace
1.0.0
withModelID
Sets the model ID
OpenKitDevice
1.0.0
withUserLanguage
Sets the user language
1.0.0
withScreenResolution
Sets the screen resolution
1.0.0
withScreenOrientation
Sets the screen orientation
1.0.0
withCommunicationChannel
Sets the communication channel
HttpCommunicationChannel
1.0.0
withRandomNumberProvider
Sets the random number provider
DefaultRandomNumberProvider
1.0.0
withLoggerFactory
Sets the logger factory
ConsoleLoggerFactory
1.0.0
withDataCollectionLevel
Sets the data collection level
2 (User Behavior)
1.0.0
withCrashReportingLevel
Sets the crash reporting level
2 (OptIn)
1.0.0
withLogLevel
Sets the default log level when the built-in logger is used
LogLevel.Info
1.0.0
SSL/TLS security
All OpenKit communication to the backend happens via HTTPS. By default, OpenKit expects valid server certificates. However, it is possible, if needed, to bypass certificate validation. You can configure a custom trust manager using the builder.
We do not recommend bypassing TLS/SSL server certificate validation, since this allows man-in-the-middle attacks.
class MyCustomTrustManager implements SSLTrustManager {
// implement interface methods
}
SSLTrustManager trustManager = new MyCustomTrustManager()
OpenKit openKit = new DynatraceOpenKitBuilder(endpointURL, applicationID, deviceID)
.withTrustManager(trustManager)
.build();
class MyCustomTrustManager : ISSLTrustManager
{
// implement interface methods
}
ISSLTrustManager trustManager = new MyCustomTrustManager()
OpenKit openKit = new DynatraceOpenKitBuilder(endpointURL, applicationID, deviceID)
By default, OpenKit uses a logger implementation that logs to stdout. If the default logger is used, you can enable verbose logging via DynatraceOpenKitBuilder. When the verbose mode is enabled, info and debug messages are logged.
When obtaining an OpenKit instance from the builder, the instance starts an automatic initialization phase. By default, initialization is performed asynchronously.
There might be situations when you need to ensure that initialization is completed before proceeding with the program logic, for example, in case of short-lived applications where a valid init and shutdown cannot be guaranteed. For such applications, OpenKit allows to wait for the initialization in two ways:
With timeout: The calling threads waits a given amount of time for OpenKit to initialize. The method returns false in case the timeout expired or a shutdown was performed in the meantime and true to indicate successful initialization.
Without timeout: The calling thread blocks until OpenKit is initialized. In case of misconfiguration this might block the calling thread indefinitely. The return value indicates whether the OpenKit instance has been initialized or a shutdown was performed meanwhile.
// wait until the OpenKit instance is fully initialized
You can tag the user assigned to a session. This enables you to search and filter specific user sessions and analyze individual user behavior over time in the backend. See User tagging for more details.
session.identifyUser("jane.doe@example.com");
session.IdentifyUser("jane.doe@example.com");
session->identifyUser("jane.doe@example.com");
identifyUser(session,"jane.doe@example.com");
session.identifyUser("jane.doe@example.com");
When the user opt-in mode is enabled for your application, it might affect user tagging and reporting of custom events, user actions, values, and errors. The exact data types not reported to Dynatrace depend on the data collection level set by a particular user. For details, refer to Data collection levels.
Finish a session
When a session is no longer needed, you should end it explicitly. Although all open sessions are automatically ended when OpenKit is shut down, it's highly recommended to manually end sessions that are no longer in use.
session.end();
session = null; // not needed, just used to indicate that the session is no longer valid.
session.End();
session = null; // not needed, just used to indicate that the session is no longer valid.
session->end();
session =nullptr;// not needed, just used to indicate that the session is no longer valid.
endSession(session);
session =NULL;// good pratice, as the allocated memory is freed in endSession
session.end();
Report a crash
You can report unexpected application crashes on a session. The crash details are sent immediately after you've reported a crash.
private static int div(int numerator, int denominator) {
// and now report the application crash via the session
session.reportCrash(e.name, e.message, e.stack);
Create custom and child actions
You can define and report custom actions. After you create a custom action, you can add a child action to it or enhance an action with additional information before finally closing it. You should create custom actions from a session and child actions from a custom action.
The maximum duration of a user action in custom apps is 10 minutes. When a user action takes longer than this, such an action is discarded and not reported to Dynatrace.
There's no limit on the number of child actions attached to a custom action. However, note that you can have only one level of child actions—you can't create a child action for another child action (child actions can't have their own child actions). Also, refer to User session structure for individual user.
When the user opt-in mode is enabled for your application, it might affect user tagging and reporting of custom events, user actions, values, and errors. The exact data types not reported to Dynatrace depend on the data collection level set by a particular user. For details, refer to Data collection levels.
End an action
To record accurate timing information for actions, you should leave actions once they're finished.
Action parentAction = action.leaveAction(); // returns the appropriate custom action
Action parent = parentAction.leaveAction(); // will always return null
IAction parentAction = action.LeaveAction(); // returns the appropriate custom action
IAction parent = parentAction.LeaveAction(); // will always return null
std::shared_ptr<openkit::IRootAction> parentAction = action->leaveAction();// returns the appropriate custom action
parentAction->leaveAction();// return type is void
leaveAction(action);// return type is void
leaveRootAction(rootAction);// return type is void
const parentAction = action.leaveAction();// returns the appropriate custom action
const parent = parentAction.leaveAction();// will always return null
Cancel an action
Canceling an action is similar to leaving an action, except that the action is discarded and is not sent to Dynatrace. Open child objects, like child actions and web request tracers, are discarded as well. Child objects that have been closed previously are sent to the backend and might be processed, depending on the event type.
Action parentAction = action.cancelAction(); // returns the appropriate custom action
Action parent = parentAction.cancelAction(); // will always return null
IAction parentAction = action.CancelAction(); // returns the appropriate custom action
IAction parent = parentAction.CancelAction(); // will always return null
std::shared_ptr<openkit::IRootAction> parentAction = action->cancelAction();// returns the appropriate custom action
parentAction->cancelAction();// return type is void
cancelAction(action);// return type is void
cancelRootAction(rootAction);// return type is void
Report an event
You can report named events on actions.
If you want to report standalone events with lots of additional information, see Report a business event.
String eventName = "eventName";
action.reportEvent(eventName);
// also report on the RootAction
rootAction.reportEvent(eventName);
string eventName = "eventName";
action.ReportEvent(eventName);
// also report on the RootAction
rootAction.ReportEvent(eventName);
constchar* eventName ="eventName";
action->reportEvent(eventName);
// also report on the RootAction
rootAction->reportEvent(eventName);
constchar* eventName ="eventName";
reportEventOnAction(action, eventName);
// also report on the RootAction
reportEventOnRootAction(rootAction, eventName);
const eventName ="eventName";
action.reportEvent(eventName);
// also report on the RootAction
rootAction.reportEvent(eventName);
When the user opt-in mode is enabled for your application, it might affect user tagging and reporting of custom events, user actions, values, and errors. The exact data types not reported to Dynatrace depend on the data collection level set by a particular user. For details, refer to Data collection levels.
Report a value
The reportValue method allows you to report key-value pairs of metadata that you can later view in the Dynatrace web UI and convert into user action and user session properties. The reported values must be part of a user action.
You can report values of the following data types:
32-bit integer
64-bit integer
Double
String
// first report a 32-bit int value
String keyIntType = "intType";
int valueInt = 42;
action.reportValue(keyIntType, valueInt);
// then let's report a 64-bit long value
String keyLongType = "longType";
long valueLong = Long.MIN_VALUE;
action.reportValue(keyLongType, valueLong);
// then let's report a double value
String keyDoubleType = "doubleType";
double valueDouble = 3.141592653589793;
action.reportValue(keyDoubleType, valueDouble);
// and also a string value
String keyStringType = "stringType";
String valueString = "The quick brown fox jumps over the lazy dog";
action.reportValue(keyStringType, valueString);
// first report a 32-bit int value
string keyIntType = "intType";
int valueInt = 42;
action.ReportValue(keyIntType, valueInt);
// then let's report a 64-bit long value
string keyLongType = "longType";
long valueLong = long.MinValue;
action.ReportValue(keyLongType, valueLong);
// then let's report a double value
string keyDoubleType = "doubleType";
double valueDouble = 3.141592653589793;
action.ReportValue(keyDoubleType, valueDouble);
// and also a string value
string keyStringType = "stringType";
string valueString = "The quick brown fox jumps over the lazy dog";
action.ReportValue(keyStringType, valueString);
// first report a 32-bit integer value
constchar* keyIntType ="intType";
int32_t valueInt =42;
action->reportValue(keyIntType, valueInt);
// then let's report a 64-bit integer value
constchar* keyLongType ="longType";
int64_t valueLong = LLONG_MIN;
action->reportValue(keyLongType, valueLong);
// then let's report a double value
constchar* keyDoubleType ="doubleType";
double valueDouble =3.141592653589793;
action->reportValue(keyDoubleType, valueDouble);
// and also a string value
constchar* keyStringType ="stringType";
constchar* valueString ="The quick brown fox jumps over the lazy dog";
const stringValue ='The quick brown fox jumps over the lazy dog';
action.reportValue(keyStringName, stringValue);
To view the reported values in Dynatrace, go to the details of the user action that should contain that metadata and scroll down to the Reported values section.
When the user opt-in mode is enabled for your application, it might affect user tagging and reporting of custom events, user actions, values, and errors. The exact data types not reported to Dynatrace depend on the data collection level set by a particular user. For details, refer to Data collection levels.
Report an error
You can report an error including its name (errorName) and error code (errorCode).
OpenKit Java and OpenKit .NET offer a convenience API to directly report caught exceptions, as demonstrated in the example below.
try {
// call a method that is throwing an exception
callMethodThrowingException();
} catch(Exception caughtException) {
// report the caught exception as error via OpenKit
String errorName = "Unknown Error";
action.reportError(errorName, caughtException);
}
try
{
// call a method that is throwing an exception
CallMethodThrowingException();
}
catch(Exception caughtException)
{
// report the caught exception as error via OpenKit
string errorName = "Unknown Error";
action.ReportError(errorName, caughtException);
}
When the user opt-in mode is enabled for your application, it might affect user tagging and reporting of custom events, user actions, values, and errors. The exact data types not reported to Dynatrace depend on the data collection level set by a particular user. For details, refer to Data collection levels.
Report a business event
Dynatrace SaaS version 1.253+
With sendBizEvent, you can report business events. These are standalone events, as Dynatrace sends them separately from user actions or user sessions.
Business events are only captured for monitored sessions. When OpenKit isn't initialized or a session is excluded from monitoring due to cost and traffic control, business events are not reported for such sessions. Note that this behavior might be subject to change in the future, potentially allowing business events to be sent to Dynatrace regardless of session monitoring.
One of the most powerful OpenKit features is web request tracing. When the application starts a web request, for example, HTTP GET, a special tag can be attached to the header. This header allows Dynatrace to correlate actions with a server-side distributed trace. An example is shown below.
// create URL and open URLConnection
URL url = new URL("http://www.my-backend.com/api/v3/users");
const headerName = webRequestTagHeader;// webRequestTagHeader can be imported
const headerValue = webRequestTracer.getTag();
webRequestTracer.start();
// perform the request here & do not forget to add the HTTP header
webRequestTracer.setBytesSent(12345);
webRequestTracer.setBytesReceived(67890);
webRequestTracer.stop(200);// stop the web request tracer, with the response code
Configure data privacy
You can dynamically adjust data privacy settings and build your custom applications in compliance with data protection laws and regulations. The privacy API methods allow you to dynamically change the data collection level and activate or deactivate crash reporting.
Data collection levels
The table below describes the available data collection levels and shows whether user tags and custom user actions, events, values, and errors are reported for a particular level.
Level
Description
User tags, custom events, and custom values
Custom user actions and errors
Off
Monitoring data is not sent
No personal data is sent; all identifiers are randomized on every launch.1
Performance
Only performance, automatically captured data is sent
No personal data is sent; all identifiers are randomized on every launch.
User behavior
Performance data and user data is sent
Personal data is sent; OneAgent recognizes and reports users who revisit in the future.2
1
A single Loading <App> event is sent to track the number of users that opted out.
2
If you haven't configured user tagging and custom event or value reporting, the User behavior level works similarly to the Performance level.
Crash reporting levels
The following crash reporting levels are available.
Level name
Crash reporting
Use this level when…
Off
Disabled
You don't need to collect crash reports.
Opt out crashes
Disabled
The user of your application has disallowed the collection of crash reports.
Opt in crashes
default
Enabled
The user of your application has allowed the collection of crash reports.
Set data collection and crash reporting levels
The code examples below show you how to work with the API:
import com.dynatrace.openkit.DataCollectionLevel;
import com.dynatrace.openkit.CrashReportingLevel;
OpenKit openKit = new DynatraceOpenKitBuilder(endpointURL, applicationID, deviceID)
// set a data collection level (user allowed you to capture performance and personal data)
When an OpenKit instance is no longer needed, for example, when the application using OpenKit is shut down, you can clear the obtained instance by invoking. A call to shutdown blocks the calling thread up to 10 seconds, while the OpenKit flushes data that hasn't been transmitted yet to the backend.