Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cloud-Computing_2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Luzzi
Cloud-Computing_2022
Commits
d0888340
Commit
d0888340
authored
2 years ago
by
Christopher Luzzi
Browse files
Options
Downloads
Patches
Plain Diff
code commented
parent
6ce80864
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
application/Dockerfile
+9
-0
9 additions, 0 deletions
application/Dockerfile
application/metrics.js
+5
-2
5 additions, 2 deletions
application/metrics.js
application/server.js
+2
-0
2 additions, 0 deletions
application/server.js
with
16 additions
and
2 deletions
application/Dockerfile
+
9
−
0
View file @
d0888340
# Node alpine is used as base for the image.
# The node alpine is the lightweight version of node, that way the image stays as small in size as possible
FROM
node:lts-alpine
FROM
node:lts-alpine
ENV
NODE_ENV=production
ENV
NODE_ENV=production
# Here the Working directory is created and navigated to.
# Into this Working directory we copy the code from the local maschine to the container.
WORKDIR
/usr/src/CLOUD-COMPUTING_2022
WORKDIR
/usr/src/CLOUD-COMPUTING_2022
# Here we copy the package*.json and then install all the dependencies.
# The Maven folder will not be copied to the Container as its on the dockerignore list.
# That way we ensure all dependencies are correctly set inside the container
COPY
["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
COPY
["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN
npm
install
--production
--silent
&&
mv
node_modules ../
RUN
npm
install
--production
--silent
&&
mv
node_modules ../
# After installing the dependencies we copy the actual code into the container and expose port 3000.
COPY
. .
COPY
. .
EXPOSE
3000
EXPOSE
3000
RUN
chown
-R
node /usr/src/CLOUD-COMPUTING_2022
RUN
chown
-R
node /usr/src/CLOUD-COMPUTING_2022
USER
node
USER
node
# When running the Image in a container the application will start
CMD
["node", "server.js"]
CMD
["node", "server.js"]
This diff is collapsed.
Click to expand it.
application/metrics.js
+
5
−
2
View file @
d0888340
const
client
=
require
(
'
prom-client
'
)
const
client
=
require
(
'
prom-client
'
)
//creating a registry where the metrics are collected
const
register
=
new
client
.
Registry
()
const
register
=
new
client
.
Registry
()
register
.
setDefaultLabels
({
register
.
setDefaultLabels
({
app
:
'
cloud-computing-chat
'
app
:
'
cloud-computing-chat
'
})
})
//this enables collecting the defaul metrics the prom-client for node offers
client
.
collectDefaultMetrics
({
register
})
client
.
collectDefaultMetrics
({
register
})
//here the metrics are defined
const
msgSent
=
new
client
.
Counter
({
const
msgSent
=
new
client
.
Counter
({
name
:
'
msgSent
'
,
name
:
'
msgSent
'
,
help
:
'
total number of messages sent
'
help
:
'
total number of messages sent
'
...
@@ -35,13 +37,14 @@ const failedLogins = new client.Counter({
...
@@ -35,13 +37,14 @@ const failedLogins = new client.Counter({
help
:
'
number of failed logins
'
help
:
'
number of failed logins
'
})
})
//this registers the metrics in the registry we have created
register
.
registerMetric
(
registercounter
)
register
.
registerMetric
(
registercounter
)
register
.
registerMetric
(
msgSent
)
register
.
registerMetric
(
msgSent
)
register
.
registerMetric
(
msgLength
)
register
.
registerMetric
(
msgLength
)
register
.
registerMetric
(
onlineUser
)
register
.
registerMetric
(
onlineUser
)
register
.
registerMetric
(
failedLogins
)
register
.
registerMetric
(
failedLogins
)
//exports the registry and the metrics to apply changes to metrics. e.g. inc the count for msgSent
exports
.
register
=
register
exports
.
register
=
register
exports
.
msgSent
=
msgSent
exports
.
msgSent
=
msgSent
exports
.
msgLength
=
msgLength
exports
.
msgLength
=
msgLength
...
...
This diff is collapsed.
Click to expand it.
application/server.js
+
2
−
0
View file @
d0888340
...
@@ -88,6 +88,7 @@ app.get('/index', (req, res) => {
...
@@ -88,6 +88,7 @@ app.get('/index', (req, res) => {
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
io
.
on
(
'
connection
'
,
(
socket
)
=>
{
socket
.
on
(
'
chat message
'
,
(
msg
)
=>
{
socket
.
on
(
'
chat message
'
,
(
msg
)
=>
{
Metrics
.
msgSent
.
inc
()
Metrics
.
msgSent
.
inc
()
//this returns the count of words for every msg sent
var
length
=
msg
.
split
(
'
'
).
filter
(
word
=>
word
!==
''
).
length
var
length
=
msg
.
split
(
'
'
).
filter
(
word
=>
word
!==
''
).
length
Metrics
.
msgLength
.
observe
(
length
)
Metrics
.
msgLength
.
observe
(
length
)
io
.
emit
(
'
chat message
'
,
msg
);
io
.
emit
(
'
chat message
'
,
msg
);
...
@@ -110,6 +111,7 @@ require(path.join(__dirname,'/app/routes/auth.routes'))(app);
...
@@ -110,6 +111,7 @@ require(path.join(__dirname,'/app/routes/auth.routes'))(app);
require
(
path
.
join
(
__dirname
,
'
/app/routes/user.routes
'
))(
app
);
require
(
path
.
join
(
__dirname
,
'
/app/routes/user.routes
'
))(
app
);
//exporting the collected metrics to prometheus metrics endpoint
app
.
get
(
'
/metrics
'
,
async
(
req
,
res
)
=>
{
app
.
get
(
'
/metrics
'
,
async
(
req
,
res
)
=>
{
res
.
set
(
'
Content-Type
'
,
Metrics
.
register
.
contentType
)
res
.
set
(
'
Content-Type
'
,
Metrics
.
register
.
contentType
)
res
.
end
(
await
Metrics
.
register
.
metrics
())
res
.
end
(
await
Metrics
.
register
.
metrics
())
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment