writeNameAndLabelPairs converts a slice of LabelPair proto messages plus the explicitly given metric name and additional label pair into text formatted as required by the text format and writes it to 'w'. An empty slice in combination with an empty string 'additionalLabelName' results in nothing bei
( w enhancedWriter, name string, in []*dto.LabelPair, additionalLabelName string, additionalLabelValue float64, )
| 352 | // legacy-valid, it will be put inside the brackets as well. Legacy-invalid |
| 353 | // label names will also be quoted. |
| 354 | func writeNameAndLabelPairs( |
| 355 | w enhancedWriter, |
| 356 | name string, |
| 357 | in []*dto.LabelPair, |
| 358 | additionalLabelName string, additionalLabelValue float64, |
| 359 | ) (int, error) { |
| 360 | var ( |
| 361 | written int |
| 362 | separator byte = '{' |
| 363 | metricInsideBraces = false |
| 364 | ) |
| 365 | |
| 366 | if name != "" { |
| 367 | // If the name does not pass the legacy validity check, we must put the |
| 368 | // metric name inside the braces. |
| 369 | if !model.LegacyValidation.IsValidMetricName(name) { |
| 370 | metricInsideBraces = true |
| 371 | err := w.WriteByte(separator) |
| 372 | written++ |
| 373 | if err != nil { |
| 374 | return written, err |
| 375 | } |
| 376 | separator = ',' |
| 377 | } |
| 378 | n, err := writeName(w, name) |
| 379 | written += n |
| 380 | if err != nil { |
| 381 | return written, err |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if len(in) == 0 && additionalLabelName == "" { |
| 386 | if metricInsideBraces { |
| 387 | err := w.WriteByte('}') |
| 388 | written++ |
| 389 | if err != nil { |
| 390 | return written, err |
| 391 | } |
| 392 | } |
| 393 | return written, nil |
| 394 | } |
| 395 | |
| 396 | for _, lp := range in { |
| 397 | err := w.WriteByte(separator) |
| 398 | written++ |
| 399 | if err != nil { |
| 400 | return written, err |
| 401 | } |
| 402 | n, err := writeName(w, lp.GetName()) |
| 403 | written += n |
| 404 | if err != nil { |
| 405 | return written, err |
| 406 | } |
| 407 | n, err = w.WriteString(`="`) |
| 408 | written += n |
| 409 | if err != nil { |
| 410 | return written, err |
| 411 | } |
no test coverage detected