limit.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sdk
  4. import (
  5. "log/slog"
  6. "os"
  7. "strconv"
  8. )
  9. // maxSpan are the span limits resolved during startup.
  10. var maxSpan = newSpanLimits()
  11. type spanLimits struct {
  12. // Attrs is the number of allowed attributes for a span.
  13. //
  14. // This is resolved from the environment variable value for the
  15. // OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT key if it exists. Otherwise, the
  16. // environment variable value for OTEL_ATTRIBUTE_COUNT_LIMIT, or 128 if
  17. // that is not set, is used.
  18. Attrs int
  19. // AttrValueLen is the maximum attribute value length allowed for a span.
  20. //
  21. // This is resolved from the environment variable value for the
  22. // OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT key if it exists. Otherwise, the
  23. // environment variable value for OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT, or -1
  24. // if that is not set, is used.
  25. AttrValueLen int
  26. // Events is the number of allowed events for a span.
  27. //
  28. // This is resolved from the environment variable value for the
  29. // OTEL_SPAN_EVENT_COUNT_LIMIT key, or 128 is used if that is not set.
  30. Events int
  31. // EventAttrs is the number of allowed attributes for a span event.
  32. //
  33. // The is resolved from the environment variable value for the
  34. // OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT key, or 128 is used if that is not set.
  35. EventAttrs int
  36. // Links is the number of allowed Links for a span.
  37. //
  38. // This is resolved from the environment variable value for the
  39. // OTEL_SPAN_LINK_COUNT_LIMIT, or 128 is used if that is not set.
  40. Links int
  41. // LinkAttrs is the number of allowed attributes for a span link.
  42. //
  43. // This is resolved from the environment variable value for the
  44. // OTEL_LINK_ATTRIBUTE_COUNT_LIMIT, or 128 is used if that is not set.
  45. LinkAttrs int
  46. }
  47. func newSpanLimits() spanLimits {
  48. return spanLimits{
  49. Attrs: firstEnv(
  50. 128,
  51. "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT",
  52. "OTEL_ATTRIBUTE_COUNT_LIMIT",
  53. ),
  54. AttrValueLen: firstEnv(
  55. -1, // Unlimited.
  56. "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT",
  57. "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT",
  58. ),
  59. Events: firstEnv(128, "OTEL_SPAN_EVENT_COUNT_LIMIT"),
  60. EventAttrs: firstEnv(128, "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"),
  61. Links: firstEnv(128, "OTEL_SPAN_LINK_COUNT_LIMIT"),
  62. LinkAttrs: firstEnv(128, "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT"),
  63. }
  64. }
  65. // firstEnv returns the parsed integer value of the first matching environment
  66. // variable from keys. The defaultVal is returned if the value is not an
  67. // integer or no match is found.
  68. func firstEnv(defaultVal int, keys ...string) int {
  69. for _, key := range keys {
  70. strV := os.Getenv(key)
  71. if strV == "" {
  72. continue
  73. }
  74. v, err := strconv.Atoi(strV)
  75. if err == nil {
  76. return v
  77. }
  78. slog.Warn(
  79. "invalid limit environment variable",
  80. "error", err,
  81. "key", key,
  82. "value", strV,
  83. )
  84. }
  85. return defaultVal
  86. }