-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
60 lines (47 loc) · 2.01 KB
/
Copy pathclient.ts
File metadata and controls
60 lines (47 loc) · 2.01 KB
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
import { ChannelCredentials, createClientFactory,createChannel } from "nice-grpc";
import { prometheusClientMiddleware, prometheusServerMiddleware } from "nice-grpc-prometheus";
import { GreetServiceClient,GreetServiceDefinition,GreetResponse } from "./compiled_proto/test";
import { ChannelImplementation } from "@grpc/grpc-js/build/src/channel";
import globalRegistry from './registry'
import { Histogram, collectDefaultMetrics, register } from 'prom-client';
// Enable default metric collection
collectDefaultMetrics();
//
// import { Histogram, register } from 'prom-client';
// import { registry as niceGrpcRegistry } from 'nice-grpc-prometheus';
// // Merge niceGrpcRegistry with the global registry
// const mergedRegistry = register.setDefaultRegistry(niceGrpcRegistry);
//
const channel: ChannelImplementation = createChannel('localhost:3500', ChannelCredentials.createInsecure())
const client = createClientFactory()
.use(prometheusClientMiddleware())
.create(GreetServiceDefinition,channel)
async function runClient(): Promise <void> {
try {
// Record the start time of the RPC call
const startTime = Date.now();
// Your gRPC client call code here
// Record the end time of the RPC call
const endTime = Date.now();
// Calculate the duration of the RPC call
const duration = endTime - startTime;
const response:GreetResponse = await client.greetings({ Hello: 'hi' });
const histogram = new Histogram({
name: 'grpc_client_request_duration',
help: 'Duration of gRPC client requests',
labelNames: ['service', 'method'],
registers: [register],
});
console.log('Response:', response);
histogram
.labels('GreetService', 'greetings')
.observe(duration);
console.log(histogram)
} catch (error) {
console.error('Error:', error);
} finally {
channel.close();
}
}
runClient();
// channel.close()