Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 2f5d8be

Browse files
DazWilkinmayurkale22
authored andcommitted
Minor changes to Stackdriver example (#289)
* Minor changes to Stackdriver example * A Promethues example that mirrors the existing Stackdriver example * Fixed typo * README for examples * Corrected port reference (should be 9464) * Remove vestigial Stackdriver dependency * Fixed installation instructions. Added Dockerfiles and instructions. * Replaced jshint with gts * Migrated Dockerfiles node:8 --> node:10 * Added Debugging info to README; Stackdriver example now uses env vars * Remove redundant (switched to gts) jshint inline config
1 parent f1983b9 commit 2f5d8be

6 files changed

Lines changed: 407 additions & 15 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:10
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY package.json .
6+
RUN npm install
7+
8+
COPY prometheus.js .
9+
COPY test.txt .
10+
11+
EXPOSE 9464
12+
13+
ENTRYPOINT ["node","prometheus.js"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:10
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY package.json .
6+
RUN npm install
7+
8+
COPY stackdriver.js .
9+
COPY test.txt .
10+
11+
ENTRYPOINT ["node","stackdriver.js"]

examples/stats/exporter/README.md

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# Examples
2+
3+
You will need to:
4+
5+
1. Clone this repo and install dependent packages
6+
7+
```bash
8+
WORK=[[YOUR-WORKING-DIRECTORY]]
9+
cd ${WORK}
10+
11+
git clone https://github.com/census-instrumentation/opencensus-node
12+
13+
cd opencensus-node
14+
npm install
15+
```
16+
17+
## Stackdriver
18+
19+
The sample uses Application Default Credentials to authenticate with the Stackdriver service.
20+
21+
See https://developers.google.com/identity/protocols/application-default-credentials
22+
23+
You will need to:
24+
25+
1. Create a Google Cloud Platform project and Enable Billing
26+
1. Create a Stackdriver workspace
27+
1. Create a service account and ensure it is permitted to write Stackdriver metrics
28+
1. Optional: Visual Studio Code debugging
29+
1. Run the sample
30+
1. Observe the metrics
31+
32+
### Create Google Cloud Platform project and Enable Billing
33+
34+
```bash
35+
PROJECT=[[YOUR-PROJECT]]
36+
BILLING=[[YOUR-BILLING]]
37+
38+
gcloud projects create ${PROJECT}
39+
40+
gcloud beta billing projects link ${PROJECT} \
41+
--billing-account=${BILLING}
42+
```
43+
### Create Stackdriver workspace
44+
45+
```
46+
google-chrome console.cloud.google.com/monitoring/?project=${PROJECT}
47+
```
48+
49+
### Create Service Account with correct permissions
50+
51+
```bash
52+
ACCOUNT=[[YOUR-SERVICE-ACCOUNT]]
53+
ADDRESS=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com
54+
55+
WORK=[[YOUR-WORKING-DIRECTORY]]
56+
FILE="${WORK}/${ACCOUNT}.key.json"
57+
58+
gcloud iam service-accounts create ${ACCOUNT} \
59+
--display-name=${ACCOUNT} \
60+
--project=${PROJECT}
61+
62+
gcloud projects add-iam-policy-binding ${PROJECT} \
63+
--member=serviceAccount:${ADDRESS} \
64+
--role=roles/monitoring.metricWriter
65+
66+
gcloud iam service-accounts keys create ${FILE} \
67+
--iam-account=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com \
68+
--project=${PROJECT}
69+
```
70+
71+
### Optional: Visual Studio Code Debugging
72+
73+
If you wish to debug the code in Visual Studio Code, you will need to edit the `launch.json` file:
74+
75+
```json
76+
{
77+
"version": "0.2.0",
78+
"configurations": [
79+
...,
80+
{
81+
"type": "node",
82+
"request": "launch",
83+
"name": "Stackdriver",
84+
"program": "${workspaceFolder}/examples/stats/exporter/stackdriver.js",
85+
"env":{
86+
"GOOGLE_APPLICATION_CREDENTIALS":"[[Replace with the value of ${WORK}/${ACCOUNT}.key.json]]",
87+
"GOOGLE_PROJECT_ID": "[[Replace with the value of ${PROJECT}]]"
88+
}
89+
},
90+
...,
91+
]
92+
}
93+
```
94+
95+
### Run it
96+
97+
```bash
98+
GOOGLE_APPLICATION_CREDENTIALS=${FILE} node ./stackdriver.js
99+
```
100+
101+
Alternatively, if you'd prefer to use Docker:
102+
103+
```bash
104+
IMAGE=[[YOUR-STACKDRIVER-IMAGE]]
105+
TAG=[[YOUR-TAG]]
106+
107+
docker build \
108+
--tag=${IMAGE}:${TAG} \
109+
--file=./Dockerfile.stackdriver \
110+
.
111+
112+
docker run \
113+
--interactive \
114+
--tty \
115+
--mount=type=bind,src=${WORK}/${ACCOUNT}.key.json,dst=/keys/${ACCOUNT}.key.json \
116+
--env=GOOGLE_APPLICATION_CREDENTIALS=/keys/${ACCOUNT}.key.json \
117+
--env=GOOGLE_PROJECT_ID=${PROJECT} \
118+
${IMAGE}:${TAG}
119+
```
120+
121+
### Observe it
122+
123+
Look for the metrics (prefixed `repl/`) in metrics explorer:
124+
125+
```bash
126+
google-chrome console.cloud.google.com/monitoring/?project=${PROJECT}
127+
```
128+
129+
130+
## Prometheus
131+
132+
The Prometheus sample is standalone and runs without any additional setup.
133+
134+
If you'd like to see the metrics consumed by a Prometheus server, you will need to configure this.
135+
136+
### Optional: Visual Studio Code Debugging
137+
138+
If you wish to debug the code in Visual Studio Code, you will need to edit the `launch.json` file:
139+
140+
```json
141+
{
142+
"version": "0.2.0",
143+
"configurations": [
144+
...,
145+
{
146+
"type": "node",
147+
"request": "launch",
148+
"name": "Prometheus",
149+
"program": "${workspaceFolder}/examples/stats/exporter/prometheus.js"
150+
},
151+
...,
152+
]
153+
}
154+
```
155+
156+
157+
### Run it
158+
159+
```bash
160+
node ./prometheus.js
161+
```
162+
163+
Alternatively, if you'd prefer to use Docker:
164+
165+
```bash
166+
IMAGE=[[YOUR-PROMETHEUS-IMAGE]]
167+
TAG=[[YOUR-TAG]]
168+
169+
docker build \
170+
--tag=${IMAGE}:${TAG} \
171+
--file=./Dockerfile.prometheus \
172+
.
173+
174+
docker run \
175+
--interactive \
176+
--tty \
177+
--publish=9464:9464 \
178+
${IMAGE}:${TAG}
179+
```
180+
181+
### Observer it
182+
183+
You can observe the Prometheus Metrics Exporter that is created by the sample:
184+
```bash
185+
google-chrome http://localhost:9464/metrics
186+
```
187+
188+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@opencensus/examples",
3+
"version": "0.0.8",
4+
"description": "OpenCensus is a toolkit for collecting application performance and behavior data.",
5+
"repository": "census-instrumentation/opencensus-node",
6+
"scripts": {},
7+
"keywords": [
8+
"opencensus",
9+
"nodejs",
10+
"tracing",
11+
"profiling"
12+
],
13+
"author": "Google Inc.",
14+
"license": "Apache-2.0",
15+
"engines": {
16+
"node": ">=6.0"
17+
},
18+
"files": [],
19+
"publishConfig": {
20+
"access": "public"
21+
},
22+
"devDependencies": {
23+
"gts": "^0.9.0"
24+
},
25+
"dependencies": {
26+
"@opencensus/core": "^0.0.8",
27+
"@opencensus/exporter-prometheus": "^0.0.8",
28+
"@opencensus/exporter-stackdriver": "^0.0.8"
29+
}
30+
}

0 commit comments

Comments
 (0)