Interana provides a set of RESTful APIs as part of the External API. These APIs are language/platform agnostic.
The purpose of this article is to specifically assist in creating a language-specific implementation that can be used for your custom scenario. After completing this how-to, you will have ready-to-run code capable of querying Interana using the supported Interana APIs.
Assuming you have an Interana environment available, ensure that you can access the environment and can run queries using the UI. If you don't know the URL or don't have an environment, contact Interana Support (for Production environments) or your friendly Interana Solution Architect (for POC environments).
Interana Dataset Name
Access your Interana Environment and find the table or dataset to use for your queries.
You must have an authorization token to access the Interana API. This will allow you to access the API and pass the authentication step.
Contact Interana Support
This is the recommended path. This will be as simple as sending a quick email to Interana Support to request a token. Keep this token safe, since it can't be generated again.
Download the Python SDK
Download this SDK into your local workspace. This is a community supported SDK and we're providing it on a best-effort basis only. Interana will not provide support for this SDK.
This section describes how to create an implementation in Python.
We will use the Query SDK to assist in querying via Python. There are four main actions involved when attempting to query Interana using this SDK:
- Create an Interana client object
- Generate an Interana query object
- Tune the query, if needed
- Retrieve the results from Interana
Create an Interana client object
To get started, we will create an Interana client object. The purpose behind this step is to establish a connection to the Interana cluster that you will be querying from. To do this, start simply by specifying cluster host and the authorization token (from above). For eg:
from query_sdk.interana_client import Client url = "<cluster domain here without no http(s) or slashes>" token = "<your authorization token>" client = Client(url, token)
Generate an Interana query object
Next, initialize a query object using the SDK. This step helps define and formulate the query to retrieve the data. Key info. needed will be the dataset to query against (from above), date range (assuming the querying is from X days back to now). Assumption will be a base query type (number visualization in Interana) and base aggregator type ("count *").
import datetime from query_sdk.interana_client import Query dataset = "<your dataset name>" days_prior = 7 end_time = datetime.datetime.now() start_time = end_time - datetime.timedelta(days=days_prior) query = Query(dataset, start_time, end_time) query_type = "single_measurement" agg_type = "count_star" ia_col = "" query.add_query_info(type=query_type,aggregator=agg_type,column=ia_col)
Tune the query
Now you can filter the query object or issue group-by requests or change the query type. This step is not mandatory but can be used to further filter the query. Or do a group by to visualize the data differently. See the External API reference for more information about the parameters.
filter = "(`event` != \"userlogin\")" query.add_query_info(type=query_type, aggregator=agg_type, column=ia_col, filter=filter_expr) groupby_col = "event" query.add_params(group_by=[groupby_col])
Retrieve results from Interana
Issue the request to the Interana backend to retrieve the result set for your query. This is the final step to actually get the dataset for the query defined.
result = client.query(query) print result
Putting it all together: sample code
The complete code will look similar to the following:
import datetime from query_sdk.interana_client import Client, Query url = "demo.interana.com"token = "tffZR6q0fbRasd21uvvLllLMHbuH+/Dj542K=ER0PY/qNh+hdjEh+16DcL5Gc=BfHT" client = Client(url, token) dataset = "users" days_prior = 7 end_time = datetime.datetime.now() start_time = end_time - datetime.timedelta(days=days_prior) query = Query(dataset, start_time, end_time) query_type = "single_measurement" agg_type = "count_star" ia_col = "" query.add_query_info(type=query_type,aggregator=agg_type,column=ia_col) result = client.query(query) print result
Use the Interana SDK utility
Alternatively, use a helper script that leverages the SDK and provides an easy to use wrapper utility to access the Interana APIs directly. Download the utility (ia_api_consumer.py) from here.
$ ./ia_query_client.py -h
usage: ia_query_client.py [-h] [-d DAYS_PRIOR] [-t QUERY_TYPE] [-a AGGREGATOR]
[-c COLUMN_NAME] [-g GROUP_BY] [-f FILTERS]
[-o OUTPUT_FILE] [-v]
auth_token cluster_domain dataset
Additional language-specific implementations like R, MS-Excel, will be provided in the future.