4

我正在尝试使用 google 监控 API 通过使用 java 客户端库来获取 stackdriver 指标。我的代码如下:

MetricServiceSettings{executorProvider=I> ProjectName name = ProjectName.of(projectId)

// Restrict time to last 'cpuCheckRange' minutes
long startMillis = System.currentTimeMillis() - MS_IN_MIN * cpuCheckRange - 1
TimeInterval interval = TimeInterval.newBuilder()
        .setStartTime(Timestamps.fromMillis(startMillis))
        .setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
        .build()

Aggregation aggregation = Aggregation.newBuilder()
        .setAlignmentPeriod(Duration.newBuilder().setSeconds(SEC_IN_MIN * cpuCheckRange).build())
        .setPerSeriesAligner(Aggregation.Aligner.ALIGN_MEAN)
        .build()

ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder()
        .setName(name.toString())
        .setFilter("metric.type=\"spanner.googleapis.com/instance/cpu/utilization\" resource.type=\"spanner_instance\" resource.label.\"instance_id\"=\"$instanceId\"")
        .setInterval(interval)
        .setAggregation(aggregation)

ListTimeSeriesRequest request = requestBuilder.build()

TimeSeries timeSeries = null
MetricServiceClient metricServiceClient = null
try {

    metricServiceClient = MetricServiceClient.create()
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request)

    for (TimeSeries ts : response.iterateAll()) {
        MonitoredResource resource = ts.getResource()
        Metric metric = ts.getMetric()

        if (metric.getType() == "spanner.googleapis.com/instance/cpu/utilization"
                && resource.getType() == "spanner_instance"
                && resource.getLabelsOrDefault("project_id", "") == projectId
                && resource.getLabelsOrDefault("instance_id", "") == instanceId) {
            timeSeries = ts
            break
        }
    }
} finally {
    if (metricServiceClient != null) {
        metricServiceClient.close()
    }
}

它在本地工作,但由于某些原因,当我在 docker 容器内的 jenkins 中运行它时:

docker.image('katalonstudio/katalon:6.1.5').inside('-u root') {
............               
}

我得到错误:

