exemplar.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package exemplar implements support for exemplars. Exemplars are additional
  15. // data associated with each measurement.
  16. //
  17. // Their purpose it to provide an example of the kind of thing
  18. // (request, RPC, trace span, etc.) that resulted in that measurement.
  19. package exemplar
  20. import (
  21. "context"
  22. "time"
  23. )
  24. const (
  25. KeyTraceID = "trace_id"
  26. KeySpanID = "span_id"
  27. KeyPrefixTag = "tag:"
  28. )
  29. // Exemplar is an example data point associated with each bucket of a
  30. // distribution type aggregation.
  31. type Exemplar struct {
  32. Value float64 // the value that was recorded
  33. Timestamp time.Time // the time the value was recorded
  34. Attachments Attachments // attachments (if any)
  35. }
  36. // Attachments is a map of extra values associated with a recorded data point.
  37. // The map should only be mutated from AttachmentExtractor functions.
  38. type Attachments map[string]string
  39. // AttachmentExtractor is a function capable of extracting exemplar attachments
  40. // from the context used to record measurements.
  41. // The map passed to the function should be mutated and returned. It will
  42. // initially be nil: the first AttachmentExtractor that would like to add keys to the
  43. // map is responsible for initializing it.
  44. type AttachmentExtractor func(ctx context.Context, a Attachments) Attachments
  45. var extractors []AttachmentExtractor
  46. // RegisterAttachmentExtractor registers the given extractor associated with the exemplar
  47. // type name.
  48. //
  49. // Extractors will be used to attempt to extract exemplars from the context
  50. // associated with each recorded measurement.
  51. //
  52. // Packages that support exemplars should register their extractor functions on
  53. // initialization.
  54. //
  55. // RegisterAttachmentExtractor should not be called after any measurements have
  56. // been recorded.
  57. func RegisterAttachmentExtractor(e AttachmentExtractor) {
  58. extractors = append(extractors, e)
  59. }
  60. // NewFromContext extracts exemplars from the given context.
  61. // Each registered AttachmentExtractor (see RegisterAttachmentExtractor) is called in an
  62. // unspecified order to add attachments to the exemplar.
  63. func AttachmentsFromContext(ctx context.Context) Attachments {
  64. var a Attachments
  65. for _, extractor := range extractors {
  66. a = extractor(ctx, a)
  67. }
  68. return a
  69. }