Google cloud platform Google对延迟数据的自定义度量跟踪
我正在使用基于REST调用的外部服务,我希望跟踪服务响应我的请求所花费的时间。我的代码是用C#(core v2.2)编写的 我计划计算所有HTTP请求的时间(使用Google cloud platform Google对延迟数据的自定义度量跟踪,google-cloud-platform,google-cloud-stackdriver,Google Cloud Platform,Google Cloud Stackdriver,我正在使用基于REST调用的外部服务,我希望跟踪服务响应我的请求所花费的时间。我的代码是用C#(core v2.2)编写的 我计划计算所有HTTP请求的时间(使用Stopwatch),并将此信息保存在列表中。每隔60秒,我会将列表中的跟踪信息写入谷歌自定义指标 最后,我希望在图中看到执行的平均时间 这是我目前的代码: public class CustomMetricsWritter { public CustomMetricsWritter(string projectId)
Stopwatch
),并将此信息保存在列表中。每隔60秒,我会将列表中的跟踪信息写入谷歌自定义指标
最后,我希望在图中看到执行的平均时间
这是我目前的代码:
public class CustomMetricsWritter
{
public CustomMetricsWritter(string projectId)
{
this.Client = MetricServiceClient.Create();
this.ProjectId = projectId;
}
private MetricServiceClient Client { get; set; }
public string ProjectId { get; private set; }
public object CreateMetric(string metricType, string title, string description, string unit)
{
// Prepare custom metric descriptor.
MetricDescriptor metricDescriptor = new MetricDescriptor();
metricDescriptor.DisplayName = title;
metricDescriptor.Description = description;
metricDescriptor.MetricKind = MetricKind.Gauge;
metricDescriptor.ValueType = MetricDescriptor.Types.ValueType.Double;
metricDescriptor.Type = metricType;
metricDescriptor.Unit = unit;
CreateMetricDescriptorRequest request = new CreateMetricDescriptorRequest
{
ProjectName = new ProjectName(this.ProjectId),
};
request.MetricDescriptor = metricDescriptor;
// Make the request.
return Client.CreateMetricDescriptor(request);
}
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public async Task WriteTimeSeriesDataAsync(string metricDescriptor, TypedValue[] points, string machineName)
{
// Initialize request argument(s).
ProjectName name = new ProjectName(this.ProjectId);
// Prepare a data point.
Timestamp timeStamp = new Timestamp();
timeStamp.Seconds = (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds;
TimeInterval interval = new TimeInterval();
interval.EndTime = timeStamp;
// Prepare monitored resource.
MonitoredResource resource = new MonitoredResource();
resource.Type = "global";
resource.Labels.Add("project_id", this.ProjectId);
// Add newly created time series to list of time series to be written.
List<TimeSeries> timeSeries = new List<TimeSeries>(points.Length);
// Prepare custom metric.
Metric metric = new Metric();
metric.Type = metricDescriptor;
metric.Labels.Add("machine", machineName);
// Create a new time series using inputs.
TimeSeries timeSeriesData = new TimeSeries();
timeSeriesData.Metric = metric;
timeSeriesData.Resource = resource;
foreach (var point in points)
{
Point dataPoint = new Point();
dataPoint.Value = point;
dataPoint.Interval = interval;
timeSeriesData.Points.Add(dataPoint);
}
timeSeries.Add(timeSeriesData);
// Write time series data.
await this.Client.CreateTimeSeriesAsync(name, timeSeries).ConfigureAwait(false);
}
}
我将返回此抛出的异常:
Grpc.Core.RpcException: Status(StatusCode=InvalidArgument, Detail="One or more TimeSeries could not be written: Field timeSeries[0].points had an invalid value: Only one point can be written per TimeSeries per request.: timeSeries[0]")
at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass0_0`2.<<WithRetry>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at ***.CustomMetricsWritter.WriteTimeSeriesDataAsync(String metricDescriptor, TypedValue[] points, String machineName) in ***\GoogleCloud\CustomMetricsWritter.cs:line 131
at Test.Program.MainAsync() in ***\Test\Program.cs:line 156
Grpc.Core.RpcException:Status(StatusCode=InvalidArgument,Detail=“无法写入一个或多个时间序列:字段时间序列[0]。点具有无效值:每个请求每个时间序列只能写入一个点。:时间序列[0]”)
在Google.Api.Gax.Grpc.ApiCallRetryExtensions.c_uudisplayClass0_0`2.d.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
位于***\GoogleCloud\CustomMetricsWriter.cs中的***.CustomMetricsWriter.WriteTimesDaaAsync(字符串度量描述符,TypedValue[]点,字符串机器名):第131行
在***\Test\Program.cs中的Test.Program.MainAsync()处:第156行
我做错了什么?我不是C#专家,但这里有一些例子:
我不是C#专家,但这里有一些例子:
嘿,谢谢你的回答。我现有的代码基于这个示例。两者的区别在于我试图将多个值推到同一时间间隔,这看起来是不可能的。作为一种解决方法,每个服务器对所有样本进行平均值
,并上载单个值。这就解决了问题。嘿,谢谢你的回答。我现有的代码基于这个示例。两者的区别在于我试图将多个值推到同一时间间隔,这看起来是不可能的。作为一种解决方法,每个服务器对所有样本进行平均值
,并上载单个值。这就解决了问题。
Grpc.Core.RpcException: Status(StatusCode=InvalidArgument, Detail="One or more TimeSeries could not be written: Field timeSeries[0].points had an invalid value: Only one point can be written per TimeSeries per request.: timeSeries[0]")
at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass0_0`2.<<WithRetry>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at ***.CustomMetricsWritter.WriteTimeSeriesDataAsync(String metricDescriptor, TypedValue[] points, String machineName) in ***\GoogleCloud\CustomMetricsWritter.cs:line 131
at Test.Program.MainAsync() in ***\Test\Program.cs:line 156