Я читаю коды измерения задержки, потому что я обнаружил, что измеренная задержка выше, чем сквозная задержка, измеренная кодами Python. Верно ли следующее использование InstrumentRoundTripperDuration
? Я не нашел подобных примеров в Интернете.
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// metrics client
func NewClientMetrics(spec *v1.PredictorSpec, deploymentName string, modelName string) *ClientMetrics {
histogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: ClientRequestsMetricName,
Help: "A histogram of latencies for client calls from executor",
Buckets: prometheus.DefBuckets,
},
[]string{DeploymentNameMetric, PredictorNameMetric, PredictorVersionMetric, ServiceMetric, ModelNameMetric, ModelImageMetric, ModelVersionMetric, "method", "code"},
)
err := prometheus.Register(histogram)
if err != nil {
prometheus.Unregister(histogram)
prometheus.Register(histogram)
}
...
return &ClientMetrics{
ClientHandledHistogram: histogram,
Predictor: spec,
DeploymentName: deploymentName,
ModelName: modelName,
ImageName: imageName,
ImageVersion: imageVersion,
}
}
// In json rest client, construct metrics client
func NewJSONRestClient(protocol string, deploymentName string, predictor *v1.PredictorSpec, annotations map[string]string, options ...BytesRestClientOption) (client.SeldonApiClient, error) {
httpClient := http.DefaultClient
...
client := JSONRestClient{
httpClient,
logf.Log.WithName("JSONRestClient"),
protocol,
deploymentName,
predictor,
metric.NewClientMetrics(predictor, deploymentName, ""),
}
...
return &client, nil
}
// In Json rest client, create a function to measure roundtrip latency
func (smc *JSONRestClient) getMetricsRoundTripper(modelName string, service string) http.RoundTripper {
...
return promhttp.InstrumentRoundTripperDuration(smc.metrics.ClientHandledHistogram.MustCurryWith(prometheus.Labels{
metric.DeploymentNameMetric: smc.DeploymentName,
metric.PredictorNameMetric: smc.predictor.Name,
metric.PredictorVersionMetric: smc.predictor.Annotations["version"],
metric.ServiceMetric: service,
metric.ModelNameMetric: modelName,
metric.ModelImageMetric: imageName,
metric.ModelVersionMetric: imageVersion,
}), http.DefaultTransport)
}
// http call (getMetricsRoundTripper is called middleware)
client := smc.httpClient
client.Transport = smc.getMetricsRoundTripper(modelName, method)
исходные коды:
https://github.com/SeldonIO/seldon-core/blob/master/executor/api/rest/client.go#L88 и строки 148, 118
https://github.com/SeldonIO/seldon-core/blob/master/executor/api/metric/client.go#L22
Спасибо