Search code examples
typescriptgoogle-cloud-platformgoogle-apis-explorergoogle-cloud-monitoring

typescript: listServiceLevelObjectives gives UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT


I'm trying to get list of SLOs in google cloud project through REST API using typescript, but receiving an error. Could you help me please. Error is:

(node:47173) UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT: Received invalid service name: [object Object] at Object.callErrorFromStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call.ts:81:24) at Object.onReceiveStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/client.ts:334:36) at Object.onReceiveStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/client-interceptors.ts:434:34) at Object.onReceiveStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/client-interceptors.ts:397:48) at Http2CallStream.outputStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:230:22) at Http2CallStream.maybeOutputStatus (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:280:14) at Http2CallStream.endCall (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:263:12) at Http2CallStream.handleTrailers (/Users/mpanova/workspace/node/monitoring-automation/node_modules/@grpc/grpc-js/src/call-stream.ts:412:10) at ClientHttp2Stream.emit (events.js:314:20) at emit (internal/http2/core.js:291:8) at processTicksAndRejections (internal/process/task_queues.js:83:22) at emitUnhandledRejectionWarning (internal/process/promises.js:168:15) at processPromiseRejections (internal/process/promises.js:247:11) at processTicksAndRejections (internal/process/task_queues.js:94:32)

To be honest I'm new to Typescript, usually I use Java, so may be an error is stupid and simple.

My code is: list_slos.ts in source directory

export module ListSloTest {
    export async function listSlos(projectId: string) {
        // Imports the Google Cloud client library
        const monitoring = require('@google-cloud/monitoring');

        // Creates a client
        const client = new monitoring.ServiceMonitoringServiceClient();

        const request = {
            parent: client.projectPath(projectId),
        };
        console.log(projectId)

        const [slos] = await client.listServiceLevelObjectives({parent: request});

        slos.forEach((slo: any) => {
            console.log('');
            console.log('slo: %j', slo);
        });
**list_slos.ts in test directory**
import "mocha";
import * as assert from "assert";
import {ListSloTest} from "../dist/list_slos";

describe("list_slos",()=>{

    before(function () {
        process.env = {GOOGLE_CLOUD_PROJECT: 'projectId',
            GOOGLE_APPLICATION_CREDENTIALS: '/somePath/projectId-a17ce3d7c2e6.json'};
    });

    it("should list slos in given project",()=>{

        ListSloTest.listSlos("projectId");
        //console.log("hello, world!")
        assert.ok(true);

    });

});

I'm compiling code from source to dist directory and then use it in test.


Solution

  • Resolved. Request should be the next:

    const request = client.projectServicePath(projectId, serviceName);
    

    This works for version 2.1.1 of @google-cloud/monitoring Seems a pity, but I didn't found an example, only for 1.7 version, which doesn't work for 2.1.1.