Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const app = express();
app.use(express.static("web_client"));
app.use(bodyParser.json());
// The project id must be provided for running in a local environment
const project = process.env.GOOGLE_CLOUD_PROJECT;
assert(typeof project !== 'undefined' && project,
"Please set environment variable GOOGLE_CLOUD_PROJECT to the project id.");
console.log(`Sending metrics data to project: ${project}`);
// OpenCensus setup
// [START web_client_monitoring_ocsetup]
const exporter = new StackdriverStatsExporter({projectId: project});
globalStats.registerExporter(exporter);
const mLatencyMs = globalStats.createMeasureDouble("webmetrics/latency",
MeasureUnit.MS,
"Latency related to page loading");
const mClickCount = globalStats.createMeasureInt64("webmetrics/click_count",
MeasureUnit.UNIT,
"Number of clicks");
const buckets = [0, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80,
100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000,
5000, 10000, 20000, 50000, 100000];
const tagPhase = { name: "phase" };
const tagClient = { name: "client" };
const latencyView = globalStats.createView(
"webmetrics/latency",
mLatencyMs,
AggregationType.DISTRIBUTION,
[tagPhase, tagClient],
// [START setup_exporter]
// Enable OpenCensus exporters to export metrics to Prometheus Monitoring.
const exporter = new PrometheusStatsExporter({
// Metrics will be exported on https://localhost:{port}/metrics
port: 9464,
startServer: true
});
// Pass the created exporter to global Stats
globalStats.registerExporter(exporter);
// [END setup_exporter]
// The latency in milliseconds
const mLatencyMs = globalStats.createMeasureDouble(
'repl/latency',
MeasureUnit.MS,
'The latency in milliseconds per REPL loop'
);
// Counts/groups the lengths of lines read in.
const mLineLengths = globalStats.createMeasureInt64(
'repl/line_lengths',
MeasureUnit.BYTE,
'The distribution of line lengths'
);
// Create a stream to read our file
const stream = fs.createReadStream('./test.txt');
// Create an interface to read and process our file line by line
const lineReader = readline.createInterface({ input: stream });
// GOOGLE_APPLICATION_CREDENTIALS are expected by a dependency of this code
// Not this code itself. Checking for existence here but not retaining (as not needed)
if (!projectId || !process.env.GOOGLE_APPLICATION_CREDENTIALS) {
throw Error('Unable to proceed without a Project ID');
}
const exporter = new StackdriverStatsExporter({ projectId: projectId });
// Pass the created exporter to global Stats
globalStats.registerExporter(exporter);
// [END setup_exporter]
// The latency in milliseconds
const mLatencyMs = globalStats.createMeasureDouble(
'repl/latency',
MeasureUnit.MS,
'The latency in milliseconds per REPL loop'
);
// Counts/groups the lengths of lines read in.
const mLineLengths = globalStats.createMeasureInt64(
'repl/line_lengths',
MeasureUnit.BYTE,
'The distribution of line lengths'
);
// Create a stream to read our file
const stream = fs.createReadStream('./test.txt');
// Create an interface to read and process our file line by line
const lineReader = readline.createInterface({ input: stream });
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const {globalStats, MeasureUnit, TagMap} = require('@opencensus/core');
const K1 = { name: 'k1' };
const K2 = { name: 'k2' };
const V1 = { value: 'v1' };
const V2 = { value: 'v2' };
const mLatencyMs = globalStats.createMeasureDouble(
'm1', MeasureUnit.MS, '1st test metric'
);
/** Main method. */
function main () {
const tags1 = new TagMap();
tags1.set(K1, V1);
globalStats.withTagContext(tags1, () => {
console.log('Enter Scope 1');
printMap('Add Tags', tags1.tags);
printMap('Current Tags == Default + tags1:',
globalStats.getCurrentTagContext().tags);
const tags2 = new TagMap();
tags2.set(K2, V2);
globalStats.withTagContext(tags2, () => {
DEFAULT_BYTES_DISTRIBUTION,
DEFAULT_MESSAGE_COUNT_DISTRIBUTION,
DEFAULT_MILLI_SECONDS_DISTRIBUTION,
} from './common-distributions';
/** {@link Measure} for number of messages received in each RPC. */
export const GRPC_SERVER_RECEIVED_MESSAGES_PER_RPC: Measure = globalStats.createMeasureInt64(
'grpc.io/server/received_messages_per_rpc',
MeasureUnit.UNIT,
'Number of messages received in each RPC. Has value 1 for non-streaming RPCs.'
);
/** {@link Measure} for gRPC server latency in milliseconds. */
export const GRPC_SERVER_SERVER_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/server/server_latency',
MeasureUnit.MS,
'Time between first byte of request received to last byte of response sent, or terminal error.'
);
/** {@link Measure} for number of messages sent in each RPC. */
export const GRPC_SERVER_SENT_MESSAGES_PER_RPC: Measure = globalStats.createMeasureInt64(
'grpc.io/server/sent_messages_per_rpc',
MeasureUnit.UNIT,
'Number of messages sent in each RPC. Has value 1 for non-streaming RPCs.'
);
/** {@link Measure} for total bytes received across all messages per RPC. */
export const GRPC_SERVER_RECEIVED_BYTES_PER_RPC: Measure = globalStats.createMeasureDouble(
'grpc.io/server/received_bytes_per_rpc',
MeasureUnit.BYTE,
'Total bytes received across all messages per RPC.'
);
* Content-Length header. This is uncompressed bytes. Responses with no
* body should record 0 for this value.
*/
export const HTTP_SERVER_SENT_BYTES: Measure = globalStats.createMeasureInt64(
'opencensus.io/http/server/sent_bytes',
MeasureUnit.BYTE,
'Server-side total bytes sent in response bodies (uncompressed)'
);
/**
* {@link Measure} for the server-side time between first byte of request
* headers received to last byte of response sent, or terminal error.
*/
export const HTTP_SERVER_LATENCY: Measure = globalStats.createMeasureDouble(
'opencensus.io/http/server/server_latency',
MeasureUnit.MS,
'Server-side time between first byte of request headers received to last byte of response sent, or terminal error'
);
/** {@link TagKey} for the value of the client-side HTTP host header. */
export const HTTP_CLIENT_HOST = {
name: 'http_client_host',
};
/** {@link TagKey} for the value of the server-side HTTP host header. */
export const HTTP_SERVER_HOST = {
name: 'http_server_host',
};
/**
* {@link TagKey} for the numeric client-side HTTP response status code (e.g.
* 200, 404, 500). If a transport error occurred and no status code was read,
'grpc.io/client/received_bytes_per_rpc',
MeasureUnit.BYTE,
'Total bytes received across all response messages per RPC.'
);
/** {@link Measure} for gRPC client roundtrip latency in milliseconds. */
export const GRPC_CLIENT_ROUNDTRIP_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/client/roundtrip_latency',
MeasureUnit.MS,
'Time between first byte of request sent to last byte of response received, or terminal error.'
);
/** {@link Measure} for gRPC server latency in milliseconds. */
export const GRPC_CLIENT_SERVER_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/client/server_latency',
MeasureUnit.MS,
'Propagated from the server and should have the same value as "grpc.io/server/latency'
);
/**
* Tag key that represents a client gRPC method.
*
* {@link #GRPC_CLIENT_METHOD} is set when an outgoing request starts and is
* available in all the recorded metrics.
*/
export const GRPC_CLIENT_METHOD = {
name: 'grpc_client_method',
};
/**
* Tag key that represents a client gRPC canonical status. Refer to
* https://github.com/grpc/grpc/blob/master/doc/statuscodes.md.
'Number of response messages received per RPC (always 1 for non-streaming RPCs).'
);
/**
* {@link Measure} for total bytes received across all response messages per RPC
*/
export const GRPC_CLIENT_RECEIVED_BYTES_PER_RPC: Measure = globalStats.createMeasureInt64(
'grpc.io/client/received_bytes_per_rpc',
MeasureUnit.BYTE,
'Total bytes received across all response messages per RPC.'
);
/** {@link Measure} for gRPC client roundtrip latency in milliseconds. */
export const GRPC_CLIENT_ROUNDTRIP_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/client/roundtrip_latency',
MeasureUnit.MS,
'Time between first byte of request sent to last byte of response received, or terminal error.'
);
/** {@link Measure} for gRPC server latency in milliseconds. */
export const GRPC_CLIENT_SERVER_LATENCY: Measure = globalStats.createMeasureDouble(
'grpc.io/client/server_latency',
MeasureUnit.MS,
'Propagated from the server and should have the same value as "grpc.io/server/latency'
);
/**
* Tag key that represents a client gRPC method.
*
* {@link #GRPC_CLIENT_METHOD} is set when an outgoing request starts and is
* available in all the recorded metrics.
*/
* the Content-Length header. This is uncompressed bytes. Responses with no
* body should record 0 for this value.
*/
export const HTTP_CLIENT_RECEIVED_BYTES: Measure = globalStats.createMeasureInt64(
'opencensus.io/http/client/received_bytes',
MeasureUnit.BYTE,
'Client-side total bytes received in response bodies (uncompressed)'
);
/**
* {@link Measure} for the client-side time between first byte of request
* headers sent to last byte of response received, or terminal error.
*/
export const HTTP_CLIENT_ROUNDTRIP_LATENCY: Measure = globalStats.createMeasureDouble(
'opencensus.io/http/client/roundtrip_latency',
MeasureUnit.MS,
'Client-side time between first byte of request headers sent to last byte of response received, or terminal error'
);
/**
* {@link Measure} for the server-side total bytes received in request body
* (not including headers). This is uncompressed bytes.
*/
export const HTTP_SERVER_RECEIVED_BYTES: Measure = globalStats.createMeasureInt64(
'opencensus.io/http/server/received_bytes',
MeasureUnit.BYTE,
'Server-side total bytes received in request body (uncompressed)'
);
/**
* {@link Measure} for the server-side total bytes sent in response bodies
* (not including headers but including error responses with bodies). Should