(ctx context.Context, method string)
| 264 | } |
| 265 | |
| 266 | func (p *Pusher) push(ctx context.Context, method string) error { |
| 267 | if p.error != nil { |
| 268 | return p.error |
| 269 | } |
| 270 | mfs, err := p.gatherers.Gather() |
| 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | buf := &bytes.Buffer{} |
| 275 | enc := expfmt.NewEncoder(buf, p.expfmt) |
| 276 | // Check for pre-existing grouping labels: |
| 277 | for _, mf := range mfs { |
| 278 | for _, m := range mf.GetMetric() { |
| 279 | for _, l := range m.GetLabel() { |
| 280 | if l.GetName() == "job" { |
| 281 | return fmt.Errorf("pushed metric %s (%s) already contains a job label", mf.GetName(), m) |
| 282 | } |
| 283 | if _, ok := p.grouping[l.GetName()]; ok { |
| 284 | return fmt.Errorf( |
| 285 | "pushed metric %s (%s) already contains grouping label %s", |
| 286 | mf.GetName(), m, l.GetName(), |
| 287 | ) |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | if err := enc.Encode(mf); err != nil { |
| 292 | return fmt.Errorf( |
| 293 | "failed to encode metric family %s, error is %w", |
| 294 | mf.GetName(), err) |
| 295 | } |
| 296 | } |
| 297 | req, err := http.NewRequestWithContext(ctx, method, p.fullURL(), buf) |
| 298 | if err != nil { |
| 299 | return err |
| 300 | } |
| 301 | if p.header != nil { |
| 302 | req.Header = p.header |
| 303 | } |
| 304 | if p.useBasicAuth { |
| 305 | req.SetBasicAuth(p.username, p.password) |
| 306 | } |
| 307 | req.Header.Set(contentTypeHeader, string(p.expfmt)) |
| 308 | resp, err := p.client.Do(req) |
| 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | defer resp.Body.Close() |
| 313 | // Depending on version and configuration of the PGW, StatusOK or StatusAccepted may be returned. |
| 314 | if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted { |
| 315 | body, _ := io.ReadAll(resp.Body) // Ignore any further error as this is for an error message only. |
| 316 | return fmt.Errorf("unexpected status code %d while pushing to %s: %s", resp.StatusCode, p.fullURL(), body) |
| 317 | } |
| 318 | return nil |
| 319 | } |
| 320 | |
| 321 | // fullURL assembles the URL used to push/delete metrics and returns it as a |
| 322 | // string. The job name and any grouping label values containing a '/' will |
no test coverage detected