Collecting Time-Series Data with Predix (a Golang API Wrapper Included)
Recently, we’ve created a wrapper to provide a more convenient way to interact with the Time Series service from Go. In this post, we show how to work with the Predix Time Series API using our Go library.
A demo application that we will deploy to Predix consists of two parts:
- a “sensor” pushing time-series data
- a web application reading the data from the Time Series service and visualizing it
The source code for the application is available in this GitHub repository.
Prerequisites
To get started with the Time Series service, make sure you have the following:
- a Predix account
- the Cloud Foundry CLI
- the Cloud Foundry UAA CLI
Getting code and creating an application in Predix
Clone the following repository and go to the demo application directory:
git clone https://github.com/Altoros/go-predix-timeseries.git
cd go-predix-timeseries/examples/go-predix-timeseries-demo/
cf push APP_NAME # This will create an application in Predix.
Setting up services
Then, create a UAA service instance and bind it to your application:
cf create-service predix-uaa Tiered go-predix-timeseries-demo-uaa -c '{"adminClientSecret":""}'
cf bind-service APP_NAME go-predix-timeseries-demo-uaa
Create and bind the Time Series service:
cf create-service predix-timeseries Bronze go-predix-timeseries-demo-ts -c '{"trustedIssuerIds":[""]}'
cf bind-service APP_NAME go-predix-timeseries-demo-ts
You can find issuerId
in the output of the cf env APP_NAME
command:
Creating ingest and query clients
Now, we need to create ingest and query clients in our UAA service with appropriate authorities. To find the required authorities, type the cf env APP_NAME
command and look for the zone-token-scopes
fields in credentials
of the predix-timeseries
service:
To create the clients, we will use uaac:
uaac target UAA_uri
uaac token client get admin
uaac client add ingest --authorized_grant_types client_credentials --authorities "timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.user,timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.ingest"
uaac client add query --authorized_grant_types client_credentials --authorities "timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.user,timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.query"
Now, set the CLIENT_ID
and CLIENT_SECRET
environment variables and restage the application to ensure the environment variable changes take effect:
cf set-env go-predix-timeseries-demo CLIENT_ID query
cf set-env go-predix-timeseries-demo CLIENT_SECRET
cf restage APP_NAME
Pushing time-series data
Run the “sensor” to start pushing time-series data:
cd sensor
go run sensor.go -clientId=ingest -clientSecret= -ingestUrl="ingest_url" -uaaIssuerId="uaa_IssuerId" -zoneId="zone_id"
Look for the ingest URL and zone ID (the uri
and zone-http-header-value
fields) in the Time Series credentials:
"ingest": {
"uri": "wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages",
"zone-http-header-name": "Predix-Zone-Id",
"zone-http-header-value": "bfdd9bb0-0b0f-4667-8338-18b048c8ce31",
"zone-token-scopes": [
"timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.user",
"timeseries.zones.bfdd9bb0-0b0f-4667-8338-18b048c8ce31.ingest"
]
}
Now you can open the web application in a browser and see it populating data.
The Time Series service enables you to quickly and efficiently ingest, store, and analyze time-series data. Our library, in its turn, provides an easy way to work with Time Series from Go.
Go API Wrapper’s GitHub repo | The demo app’s GitHub repo