labeler.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright The OpenTelemetry 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 otelhttp // import "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  15. import (
  16. "context"
  17. "sync"
  18. "go.opentelemetry.io/otel/attribute"
  19. )
  20. // Labeler is used to allow instrumented HTTP handlers to add custom attributes to
  21. // the metrics recorded by the net/http instrumentation.
  22. type Labeler struct {
  23. mu sync.Mutex
  24. attributes []attribute.KeyValue
  25. }
  26. // Add attributes to a Labeler.
  27. func (l *Labeler) Add(ls ...attribute.KeyValue) {
  28. l.mu.Lock()
  29. defer l.mu.Unlock()
  30. l.attributes = append(l.attributes, ls...)
  31. }
  32. // Get returns a copy of the attributes added to the Labeler.
  33. func (l *Labeler) Get() []attribute.KeyValue {
  34. l.mu.Lock()
  35. defer l.mu.Unlock()
  36. ret := make([]attribute.KeyValue, len(l.attributes))
  37. copy(ret, l.attributes)
  38. return ret
  39. }
  40. type labelerContextKeyType int
  41. const lablelerContextKey labelerContextKeyType = 0
  42. func injectLabeler(ctx context.Context, l *Labeler) context.Context {
  43. return context.WithValue(ctx, lablelerContextKey, l)
  44. }
  45. // LabelerFromContext retrieves a Labeler instance from the provided context if
  46. // one is available. If no Labeler was found in the provided context a new, empty
  47. // Labeler is returned and the second return value is false. In this case it is
  48. // safe to use the Labeler but any attributes added to it will not be used.
  49. func LabelerFromContext(ctx context.Context) (*Labeler, bool) {
  50. l, ok := ctx.Value(lablelerContextKey).(*Labeler)
  51. if !ok {
  52. l = &Labeler{}
  53. }
  54. return l, ok
  55. }