limit.go

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