Skip to content

Commit

Permalink
Merge pull request #402 from jianyi-gronk/add_observable_test
Browse files Browse the repository at this point in the history
Add observable test
  • Loading branch information
chickenlj committed May 27, 2024
2 parents 707adae + a10caac commit 55ad68a
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 62 deletions.
2 changes: 0 additions & 2 deletions example/dubbo-observable-example/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
const transport = createDubboTransport({
baseUrl: "http://localhost:8080",
httpVersion: "1.1",

/**
* Enable Observable Service on service consumer.
*/
Expand All @@ -36,7 +35,6 @@ async function main() {
const client = createPromiseClient(ExampleService, transport, { serviceVersion: '1.0.0', serviceGroup: 'dubbo' });

server.get("/", (_, reply) => {

client.say({ sentence: "Hello World" }).then(rs => {
reply.type("application/json");
reply.send(rs.toJsonString());
Expand Down
5 changes: 2 additions & 3 deletions example/dubbo-observable-example/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ services:
- "13000:3000"

vm:
#victoria-metrics-prod -storageDataPath /victoria-metrics -retentionPeriod 180d -promscrape.config /prometheus.yml -promscrape.httpSDCheckInterval 60s
image: victoriametrics/victoria-metrics:latest
hostname: vm
command: -storageDataPath /victoria-metrics-data -retentionPeriod 180d -promscrape.config /prometheus.yml -promscrape.httpSDCheckInterval 60s
volumes:
- ./prometheus.yml:/prometheus.yml
- ./victoria-metrics-data:/victoria-metrics-data
- ./prometheus.yml:/prometheus.yml
- ./victoria-metrics-data:/victoria-metrics-data
networks:
- dubbo-network
ports:
Expand Down
1 change: 0 additions & 1 deletion example/dubbo-observable-example/proto/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ message SayResponse {

service ExampleService {
rpc Say(SayRequest) returns (SayResponse) {}
rpc hello(SayRequest) returns (SayResponse) {}
}
1 change: 0 additions & 1 deletion example/dubbo-observable-example/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ async function main() {
const server = fastify();
await server.register(fastifyDubboPlugin, {
routes,

/**
* Enable Observable Service on service provider.
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/dubbo-node/src/dubbo-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
type NodeTransportOptions,
validateNodeTransportOptions,
} from "./node-transport-options.js";
import type { ObservableOptions } from '@apachedubbo/dubbo-observable'

/**
* Options used to configure the Dubbo transport.
Expand Down Expand Up @@ -122,6 +123,11 @@ type DubboTransportOptions = NodeTransportOptions &
* available, on side-effect free methods. Defaults to false.
*/
useHttpGet?: boolean;

/**
* Observable related configurations
*/
observableOptions?: ObservableOptions;
};

/**
Expand Down
30 changes: 16 additions & 14 deletions packages/dubbo-observable/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// 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.

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

export type { ObservableOptions, Observable } from "./observable.js";
export { createObservable } from "./observable.js";
Expand Down
69 changes: 43 additions & 26 deletions packages/dubbo-observable/src/meter-collector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 type {
Attributes,
Counter,
Expand All @@ -7,9 +24,9 @@ import type {
MetricOptions,
ObservableCounter,
UpDownCounter,
} from '@opentelemetry/api'
import { metrics, ValueType } from '@opentelemetry/api';
import { QpsCounter } from './qps-counter.js';
} from "@opentelemetry/api"
import { metrics, ValueType } from "@opentelemetry/api";
import { QpsCounter } from "./qps-counter.js";

export { ValueType } from "@opentelemetry/api";
export type { MetricOptions } from "@opentelemetry/api";
Expand Down Expand Up @@ -126,36 +143,35 @@ export class MeterCollector {
* Service provider metrics collector
*/
export class ProviderMeterCollector extends MeterCollector {

/**
* The counter of total number of received requests by the provider
* @private
*/
private providerRequestTotal: Counter | undefined;

/**
* The counter of number of requests successfully received by the provider
* The counter of total number of successfully received requests by the provider
* @private
*/
private providerRequestSucceedTotal: Counter | undefined;

/**
* The counter of number of requests received by the provider per second counter
* The counter of total number of unsuccessfully received requests by the provider
* @private
*/
private providerRequestQps: ObservableCounter | undefined;
private providerRequestFailedTotal: Counter | undefined

/**
* The QPS counter
* The counter of number of requests received by the provider per second counter
* @private
*/
private qpsCounter: QpsCounter | undefined;
private providerRequestQps: ObservableCounter | undefined;

/**
* Total counter of Failed Requests
* The QPS counter
* @private
*/
private providerRequestFailedTotal: Counter | undefined
private qpsCounter: QpsCounter | undefined;

constructor(options?: MeterCollectorOptions) {
super(options)
Expand All @@ -166,13 +182,13 @@ export class ProviderMeterCollector extends MeterCollector {

this.providerRequestSucceedTotal =
this.getCounter("dubbo_provider_requests_succeed_total", {
description: `The number of requests successfully received by the provider`,
description: `The total number of successfully received requests by the provider`,
valueType: ValueType.INT
});

this.providerRequestFailedTotal =
this.getCounter("dubbo_provider_requests_failed_total", {
description: `Total Failed Requests`,
description: `The total number of unsuccessfully received requests by the provider`,
valueType: ValueType.INT
});

Expand Down Expand Up @@ -231,55 +247,56 @@ export class ProviderMeterCollector extends MeterCollector {
*/
export class ConsumerMeterCollector extends MeterCollector {
/**
* The counter of total number of sent requests by consumers
* The counter of total number of received requests by the consumer
* @private
*/
private consumerRequestTotal: Counter | undefined;

/**
* The counter of number of requests sent by consumers per second
* The counter of total number of successfully received requests by the consumer
* @private
*/
private consumerRequestQps: ObservableCounter | undefined;
private consumerRequestSucceedTotal: Counter | undefined;

/**
* The QPS counter.
* The counter of total number of unsuccessfully received requests by the consumer
* @private
*/
private qpsCounter: QpsCounter | undefined;
private consumerRequestFailedTotal: Counter | undefined

/**
* The counter of number of successful requests sent by consumers
* The counter of number of requests received by the consumer per second counter
* @private
*/
private consumerRequestSucceedTotal: Counter | undefined;
private consumerRequestQps: ObservableCounter | undefined;

/**
* The counter of total Failed Requests.
* The QPS counter.
* @private
*/
private consumerRequestFailedTotal: Counter | undefined
private qpsCounter: QpsCounter | undefined;

constructor(options?: MeterCollectorOptions) {
super(options)
this.consumerRequestTotal = this.getCounter("dubbo_consumer_requests_total", {
description: `Total number of sent requests by consumers`,
description: `The total number of received requests by the consumer`,
valueType: ValueType.INT
});

this.consumerRequestSucceedTotal =
this.getCounter("dubbo_consumer_requests_succeed_total", {
description: `The number of successful requests sent by consumers`,
description: `The total number of successfully received requests by the consumer`,
valueType: ValueType.INT
});

this.consumerRequestFailedTotal =
this.getCounter("dubbo_consumer_requests_failed_total", {
description: `Total Failed Requests`,
description: `The total number of unsuccessfully received requests by the consumer`,
valueType: ValueType.INT
});

this.consumerRequestQps = this.getObservableCounter("dubbo_consumer_qps_total", {
description: "The number of requests sent by consumers per second",
description: "The number of requests received by the consumer per second",
valueType: ValueType.INT
});
}
Expand Down
21 changes: 19 additions & 2 deletions packages/dubbo-observable/src/observable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { NodeSDK } from '@opentelemetry/sdk-node';
import type {NodeSDKConfiguration} from '@opentelemetry/sdk-node';
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 { NodeSDK } from "@opentelemetry/sdk-node";
import type { NodeSDKConfiguration } from "@opentelemetry/sdk-node";

export type ObservableOptions = {
/**
Expand Down
50 changes: 47 additions & 3 deletions packages/dubbo-observable/src/qps-counter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 { QpsCounter } from "./qps-counter.js";

describe("QpsCounter Error", () => {
console.log("");
})
describe("QpsCounter", function () {
let qpsCounter = new QpsCounter();

beforeEach(function () {
jasmine.clock().install();
jasmine.clock().mockDate(new Date());
qpsCounter = new QpsCounter();
});

afterEach(function () {
jasmine.clock().uninstall();
qpsCounter.stop();
});

it("should initialize counter correctly", function () {
expect(qpsCounter).toBeDefined();
expect(qpsCounter.getQps()).toBe(0);
});

it("should increment the counter correctly", function () {
qpsCounter.increment();
qpsCounter.increment();
qpsCounter.increment();
expect(qpsCounter.getQps()).toBe(0);

jasmine.clock().tick(1000);

expect(qpsCounter.getQps()).toBe(3);
});
});
19 changes: 17 additions & 2 deletions packages/dubbo-observable/src/qps-counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/

export class QpsCounter {
/**
Expand Down Expand Up @@ -48,7 +63,7 @@ export class QpsCounter {
* Total number of seconds from 00:00:00 Greenwich Mean Time on January 1, 1970 to now
*/
currentSecond() {
return Math.floor(new Date().getTime()/1000);
return Math.floor(new Date().getTime() / 1000);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/dubbo/src/protocol-grpc/gen/status_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// @generated by protoc-gen-es v1.8.0 with parameter "target=ts"
// @generated by protoc-gen-es v1.2.1 with parameter "target=ts"
// @generated from file status.proto (package google.rpc, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
Loading

0 comments on commit 55ad68a

Please sign in to comment.