Skip to content

Commit

Permalink
eslint configuration for getting-started examples (#2297)
Browse files Browse the repository at this point in the history
* chore: add eslint config to getting-started

* chore: apply lint on getting-started

* chore: fix strict rule in getting-started lint config

* chore: import examples eslintrc for getting-started eslint config

* chore: rearrage lint scripts in package.json

* chore: restore removed require from getting-started example

* chore: remove duplicate require in getting-started
  • Loading branch information
alisabzevari authored Aug 23, 2021
1 parent 78a78c0 commit c62ea11
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 117 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions getting-started/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable global-require */
/* eslint-disable strict */

module.exports = {
...require('../examples/.eslintrc.json'),
};
24 changes: 12 additions & 12 deletions getting-started/example/app.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
"use strict";
'use strict';

const PORT = process.env.PORT || "8080";
const PORT = process.env.PORT || '8080';

const express = require("express");
const axios = require("axios");
const express = require('express');
const axios = require('axios');

const app = express();

app.get("/", (req, res) => {
app.get('/', (req, res) => {
axios
.get(`http://localhost:${PORT}/middle-tier`)
.then(() => axios.get(`http://localhost:${PORT}/middle-tier`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/middle-tier", (req, res) => {
app.get('/middle-tier', (req, res) => {
axios
.get(`http://localhost:${PORT}/backend`)
.then(() => axios.get(`http://localhost:${PORT}/backend`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/backend", (req, res) => {
res.send("Hello from the backend");
app.get('/backend', (req, res) => {
res.send('Hello from the backend');
});

app.listen(parseInt(PORT, 10), () => {
Expand Down
27 changes: 14 additions & 13 deletions getting-started/monitored-example/app.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
"use strict";
'use strict';

const PORT = process.env.PORT || "8080";
const PORT = process.env.PORT || '8080';

const express = require("express");
const axios = require("axios");
const express = require('express');
const axios = require('axios');

const { countAllRequests } = require('./monitoring');

const { countAllRequests } = require("./monitoring");
const app = express();
app.use(countAllRequests());

app.get("/", (req, res) => {
app.get('/', (req, res) => {
axios
.get(`http://localhost:${PORT}/middle-tier`)
.then(() => axios.get(`http://localhost:${PORT}/middle-tier`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/middle-tier", (req, res) => {
app.get('/middle-tier', (req, res) => {
axios
.get(`http://localhost:${PORT}/backend`)
.then(() => axios.get(`http://localhost:${PORT}/backend`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/backend", (req, res) => {
res.send("Hello from the backend");
app.get('/backend', (req, res) => {
res.send('Hello from the backend');
});

app.listen(parseInt(PORT, 10), () => {
Expand Down
45 changes: 21 additions & 24 deletions getting-started/monitored-example/monitoring.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use strict";
'use strict';

const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');
const prometheusPort = PrometheusExporter.DEFAULT_OPTIONS.port
const prometheusEndpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint

const prometheusPort = PrometheusExporter.DEFAULT_OPTIONS.port;
const prometheusEndpoint = PrometheusExporter.DEFAULT_OPTIONS.endpoint;

const exporter = new PrometheusExporter(
{
startServer: true,
Expand All @@ -16,28 +16,25 @@ const exporter = new PrometheusExporter(
);
},
);

const meter = new MeterProvider({
exporter,
interval: 1000,
}).getMeter('your-meter-name');
const requestCount = meter.createCounter("requests", {
description: "Count all incoming requests"

const requestCount = meter.createCounter('requests', {
description: 'Count all incoming requests',
});

const boundInstruments = new Map();

module.exports.countAllRequests = () => {
return (req, res, next) => {
if (!boundInstruments.has(req.path)) {
const labels = { route: req.path };
const boundCounter = requestCount.bind(labels);
boundInstruments.set(req.path, boundCounter);
}

boundInstruments.get(req.path).add(1);
next();
};
};

module.exports.countAllRequests = () => (req, res, next) => {
if (!boundInstruments.has(req.path)) {
const labels = { route: req.path };
const boundCounter = requestCount.bind(labels);
boundInstruments.set(req.path, boundCounter);
}

boundInstruments.get(req.path).add(1);
next();
};
24 changes: 12 additions & 12 deletions getting-started/traced-example/app.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
"use strict";
'use strict';

const PORT = process.env.PORT || "8080";
const PORT = process.env.PORT || '8080';

const express = require("express");
const axios = require("axios");
const express = require('express');
const axios = require('axios');

const app = express();

app.get("/", (req, res) => {
app.get('/', (req, res) => {
axios
.get(`http://localhost:${PORT}/middle-tier`)
.then(() => axios.get(`http://localhost:${PORT}/middle-tier`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/middle-tier", (req, res) => {
app.get('/middle-tier', (req, res) => {
axios
.get(`http://localhost:${PORT}/backend`)
.then(() => axios.get(`http://localhost:${PORT}/backend`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/backend", (req, res) => {
res.send("Hello from the backend");
app.get('/backend', (req, res) => {
res.send('Hello from the backend');
});

app.listen(parseInt(PORT, 10), () => {
Expand Down
18 changes: 9 additions & 9 deletions getting-started/traced-example/tracing.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
"use strict";
'use strict';

const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin");
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');

const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: "getting-started",
})
[SemanticResourceAttributes.SERVICE_NAME]: 'getting-started',
}),
});

provider.addSpanProcessor(
Expand All @@ -21,8 +21,8 @@ provider.addSpanProcessor(
// If you are running your tracing backend on another host,
// you can point to it using the `url` parameter of the
// exporter config.
})
)
}),
),
);

provider.register();
Expand All @@ -35,4 +35,4 @@ registerInstrumentations({
],
});

console.log("tracing initialized");
console.log('tracing initialized');
16 changes: 16 additions & 0 deletions getting-started/ts-example/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"plugins": ["@typescript-eslint", "node"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"rules": {
"@typescript-eslint/no-var-requires": 0,
"import/prefer-default-export": "off",
"import/extensions": [
"error",
"ignorePackages",
{
"": "never"
}
]
}
}
22 changes: 11 additions & 11 deletions getting-started/ts-example/example/app.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import * as express from "express";
import axios from "axios";
import * as express from 'express';
import axios from 'axios';

const PORT: string = process.env.PORT || "8080";
const PORT: string = process.env.PORT || '8080';

const app = express();

app.get("/", (req, res) => {
app.get('/', (req, res) => {
axios
.get(`http://localhost:${PORT}/middle-tier`)
.then(() => axios.get(`http://localhost:${PORT}/middle-tier`))
.then(response => {
.then((response) => {
res.send(response.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/middle-tier", (req, res) => {
app.get('/middle-tier', (req, res) => {
axios
.get(`http://localhost:${PORT}/backend`)
.then(() => axios.get(`http://localhost:${PORT}/backend`))
.then(response => {
.then((response) => {
res.send(response.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/backend", (req, res) => {
res.send("Hello from the backend");
app.get('/backend', (req, res) => {
res.send('Hello from the backend');
});

app.listen(parseInt(PORT, 10), () => {
Expand Down
24 changes: 12 additions & 12 deletions getting-started/ts-example/monitored-example/app.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import * as express from "express";
import axios from "axios";

const PORT: string = process.env.PORT || "8080";
import * as express from 'express';
import axios from 'axios';

import { countAllRequests } from './monitoring';

const PORT: string = process.env.PORT || '8080';
const app = express();
app.use(countAllRequests());

app.get("/", (req, res) => {
app.get('/', (req, res) => {
axios
.get(`http://localhost:${PORT}/middle-tier`)
.then(() => axios.get(`http://localhost:${PORT}/middle-tier`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/middle-tier", (req, res) => {
app.get('/middle-tier', (req, res) => {
axios
.get(`http://localhost:${PORT}/backend`)
.then(() => axios.get(`http://localhost:${PORT}/backend`))
.then(result => {
.then((result) => {
res.send(result.data);
})
.catch(err => {
.catch((err) => {
console.error(err);
res.status(500).send();
});
});

app.get("/backend", (req, res) => {
res.send("Hello from the backend");
app.get('/backend', (req, res) => {
res.send('Hello from the backend');
});

app.listen(parseInt(PORT, 10), () => {
Expand Down
Loading

0 comments on commit c62ea11

Please sign in to comment.