com.google.api.gax.rpc.DeadlineExceededException: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 19999685365ns. [buffered_nanos=19999833833, waiting_for_connection

我假设存在一些与 gRPC 相关的连接问题。那么我该如何使用 http 而不是 gRPC 来实现这个 API 呢?或任何其他建议?

环境细节:

  1. 在 docker 容器 'katalonstudio/katalon:6.1.5' 中运行的代码
  2. 依赖:

    编译'io.cucumber:cucumber-picocontainer:3.0.2' 编译'com.google.code.gson:gson:2.8.5' 编译'org.jdom:jdom2:2.0.6' 编译'jaxen:jaxen:1.1。 6' 编译'org.apache.ant:ant-jsch:1.10.5' 编译'com.google.cloud:google-cloud-monitoring:1.90.0' 编译'io.netty:netty-all:4.1.3.最后'

堆栈跟踪:

22:33:29  com.google.api.gax.rpc.DeadlineExceededException: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 19999701924ns. [buffered_nanos=19999820820, waiting_for_connection]
22:33:29    at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:51)
22:33:29    at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
22:33:29    at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
22:33:29    at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:97)
22:33:29    at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68)
22:33:29    at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1365)
22:33:29    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399)
22:33:29    at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:973)
22:33:29    at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:821)
22:33:29    at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:685)
22:33:29    at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:515)
22:33:29    at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:490)
22:33:29    at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
22:33:29    at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
22:33:29    at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
22:33:29    at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:700)
22:33:29    at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
22:33:29    at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
22:33:29    at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
22:33:29    at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:399)
22:33:29    at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:510)
22:33:29    at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:66)
22:33:29    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:630)
22:33:29    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$700(ClientCallImpl.java:518)
22:33:29    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:692)
22:33:29    at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:681)
22:33:29    at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
22:33:29    at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
22:33:29    at ?.Customers exists in the system(/tmp/katalon_execute/project/Include/features/OCS_SPANNER_diameter_event.feature:5)
22:33:29    Suppressed: com.google.api.gax.rpc.AsyncTaskException: Asynchronous task failed
22:33:29        at com.google.api.gax.rpc.ApiExceptions.callAndTranslateApiException(ApiExceptions.java:57)
22:33:29        at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
22:33:29        at com.google.cloud.monitoring.v3.MetricServiceClient.listTimeSeries(MetricServiceClient.java:995)
22:33:29        at com.google.cloud.monitoring.v3.MetricServiceClient$listTimeSeries$0.call(Unknown Source)
22:33:29        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
22:33:29        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
22:33:29        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
22:33:29        at com.optiva.CommonStepDefinition.getSpannerCpuUsage(CommonStepDefinition.groovy:744)
22:33:29        at com.optiva.CommonStepDefinition.checkSpannerCpuUsage(CommonStepDefinition.groovy:786)
22:33:29        at com.optiva.CommonStepDefinition.assertCustomers(CommonStepDefinition.groovy:117)
22:33:29        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
22:33:29        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
22:33:29        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
22:33:29        at java.lang.reflect.Method.invoke(Method.java:498)
22:33:29        at cucumber.runtime.Utils$1.call(Utils.java:26)
22:33:29        at cucumber.runtime.Timeout.timeout(Timeout.java:16)
22:33:29        at cucumber.runtime.Utils.invoke(Utils.java:20)
22:33:29        at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:48)
22:33:29        at cucumber.runtime.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
22:33:29        at cucumber.runner.TestStep.executeStep(TestStep.java:55)
22:33:29        at cucumber.runner.TestStep.run(TestStep.java:42)
22:33:29        at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:53)
22:33:29        at cucumber.runner.TestCase.run(TestCase.java:47)
22:33:29        at cucumber.runner.Runner.runPickle(Runner.java:44)
22:33:29        at cucumber.runtime.Runtime.runFeature(Runtime.java:120)
22:33:29        at cucumber.runtime.Runtime.run(Runtime.java:106)
22:33:29        at cucumber.api.cli.Main.run(Main.java:35)
22:33:29        at cucumber.api.cli.Main$run.call(Unknown Source)
22:33:29        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
22:33:29        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
22:33:29        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
22:33:29        at 
4

2 回答 2

0

此链接讨论 REST 和 grpc https://cloud.google.com/monitoring/api/v3/

但是 Java 客户端库和 gRPC 应该可以工作(并且是首选?)所以最好调试一下,因为我看不出 grpc 和 REST 在连接要求方面有任何区别(除了 http2 和 http)。您是否从 docker 容器的外壳发出 curl 以确保从那里连接?

于 2019-09-25T17:45:33.117 回答
0

我正在使用云运行服务内部的云监控 api,我自己观察到经常超出最后期限的错误:

com.google.api.gax.rpc.DeadlineExceededException: io.grpc.StatusRuntimeException: DEADLINE_EXCEEDED: deadline exceeded after 12.900875773s. [buffered_nanos=14900102827, waiting_for_connection]
at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:51)
at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72) 
at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60) 
at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:97) 
at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68) 
at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1072) 
at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:30) 
at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1164) 
at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:958) 
at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:749) 
at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:522) 
at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:497) 
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:426) 
at io.grpc.internal.ClientCallImpl.access$500(ClientCallImpl.java:66) 
at io.grpc.internal.ClientCallImpl$1CloseInContext.runInContext(ClientCallImpl.java:416) 
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) 
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123) 
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) 
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) 
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) 
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 
at java.base/java.lang.Thread.run(Thread.java:834)

Suppressed: com.google.api.gax.rpc.AsyncTaskException: Asynchronous task failed
at com.google.api.gax.rpc.ApiExceptions.callAndTranslateApiException(ApiExceptions.java:57) 
at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112) 
at com.google.cloud.monitoring.v3.MetricServiceClient.createTimeSeries(MetricServiceClient.java:1145) 
at org.mernst.metrics.ExporterModule$1.lambda$runOneIteration$3(ExporterModule.java:98) 

从同一任务中再次尝试通常会使其工作。有时它会立即在新开始的任务中运行。

com.google.cloud:google-cloud-monitoring:1.100.0,默认客户端选项。

于 2020-04-14T09:32:18.517 回答