Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env support for span limits configuration #2606

Merged
merged 12 commits into from Feb 17, 2022
9 changes: 9 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,15 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

- Added support to configure the span limits with environment variables.
cuichenli marked this conversation as resolved.
Show resolved Hide resolved
The following environment variables are used. (#2606)
- `OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT`
- `OTEL_SPAN_EVENT_COUNT_LIMIT`
- `OTEL_SPAN_LINK_COUNT_LIMIT`

If the provided environment variables are invalid (negative), the default values would be
used.

MrAlias marked this conversation as resolved.
Show resolved Hide resolved
## [1.4.0] - 2022-02-11

### Added
Expand Down
15 changes: 15 additions & 0 deletions sdk/internal/env/env.go
Expand Up @@ -40,6 +40,21 @@ const (
// Note: Must be less than or equal to EnvBatchSpanProcessorMaxQueueSize
// i.e. 512
BatchSpanProcessorMaxExportBatchSizeKey = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE"

// SpanAttributesCountKey
// Maximum allowed span attribute count
// Default: 128
SpanAttributesCountKey = "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT"

// SpanEventCountKey
// Maximum allowed span event count
// Default: 128
SpanEventCountKey = "OTEL_SPAN_EVENT_COUNT_LIMIT"

// SpanLinkCountKey
// Maximum allowed span link count
// Default: 128
SpanLinkCountKey = "OTEL_SPAN_LINK_COUNT_LIMIT"
)

// IntEnvOr returns the int value of the environment variable with name key if
Expand Down
7 changes: 7 additions & 0 deletions sdk/trace/config.go
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

package trace // import "go.opentelemetry.io/otel/sdk/trace"
import "go.opentelemetry.io/otel/sdk/internal/env"

// SpanLimits represents the limits of a span.
type SpanLimits struct {
Expand Down Expand Up @@ -50,6 +51,12 @@ func (sl *SpanLimits) ensureDefault() {
}
}

func (sl *SpanLimits) parsePotentialEnvConfigs() {
sl.AttributeCountLimit = env.IntEnvOr(env.SpanAttributesCountKey, sl.AttributeCountLimit)
sl.LinkCountLimit = env.IntEnvOr(env.SpanLinkCountKey, sl.LinkCountLimit)
sl.EventCountLimit = env.IntEnvOr(env.SpanEventCountKey, sl.EventCountLimit)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}

const (
// DefaultAttributeCountLimit is the default maximum allowed span attribute count.
DefaultAttributeCountLimit = 128
Expand Down
1 change: 1 addition & 0 deletions sdk/trace/provider.go
Expand Up @@ -366,6 +366,7 @@ func ensureValidTracerProviderConfig(cfg tracerProviderConfig) tracerProviderCon
if cfg.idGenerator == nil {
cfg.idGenerator = defaultIDGenerator()
}
cfg.spanLimits.parsePotentialEnvConfigs()
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
cfg.spanLimits.ensureDefault()
if cfg.resource == nil {
cfg.resource = resource.Default()
Expand Down
42 changes: 42 additions & 0 deletions sdk/trace/provider_test.go
Expand Up @@ -17,8 +17,14 @@ package trace
import (
"context"
"errors"
"os"
"testing"

"github.com/stretchr/testify/require"

ottest "go.opentelemetry.io/otel/internal/internaltest"
"go.opentelemetry.io/otel/sdk/internal/env"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -94,3 +100,39 @@ func TestSchemaURL(t *testing.T) {
tracerStruct := tracerIface.(*tracer)
assert.EqualValues(t, schemaURL, tracerStruct.instrumentationLibrary.SchemaURL)
}

func TestEnsureValidTracerProviderConfig(t *testing.T) {
envStore := ottest.NewEnvStore()
defer func() {
require.NoError(t, envStore.Restore())
}()
envStore.Record(env.SpanAttributesCountKey)
envStore.Record(env.SpanEventCountKey)
envStore.Record(env.SpanLinkCountKey)
require.NoError(t, os.Setenv(env.SpanEventCountKey, "111"))
Aneurysm9 marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, os.Setenv(env.SpanAttributesCountKey, "222"))
require.NoError(t, os.Setenv(env.SpanLinkCountKey, "333"))
o := tracerProviderConfig{}
o = ensureValidTracerProviderConfig(o)
assert.Equal(t, 111, o.spanLimits.EventCountLimit)
assert.Equal(t, 222, o.spanLimits.AttributeCountLimit)
assert.Equal(t, 333, o.spanLimits.LinkCountLimit)
}

func TestEnsureValidTracerProviderConfigWithInvliadValue(t *testing.T) {
envStore := ottest.NewEnvStore()
defer func() {
require.NoError(t, envStore.Restore())
}()
envStore.Record(env.SpanAttributesCountKey)
envStore.Record(env.SpanEventCountKey)
envStore.Record(env.SpanLinkCountKey)
require.NoError(t, os.Setenv(env.SpanEventCountKey, "-111"))
require.NoError(t, os.Setenv(env.SpanAttributesCountKey, "222"))
require.NoError(t, os.Setenv(env.SpanLinkCountKey, "-333"))
o := tracerProviderConfig{}
o = ensureValidTracerProviderConfig(o)
assert.Equal(t, 128, o.spanLimits.EventCountLimit)
assert.Equal(t, 222, o.spanLimits.AttributeCountLimit)
assert.Equal(t, 128, o.spanLimits.LinkCountLimit)
}