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

Support for OpenObserve ( Alternative to Elasticsearch) #292

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
152 changes: 86 additions & 66 deletions examples/spectest/spectest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
"use strict";

var http = require('http');
var path = require('path');
var debug = require('debug')('sws:spectest');
var http = require("http");
var path = require("path");
var debug = require("debug")("sws:spectest");

//http.globalAgent.keepAlive = true;

// Prometheus Client
const promClient = require('prom-client');
const promClient = require("prom-client");
const collectDefaultMetrics = promClient.collectDefaultMetrics;
// Probe every 1 second
collectDefaultMetrics({ timeout: 1000 });
Expand All @@ -16,81 +16,78 @@ collectDefaultMetrics({ timeout: 1000 });
var server = null;

// Express and middlewares
var express = require('express');
var expressBodyParser = require('body-parser');
var express = require("express");
var expressBodyParser = require("body-parser");

var swaggerParser = require('swagger-parser');
var swaggerParser = require("swagger-parser");

var swStats = require('../../lib'); // require('swagger-stats');
var swStats = require("../../lib"); // require('swagger-stats');

var app = module.exports = express();
var app = (module.exports = express());
app.use(expressBodyParser.json());
app.use(expressBodyParser.urlencoded({ extended: true }));

// JSON formatting
app.set('json spaces', 2);
app.set('json replacer', null);
app.set("json spaces", 2);
app.set("json replacer", null);

// all environments
app.set('port', process.env.PORT || 3040);
app.set("port", process.env.PORT || 3040);

// Suppress cache on the GET API responses
app.disable('etag');
app.disable("etag");

