Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow per-query tenant configuration #85

Merged
merged 5 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions spec/datasource_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ describe('HawkularDatasource', () => {
},
annotation: {
query: "my.timeline",
name: "Timeline"
name: "Timeline",
type: "strings"
}
};

Expand All @@ -498,13 +499,13 @@ describe('HawkularDatasource', () => {

ctx.ds.annotationQuery(options).then(result => {
expect(result).to.have.length(2);
expect(result[0].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline" });
expect(result[0].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline", type: "strings" });
expect(result[0].time).to.equal(13);
expect(result[0].title).to.equal("Timeline");
expect(result[0].tags).to.be.undefined;
expect(result[0].text).to.equal("start");

expect(result[1].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline" });
expect(result[1].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline", type: "strings" });
expect(result[1].time).to.equal(19);
expect(result[1].title).to.equal("Timeline");
expect(result[1].tags).to.be.undefined;
Expand All @@ -521,7 +522,8 @@ describe('HawkularDatasource', () => {
},
annotation: {
query: "my.timeline",
name: "Timeline"
name: "Timeline",
type: "strings"
}
};

Expand Down Expand Up @@ -556,13 +558,13 @@ describe('HawkularDatasource', () => {

ctx.ds.annotationQuery(options).then(result => {
expect(result).to.have.length(2);
expect(result[0].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline" });
expect(result[0].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline", type: "strings" });
expect(result[0].time).to.equal(13);
expect(result[0].title).to.equal("Timeline");
expect(result[0].tags).to.equal("myItem start");
expect(result[0].text).to.equal("start");

expect(result[1].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline" });
expect(result[1].annotation).to.deep.equal({ query: "my.timeline", name: "Timeline", type: "strings" });
expect(result[1].time).to.equal(19);
expect(result[1].title).to.equal("Timeline");
expect(result[1].tags).to.equal("myItem stop");
Expand All @@ -587,7 +589,7 @@ describe('HawkularDatasource', () => {
});
};

ctx.ds.suggestTags('gauge', 'host').then(result => {
ctx.ds.suggestTags({type: 'gauge'}, 'host').then(result => {
expect(result).to.have.length(2);
expect(result[0]).to.deep.equal({ text: 'cartago', value: 'cartago' });
expect(result[1]).to.deep.equal({ text: 'rio', value: 'rio' });
Expand All @@ -607,7 +609,7 @@ describe('HawkularDatasource', () => {
data: {}
});
};
ctx.ds.suggestTags('gauge', 'host').then(result => {
ctx.ds.suggestTags({type: 'gauge'}, 'host').then(result => {
expect(result).to.have.length(0);
}).then(v => done(), err => done(err));
});
Expand All @@ -626,7 +628,7 @@ describe('HawkularDatasource', () => {
});
};

ctx.ds.suggestTagKeys().then(result => {
ctx.ds.suggestTagKeys({}).then(result => {
expect(result).to.have.length(2);
expect(result[0]).to.deep.equal({ text: 'host', value: 'host' });
expect(result[1]).to.deep.equal({ text: 'app', value: 'app' });
Expand All @@ -641,7 +643,8 @@ describe('HawkularDatasource', () => {
},
annotation: {
query: "$who.timeline",
name: "Timeline"
name: "Timeline",
type: "strings"
}
};

Expand Down Expand Up @@ -680,13 +683,13 @@ describe('HawkularDatasource', () => {

ctx.ds.annotationQuery(options).then(result => {
expect(result).to.have.length(2);
expect(result[0].annotation).to.deep.equal({ query: "$who.timeline", name: "Timeline" });
expect(result[0].annotation).to.deep.equal({ query: "$who.timeline", name: "Timeline", type: "strings" });
expect(result[0].time).to.equal(15);
expect(result[0].title).to.equal("Timeline");
expect(result[0].tags).to.equal('your.timeline');
expect(result[0].text).to.equal("start");

expect(result[1].annotation).to.deep.equal({ query: "$who.timeline", name: "Timeline" });
expect(result[1].annotation).to.deep.equal({ query: "$who.timeline", name: "Timeline", type: "strings" });
expect(result[1].time).to.equal(13);
expect(result[1].title).to.equal("Timeline");
expect(result[1].tags).to.equal('my.timeline');
Expand Down
8 changes: 8 additions & 0 deletions src/config_ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class HawkularConfigCtrl {

constructor() {
this.current.url = this.current.url || 'http://your_server:8080/hawkular/metrics'
}
}

HawkularConfigCtrl.templateUrl = 'partials/config.html';
71 changes: 45 additions & 26 deletions src/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,40 @@ export class HawkularDatasource {
this.type = instanceSettings.type;
this.url = instanceSettings.url;
this.name = instanceSettings.name;
this.q = $q;
this.backendSrv = backendSrv;
this.headers = {
'Content-Type': 'application/json',
'Hawkular-Tenant': instanceSettings.jsonData.tenant
};
this.tenant = instanceSettings.jsonData.tenant;
this.isTenantPerQuery = instanceSettings.jsonData.isTenantPerQuery;
this.authorization = null;
if (typeof instanceSettings.basicAuth === 'string' && instanceSettings.basicAuth.length > 0) {
this.headers['Authorization'] = instanceSettings.basicAuth;
this.authorization = instanceSettings.basicAuth;
} else if (typeof instanceSettings.jsonData.token === 'string' && instanceSettings.jsonData.token.length > 0) {
this.headers['Authorization'] = 'Bearer ' + instanceSettings.jsonData.token;
this.authorization = 'Bearer ' + instanceSettings.jsonData.token;
}
this.q = $q;
this.backendSrv = backendSrv;
this.typeResources = {
"gauge": "gauges",
"counter": "counters",
"availability": "availability"
'gauge': 'gauges',
'counter': 'counters',
'availability': 'availability'
};
this.variablesHelper = new VariablesHelper(templateSrv);
this.capabilitiesPromise = this.queryVersion().then(version => new Capabilities(version));
this.queryProcessor = new QueryProcessor($q, backendSrv, this.variablesHelper, this.capabilitiesPromise, this.url, this.headers, this.typeResources);
this.queryProcessor = new QueryProcessor($q, backendSrv, this.variablesHelper, this.capabilitiesPromise, this.url,
this.getHeaders.bind(this), this.typeResources);
}

getHeaders(tenant) {
const headers = {
'Content-Type': 'application/json'
}
if (tenant && this.isTenantPerQuery) {
headers['Hawkular-Tenant'] = tenant;
} else {
headers['Hawkular-Tenant'] = this.tenant;
}
if (this.authorization) {
headers['Authorization'] = this.authorization;
}
return headers;
}

query(options) {
Expand Down Expand Up @@ -74,31 +89,35 @@ export class HawkularDatasource {
}

testDatasource() {
// If tenants is unknown at this point (when having per-query tenants)
// We do a more basic check to status endpoint
// Else, it's full connectivity with tenant check
const endpoint = this.isTenantPerQuery ? '/status' : '/metrics';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that the status endpoint might be reachable even without the proper credentials

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I can't see other way round. In openshift for example authentication is closely linked to the tenant, so it cannot be checked anyway at this step.

return this.backendSrv.datasourceRequest({
url: this.url + '/metrics',
url: this.url + endpoint,
method: 'GET',
headers: this.headers
headers: this.getHeaders()
}).then(response => {
if (response.status === 200 || response.status === 204) {
return { status: "success", message: "Data source is working", title: "Success" };
return { status: 'success', message: 'Data source is working', title: 'Success' };
} else {
return { status: "error", message: "Connection failed (" + response.status + ")", title: "Error" };
return { status: 'error', message: 'Connection failed (' + response.status + ')', title: 'Error' };
}
});
}

annotationQuery(options) {
const metricIds = this.variablesHelper.resolve(options.annotation.query, options);
return this.backendSrv.datasourceRequest({
url: this.url + '/strings/raw/query',
url: `${this.url}/${options.annotation.type}/raw/query`,
data: {
start: options.range.from.valueOf(),
end: options.range.to.valueOf(),
order: 'ASC',
ids: metricIds
},
method: 'POST',
headers: this.headers
headers: this.getHeaders(options.annotation.tenant)
}).then(response => response.status == 200 ? response.data : [])
.then(metrics => {
let allAnnotations = [];
Expand Down Expand Up @@ -141,7 +160,7 @@ export class HawkularDatasource {
return this.backendSrv.datasourceRequest({
url: url,
method: 'GET',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => response.status == 200 ? response.data : [])
.then(result => {
return result.map(m => m.id)
Expand All @@ -152,25 +171,25 @@ export class HawkularDatasource {
});
}

suggestTags(type, key) {
suggestTags(target, key) {
if (!key) {
return this.q.when([]);
}
return this.backendSrv.datasourceRequest({
url: this.url + '/' + this.typeResources[type] + '/tags/' + key + ':*',
url: this.url + '/' + this.typeResources[target.type] + '/tags/' + key + ':*',
method: 'GET',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(result => result.data.hasOwnProperty(key) ? result.data[key] : [])
.then(tags => tags.map(tag => {
return {text: tag, value: tag};
}));
}

suggestTagKeys(type) {
suggestTagKeys(target) {
return this.backendSrv.datasourceRequest({
url: this.url + '/metrics/tags',
method: 'GET',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => response.status == 200 ? response.data : [])
.then(result => result.map(key => ({text: key, value: key})));
}
Expand All @@ -190,7 +209,7 @@ export class HawkularDatasource {
return this.runWithResolvedVariables(params, p => this.backendSrv.datasourceRequest({
url: this.url + '/metrics' + p,
method: 'GET',
headers: this.headers
headers: this.getHeaders()
}).then(result => {
return _.map(result.data, metric => {
return {text: metric.id, value: metric.id};
Expand All @@ -202,7 +221,7 @@ export class HawkularDatasource {
return this.runWithResolvedVariables(pattern, p => this.backendSrv.datasourceRequest({
url: this.url + '/metrics/tags/' + p,
method: 'GET',
headers: this.headers
headers: this.getHeaders()
}).then(result => {
let flatTags = [];
if (result.data) {
Expand Down
4 changes: 1 addition & 3 deletions src/module.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {HawkularDatasource} from './datasource';
import {HawkularDatasourceQueryCtrl} from './query_ctrl';

class HawkularConfigCtrl {}
HawkularConfigCtrl.templateUrl = 'partials/config.html';
import {HawkularConfigCtrl} from './config_ctrl';

class HawkularQueryOptionsCtrl {}
HawkularQueryOptionsCtrl.templateUrl = 'partials/query.options.html';
Expand Down
19 changes: 17 additions & 2 deletions src/partials/annotations.editor.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@

<h5 class="section-heading">Query</h5>
<div class="gf-form-group">
<div class="gf-form-inline">
<div class="gf-form">
<span class="gf-form-label">
Tenant&nbsp;
<i class="fa fa-question-circle" bs-tooltip="'Use for per-query tenant configuration only, see datasource configuration'" data-placement="right"></i>
</span>
<input type="text" class="gf-form-input max-width-10" ng-model="ctrl.annotation.tenant" placeholder="">
</div>
<div class="gf-form">
<span class="gf-form-label">Type</span>
<select class="gf-form-input max-width-10" ng-model="ctrl.annotation.type">
<option value="availability">Availability</option>
<option value="strings">String</option>
</select>
</div>
</div>
<div class="gf-form">
<span class="gf-form-label">Metric name</span>
<input type="text" class="gf-form-input" ng-model="ctrl.annotation.query" placeholder="">
</div>
</div>
3 changes: 2 additions & 1 deletion src/partials/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<h3 class="page-heading">Hawkular settings</h3>

<div class="gf-form-group">
<div class="gf-form-inline">
<gf-form-switch class="gf-form" label="Tenant per query" checked="ctrl.current.jsonData.isTenantPerQuery" switch-class="max-width-6"></gf-form-switch>
<div class="gf-form-inline" ng-if="!ctrl.current.jsonData.isTenantPerQuery">
<div class="gf-form max-width-30">
<span class="gf-form-label width-7">Tenant</span>
<input type="text" class="gf-form-input" ng-model="ctrl.current.jsonData.tenant" placeholder="default" required>
Expand Down
2 changes: 2 additions & 0 deletions src/partials/query.editor.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<query-editor-row query-ctrl="ctrl" class="generic-datasource-query-row" has-text-edit-mode="true">
<div class="gf-form-inline">
<div class="gf-form">
<label ng-if="ctrl.datasource.isTenantPerQuery" class="gf-form-label query-keyword fix-query-keyword">Tenant</label>
<input ng-if="ctrl.datasource.isTenantPerQuery" type="text" class="gf-form-input" ng-model="ctrl.target.tenant" placeholder="default" required>
<select class="gf-form-input" ng-model="ctrl.target.type" ng-change="ctrl.onChangeInternal()">
<option ng-repeat="type in ctrl.metricTypes" value="{{type.value}}">{{type.text}}</option>
</select>
Expand Down
2 changes: 1 addition & 1 deletion src/partials/query.options.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<section class="grafana-metric-options" >
<section class="grafana-metric-options">
<div class="gf-form">
</div>
</section>
16 changes: 8 additions & 8 deletions src/queryProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const STATS_BUCKETS = 60;

export class QueryProcessor {

constructor(q, backendSrv, variablesHelper, capabilities, url, headers, typeResources) {
constructor(q, backendSrv, variablesHelper, capabilities, url, getHeaders, typeResources) {
this.q = q;
this.backendSrv = backendSrv;
this.variablesHelper = variablesHelper;
this.capabilities = capabilities;
this.url = url;
this.headers = headers;
this.getHeaders = getHeaders;
this.typeResources = typeResources;
this.numericMapping = point => [point.value, point.timestamp];
this.availMapping = point => [point.value == 'up' ? 1 : 0, point.timestamp];
Expand Down Expand Up @@ -87,7 +87,7 @@ export class QueryProcessor {
url: url,
data: postData,
method: 'POST',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => this.processRawResponse(target, response.status == 200 ? response.data : []));
}

Expand All @@ -106,7 +106,7 @@ export class QueryProcessor {
end: range.to.valueOf()
},
method: 'GET',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => this.processRawResponseLegacy(target, metric, response.status == 200 ? response.data : []));
}));
}
Expand Down Expand Up @@ -168,7 +168,7 @@ export class QueryProcessor {
url: url,
data: postData,
method: 'POST',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => this.processStatsResponse(target, response.status == 200 ? response.data : []));
}

Expand Down Expand Up @@ -211,7 +211,7 @@ export class QueryProcessor {
url: url,
data: postData,
method: 'POST',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => this.processUnmergedStatsResponse(target, response.status == 200 ? response.data : []));
}

Expand Down Expand Up @@ -285,7 +285,7 @@ export class QueryProcessor {
url: url,
data: postData,
method: 'POST',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => this.processSingleStatResponse(target, fnBucket, response.status == 200 ? response.data : []));
}

Expand All @@ -312,7 +312,7 @@ export class QueryProcessor {
url: url,
data: postData,
method: 'POST',
headers: this.headers
headers: this.getHeaders(target.tenant)
}).then(response => this.processSingleStatLiveResponse(target, response.status == 200 ? response.data : []));
}

Expand Down
Loading