Skip to content

Commit acd9913

Browse files
committed
Add admin interface
1 parent 74fdd85 commit acd9913

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

packages/grpc-js/src/admin.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2021 gRPC authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
import { ServiceDefinition } from "./make-client";
19+
import { Server, UntypedServiceImplementation } from "./server";
20+
21+
interface GetServiceDefinition {
22+
(): ServiceDefinition;
23+
}
24+
25+
interface GetHandlers {
26+
(): UntypedServiceImplementation;
27+
}
28+
29+
const registeredAdminServices: {getServiceDefinition: GetServiceDefinition, getHandlers: GetHandlers}[] = [];
30+
31+
export function registerAdminService(getServiceDefinition: GetServiceDefinition, getHandlers: GetHandlers) {
32+
registeredAdminServices.push({getServiceDefinition, getHandlers});
33+
}
34+
35+
export function addAdminServicesToServer(server: Server): void {
36+
for (const {getServiceDefinition, getHandlers} of registeredAdminServices) {
37+
server.addService(getServiceDefinition(), getHandlers());
38+
}
39+
}

packages/grpc-js/src/channelz.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { GetServerSocketsResponse } from "./generated/grpc/channelz/v1/GetServer
5050
import { ChannelzDefinition, ChannelzHandlers } from "./generated/grpc/channelz/v1/Channelz";
5151
import { ProtoGrpcType as ChannelzProtoGrpcType } from "./generated/channelz";
5252
import type { loadSync } from '@grpc/proto-loader';
53+
import { registerAdminService } from "./admin";
5354

5455
export type TraceSeverity = 'CT_UNKNOWN' | 'CT_INFO' | 'CT_WARNING' | 'CT_ERROR';
5556

@@ -713,4 +714,8 @@ export function getChannelzServiceDefinition(): ChannelzDefinition {
713714
}) as unknown as ChannelzProtoGrpcType;
714715
loadedChannelzDefinition = loadedProto.grpc.channelz.v1.Channelz.service;
715716
return loadedChannelzDefinition;
717+
}
718+
719+
export function setup() {
720+
registerAdminService(getChannelzServiceDefinition, getChannelzHandlers);
716721
}

packages/grpc-js/src/experimental.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export {
3232
export { Call as CallStream } from './call-stream';
3333
export { Filter, BaseFilter, FilterFactory } from './filter';
3434
export { FilterStackFactory } from './filter-stack';
35+
export { registerAdminService } from './admin';

packages/grpc-js/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ export { GrpcObject } from './make-client';
249249

250250
export { ChannelOptions } from './channel-options';
251251

252+
export {
253+
getChannelzServiceDefinition,
254+
getChannelzHandlers
255+
} from './channelz';
256+
257+
export { addAdminServicesToServer } from './admin';
258+
252259
import * as experimental from './experimental';
253260
export { experimental };
254261

@@ -257,11 +264,13 @@ import * as resolver_uds from './resolver-uds';
257264
import * as resolver_ip from './resolver-ip';
258265
import * as load_balancer_pick_first from './load-balancer-pick-first';
259266
import * as load_balancer_round_robin from './load-balancer-round-robin';
267+
import * as channelz from './channelz';
260268

261269
(() => {
262270
resolver_dns.setup();
263271
resolver_uds.setup();
264272
resolver_ip.setup();
265273
load_balancer_pick_first.setup();
266274
load_balancer_round_robin.setup();
275+
channelz.setup();
267276
})();

0 commit comments

Comments
 (0)