This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
forked from IBM-Cloud/cf-deployment-tracker-client-node
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tracker.js
113 lines (102 loc) · 4.26 KB
/
tracker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Licensed under the Apache 2.0 License. See footer for details.
'use strict';
var restler = require('restler'),
path = require('path'),
cwd = require('cwd'),
os = require("os"),
metric = require('./metric_tracker');
function track() {
var pkg = null;
try {
pkg = require(path.join(cwd(), 'package.json'));
}
catch(ex) {
// package.json could not be loaded from the cwd
}
var vcapApplication,
vcapServices;
var journey_metric = metric.getJson();
if (process.env.VCAP_APPLICATION) {
vcapApplication = JSON.parse(process.env.VCAP_APPLICATION);
}
var event = {
date_sent: new Date().toJSON()
};
if(journey_metric!=null && journey_metric.language){
event.runtime = journey_metric.language;
}else{
event.runtime = 'nodejs';
}
try{
event.space_id = os.userInfo().username
}catch(ex){
// Username can't be read.
}
if ((vcapApplication) && (pkg)) {
if (pkg.version) {
event.code_version = pkg.version;
}
if (pkg.repository && pkg.repository.url) {
event.repository_url = pkg.repository.url;
}
if (vcapApplication.application_name) {
event.application_name = vcapApplication.application_name;
}
if (vcapApplication.application_id) {
event.application_id = vcapApplication.application_id;
}
if (vcapApplication.hasOwnProperty('instance_index')) {
event.instance_index = vcapApplication.instance_index;
}
if (vcapApplication.space_id) {
event.space_id = vcapApplication.space_id;
}
if (vcapApplication.application_version) {
event.application_version = vcapApplication.application_version;
}
if (vcapApplication.application_uris) {
event.application_uris = vcapApplication.application_uris;
}
if (vcapApplication.cf_api) {
event.provider = vcapApplication.cf_api;
}
event.bound_services = [];
if (process.env.VCAP_SERVICES) {
// refer to http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#VCAP-SERVICES
vcapServices = JSON.parse(process.env.VCAP_SERVICES);
if(Object.keys(vcapServices).length > 0) {
event.bound_vcap_services = {};
// for each bound service count the number of instances and identify used plans
Object.keys(vcapServices).forEach(function(service_label) {
event.bound_vcap_services[service_label] = {
'count': vcapServices[service_label].length, // number of service_label instances
'plans': [] // (optional) plan information for service_label
};
vcapServices[service_label].forEach(function (serviceInstance) {
if(serviceInstance.hasOwnProperty('plan')) {
event.bound_vcap_services[service_label].plans.push(serviceInstance.plan);
}
if(serviceInstance.hasOwnProperty('name')) {
event.bound_services.push(serviceInstance.name);
}
});
// Keep plans property only if at least one plan is associated with this service
if(event.bound_vcap_services[service_label].plans.length === 0) {
delete event.bound_vcap_services[service_label].plans;
}
});
}
}
// original tracker
// restler.postJson('https://deployment-tracker.mybluemix.net/api/v1/track', event).on('complete', function (data) {
// });
}
var url = 'https://metrics-tracker.mybluemix.net/api/v1/track';
if(journey_metric!=null){
event = metric.massage(journey_metric,event);
}
restler.postJson(url, event).on('complete', function (data) {
console.log('Uploaded stats', data);
});
}
module.exports.track = track;