(urls []string, timeout time.Duration)
| 39 | } |
| 40 | |
| 41 | func (t *TimeSync) queryMultipleHttp(urls []string, timeout time.Duration) (now *time.Time) { |
| 42 | results := make(chan *time.Time, len(urls)) |
| 43 | |
| 44 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 45 | defer cancel() |
| 46 | |
| 47 | for _, url := range urls { |
| 48 | go func(url string) { |
| 49 | scopedLogger := t.l.With(). |
| 50 | Str("http_url", url). |
| 51 | Logger() |
| 52 | |
| 53 | metricHttpRequestCount.WithLabelValues(url).Inc() |
| 54 | metricHttpTotalRequestCount.Inc() |
| 55 | |
| 56 | startTime := time.Now() |
| 57 | now, response, err := queryHttpTime( |
| 58 | ctx, |
| 59 | url, |
| 60 | timeout, |
| 61 | t.networkConfig.GetTransportProxyFunc(), |
| 62 | ) |
| 63 | duration := time.Since(startTime) |
| 64 | |
| 65 | metricHttpServerLastRTT.WithLabelValues(url).Set(float64(duration.Milliseconds())) |
| 66 | metricHttpServerRttHistogram.WithLabelValues(url).Observe(float64(duration.Milliseconds())) |
| 67 | |
| 68 | status := 0 |
| 69 | if response != nil { |
| 70 | status = response.StatusCode |
| 71 | } |
| 72 | metricHttpServerInfo.WithLabelValues( |
| 73 | url, |
| 74 | strconv.Itoa(status), |
| 75 | ).Set(1) |
| 76 | |
| 77 | if err == nil { |
| 78 | metricHttpTotalSuccessCount.Inc() |
| 79 | metricHttpSuccessCount.WithLabelValues(url).Inc() |
| 80 | |
| 81 | requestId := response.Header.Get("X-Request-Id") |
| 82 | if requestId != "" { |
| 83 | requestId = response.Header.Get("X-Msedge-Ref") |
| 84 | } |
| 85 | if requestId == "" { |
| 86 | requestId = response.Header.Get("Cf-Ray") |
| 87 | } |
| 88 | scopedLogger.Info(). |
| 89 | Str("time", now.Format(time.RFC3339)). |
| 90 | Int("status", status). |
| 91 | Str("request_id", requestId). |
| 92 | Str("time_taken", duration.String()). |
| 93 | Msg("HTTP server returned time") |
| 94 | |
| 95 | cancel() |
| 96 | results <- now |
| 97 | } else if errors.Is(err, context.Canceled) { |
| 98 | metricHttpCancelCount.WithLabelValues(url).Inc() |
no test coverage detected