-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtelemetry_metrics_otlp_example.ts
90 lines (78 loc) · 3.02 KB
/
telemetry_metrics_otlp_example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks";
import { context } from "@opentelemetry/api";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { AWSXRayPropagator } from "@opentelemetry/propagator-aws-xray";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { AwsInstrumentation } from "@opentelemetry/instrumentation-aws-sdk";
import { AWSXRayIdGenerator } from "@opentelemetry/id-generator-aws-xray";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-grpc";
import { Resource } from "@opentelemetry/resources";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { PeriodicExportingMetricReader } from "@opentelemetry/sdk-metrics";
import { OTLPMetricExporter } from "@opentelemetry/exporter-metrics-otlp-grpc";
import { AwsPGClient } from "../../pg/lib";
const traceExporter = new OTLPTraceExporter({ url: "http://localhost:4317" });
const resource = Resource.default().merge(
new Resource({
[ATTR_SERVICE_NAME]: "aws-advanced-nodejs-wrapper"
})
);
const metricReader = new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter(),
exportIntervalMillis: 1000
});
const contextManager = new AsyncHooksContextManager();
contextManager.enable();
context.setGlobalContextManager(contextManager);
const sdk = new NodeSDK({
textMapPropagator: new AWSXRayPropagator(),
instrumentations: [
new HttpInstrumentation(),
new AwsInstrumentation({
suppressInternalInstrumentation: true
})
],
resource: resource,
traceExporter: traceExporter,
metricReader: metricReader,
idGenerator: new AWSXRayIdGenerator()
});
// This enables the API to record telemetry.
sdk.start();
// Shut down the SDK on process exit.
process.on("SIGTERM", () => {
sdk
.shutdown()
.then(() => console.log("Tracing and Metrics terminated"))
.catch((error) => console.log("Error terminating tracing and metrics", error))
.finally(() => process.exit(0));
});
const client = new AwsPGClient({
user: "username",
host: "db-identifier.XYZ.us-east-2.rds.amazonaws.com",
database: "database_name",
password: "password",
port: 5432,
enableTelemetry: true,
telemetryTracesBackend: "OTLP",
telemetryMetricsBackend: "OTLP"
});
try {
await client.connect();
const result = (await client.query("select * from aurora_db_instance_identifier()")).rows[0];
console.log(result);
} finally {
await client.end();
}