app.get('/', function(req,res) {
res.redirect('/swagger-stats/');
app.get("/", function (req, res) {
res.redirect("/swagger-stats/");
});

// Return Prometheus metrics from prom-client
app.get('/metrics', function(req,res) {
res.status(200).set('Content-Type', 'text/plain');
Promise.resolve(promClient.register.metrics()).then( (x) => {
app.get("/metrics", function (req, res) {
res.status(200).set("Content-Type", "text/plain");
Promise.resolve(promClient.register.metrics()).then((x) => {
res.end(x);
});
});


// Testing validation of 3rd-party API spec
var swaggerSpec = null;
var parser = new swaggerParser();

//var specLocation = 'petstore3.yaml';
var specLocation = 'petstore.yaml';
var specLocation = "petstore.yaml";

if( process.env.SWS_SPECTEST_URL ){
if (process.env.SWS_SPECTEST_URL) {
specLocation = process.env.SWS_SPECTEST_URL;
}

var tlBucket = 60000;
if( process.env.SWS_SPECTEST_TIMEBUCKET ){
if (process.env.SWS_SPECTEST_TIMEBUCKET) {
tlBucket = parseInt(process.env.SWS_SPECTEST_TIMEBUCKET);
}

debug('Loading Swagger Spec from ' + specLocation );
debug("Loading Swagger Spec from " + specLocation);

parser.validate(specLocation,function(err, api) {
parser.validate(specLocation, function (err, api) {
if (!err) {

debug('Success validating swagger file!');
debug("Success validating swagger file!");
swaggerSpec = api;

var swsOptions = {
name: 'swagger-stats-spectest',
version: '0.99.7',
name: "swagger-stats-spectest",
version: "0.99.7",
hostname: "hostname",
ip: "127.0.0.1",
timelineBucketDuration: tlBucket,
swaggerSpec:swaggerSpec,
swaggerSpec: swaggerSpec,
//basePath: '/api',
uriPath: '/swagger-stats',
uriPath: "/swagger-stats",
durationBuckets: [10, 25, 50, 100, 200],
requestSizeBuckets: [10, 25, 50, 100, 200],
responseSizeBuckets: [10, 25, 50, 100, 200],
apdexThreshold: 25,
onResponseFinish: function(req,res,rrr){

onResponseFinish: function (req, res, rrr) {
// You can remove non-needed or private attributes from Request Response Record
delete rrr.http.request.headers['user-agent'];
delete rrr.http.request.headers["user-agent"];

// You can also extend Request Response Record with custom attributes

Expand All @@ -100,7 +97,7 @@ parser.validate(specLocation,function(err, api) {
test2: "test2",
test3: 10,
test4: true,
test5: {prop:"value"}
test5: { prop: "value" },
};

// All custom properties under attrsint will be casted to numeric and indexed in ElasticSearch as long
Expand All @@ -109,23 +106,42 @@ parser.validate(specLocation,function(err, api) {
numvalue2: "100",
numvalue3: false,
numvalue4: "",
numvalue5: {prop:"value"}
numvalue5: { prop: "value" },
};

debug('onResponseFinish: %s', JSON.stringify(rrr));
}

debug("onResponseFinish: %s", JSON.stringify(rrr));
},
};

// Enable Elasticsearch if specified
if( process.env.SWS_ELASTIC ){
if (process.env.SWS_ELASTIC) {
swsOptions.elasticsearch = process.env.SWS_ELASTIC;
}

if( process.env.SWS_ELASTIC_INDEX_PREFIX ){
swsOptions.elasticsearchIndexPrefix = process.env.SWS_ELASTIC_INDEX_PREFIX;
if (process.env.SWS_ELASTIC_INDEX_PREFIX) {
swsOptions.elasticsearchIndexPrefix =
process.env.SWS_ELASTIC_INDEX_PREFIX;
}
if (process.env.SWS_OPENOBSERVE) {
debug("Enabling OpenObserve: " + process.env.SWS_OPENOBSERVE);
swsOptions.openobserve = process.env.SWS_OPENOBSERVE;
}
if (process.env.SWS_OPENOBSERVE_INDEX_PREFIX) {
debug(
"Enabling OpenObserve Index Prefix: " +
process.env.SWS_OPENOBSERVE_INDEX_PREFIX
);
swsOptions.openobserveIndexPrefix =
process.env.SWS_OPENOBSERVE_INDEX_PREFIX;
}
if (process.env.SWS_OPENOBSERVE_USERNAME) {
swsOptions.openobserveUsername =
process.env.SWS_OPENOBSERVE_USERNAME;
}
if (process.env.SWS_OPENOBSERVE_PASSWORD) {
swsOptions.openobservePassword =
process.env.SWS_OPENOBSERVE_PASSWORD;
}


// Enable swagger-stats middleware with options
app.use(swStats.getMiddleware(swsOptions));
Expand All @@ -135,10 +151,15 @@ parser.validate(specLocation,function(err, api) {

// Setup server
server = http.createServer(app);
server.listen(app.get('port'));
server.listen(app.get("port"));
server.keepAliveTimeout = 61 * 1000;
server.headersTimeout = 65 * 1000;
debug('Server started on port ' + app.get('port') + ' http://localhost:'+app.get('port'));
debug(
"Server started on port " +
app.get("port") +
" http://localhost:" +
app.get("port")
);
}
});

Expand All @@ -149,43 +170,42 @@ parser.validate(specLocation,function(err, api) {
// delay:<delay to respond>,
// payloadsize:<size of payload JSON to generate>
// }
function mockApiImplementation(req,res,next){

function mockApiImplementation(req, res, next) {
var code = 500;
var message = "MOCK API RESPONSE";
var delay = 0;
var payloadsize = 0;

// get header
var hdrSwsRes = req.header('x-sws-res');
var hdrSwsRes = req.header("x-sws-res");

if(typeof hdrSwsRes !== 'undefined'){
if (typeof hdrSwsRes !== "undefined") {
var swsRes = JSON.parse(hdrSwsRes);
if( 'code' in swsRes ) code = swsRes.code;
if( 'message' in swsRes ) message = swsRes.message;
if( 'delay' in swsRes ) delay = swsRes.delay;
if( 'payloadsize' in swsRes ) payloadsize = swsRes.payloadsize;
if ("code" in swsRes) code = swsRes.code;
if ("message" in swsRes) message = swsRes.message;
if ("delay" in swsRes) delay = swsRes.delay;
if ("payloadsize" in swsRes) payloadsize = swsRes.payloadsize;
}

if( delay > 0 ){
setTimeout(function(){
mockApiSendResponse(res,code,message,payloadsize);
},delay);
}else{
mockApiSendResponse(res,code,message,payloadsize);
if (delay > 0) {
setTimeout(function () {
mockApiSendResponse(res, code, message, payloadsize);
}, delay);
} else {
mockApiSendResponse(res, code, message, payloadsize);
}
}

function mockApiSendResponse(res,code,message,payloadsize){
if(payloadsize<=0){
function mockApiSendResponse(res, code, message, payloadsize) {
if (payloadsize <= 0) {
res.status(code).send(message);
}else{
} else {
// generate dummy payload of approximate size
var dummyPayload = [];
var adjSize = payloadsize-4;
if(adjSize<=0) adjSize = 1;
var str = '';
for(var i=0;i<adjSize;i++) str += 'a';
var adjSize = payloadsize - 4;
if (adjSize <= 0) adjSize = 1;
var str = "";
for (var i = 0; i < adjSize; i++) str += "a";
dummyPayload.push(str);
res.status(code).json(dummyPayload);
}
Expand Down
Loading