Skip to content

Commit

Permalink
General attribute limits configuration factory (#5779)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Aug 30, 2023
1 parent 68d7ab5 commit da79278
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
package io.opentelemetry.sdk.extension.incubator.fileconfig;

import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeLimits;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LogRecordLimits;
import io.opentelemetry.sdk.logs.LogLimits;
import io.opentelemetry.sdk.logs.LogLimitsBuilder;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class LogLimitsFactory implements Factory<LogRecordLimits, LogLimits> {
final class LogLimitsFactory implements Factory<LogRecordLimitsAndAttributeLimits, LogLimits> {

private static final LogLimitsFactory INSTANCE = new LogLimitsFactory();

Expand All @@ -25,18 +26,34 @@ static LogLimitsFactory getInstance() {

@Override
public LogLimits create(
@Nullable LogRecordLimits model, SpiHelper spiHelper, List<Closeable> closeables) {
@Nullable LogRecordLimitsAndAttributeLimits model,
SpiHelper spiHelper,
List<Closeable> closeables) {
if (model == null) {
return LogLimits.getDefault();
}

LogLimitsBuilder builder = LogLimits.builder();
if (model.getAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributes(model.getAttributeCountLimit());

AttributeLimits attributeLimitsModel = model.getAttributeLimits();
if (attributeLimitsModel != null) {
if (attributeLimitsModel.getAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributes(attributeLimitsModel.getAttributeCountLimit());
}
if (attributeLimitsModel.getAttributeValueLengthLimit() != null) {
builder.setMaxAttributeValueLength(attributeLimitsModel.getAttributeValueLengthLimit());
}
}
if (model.getAttributeValueLengthLimit() != null) {
builder.setMaxAttributeValueLength(model.getAttributeValueLengthLimit());

LogRecordLimits logRecordLimitsModel = model.getLogRecordLimits();
if (logRecordLimitsModel != null) {
if (logRecordLimitsModel.getAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributes(logRecordLimitsModel.getAttributeCountLimit());
}
if (logRecordLimitsModel.getAttributeValueLengthLimit() != null) {
builder.setMaxAttributeValueLength(logRecordLimitsModel.getAttributeValueLengthLimit());
}
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import com.google.auto.value.AutoValue;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeLimits;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LogRecordLimits;
import javax.annotation.Nullable;

@AutoValue
abstract class LogRecordLimitsAndAttributeLimits {

static LogRecordLimitsAndAttributeLimits create(
@Nullable AttributeLimits attributeLimits, @Nullable LogRecordLimits spanLimits) {
return new AutoValue_LogRecordLimitsAndAttributeLimits(attributeLimits, spanLimits);
}

@Nullable
abstract AttributeLimits getAttributeLimits();

@Nullable
abstract LogRecordLimits getLogRecordLimits();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import com.google.auto.value.AutoValue;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeLimits;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.LoggerProvider;
import javax.annotation.Nullable;

@AutoValue
abstract class LoggerProviderAndAttributeLimits {

static LoggerProviderAndAttributeLimits create(
@Nullable AttributeLimits attributeLimits, @Nullable LoggerProvider loggerProvider) {
return new AutoValue_LoggerProviderAndAttributeLimits(attributeLimits, loggerProvider);
}

@Nullable
abstract AttributeLimits getAttributeLimits();

@Nullable
abstract LoggerProvider getLoggerProvider();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.util.List;
import javax.annotation.Nullable;

final class LoggerProviderFactory implements Factory<LoggerProvider, SdkLoggerProviderBuilder> {
final class LoggerProviderFactory
implements Factory<LoggerProviderAndAttributeLimits, SdkLoggerProviderBuilder> {

private static final LoggerProviderFactory INSTANCE = new LoggerProviderFactory();

Expand All @@ -27,18 +28,28 @@ static LoggerProviderFactory getInstance() {

@Override
public SdkLoggerProviderBuilder create(
@Nullable LoggerProvider model, SpiHelper spiHelper, List<Closeable> closeables) {
@Nullable LoggerProviderAndAttributeLimits model,
SpiHelper spiHelper,
List<Closeable> closeables) {
SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder();
if (model == null) {
return SdkLoggerProvider.builder();
return builder;
}
LoggerProvider loggerProviderModel = model.getLoggerProvider();
if (loggerProviderModel == null) {
return builder;
}

SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder();

LogLimits logLimits =
LogLimitsFactory.getInstance().create(model.getLimits(), spiHelper, closeables);
LogLimitsFactory.getInstance()
.create(
LogRecordLimitsAndAttributeLimits.create(
model.getAttributeLimits(), loggerProviderModel.getLimits()),
spiHelper,
closeables);
builder.setLogLimits(() -> logLimits);

List<LogRecordProcessor> processors = model.getProcessors();
List<LogRecordProcessor> processors = loggerProviderModel.getProcessors();
if (processors != null) {
processors.forEach(
processor ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ static OpenTelemetryConfigurationFactory getInstance() {
@Override
public OpenTelemetrySdk create(
@Nullable OpenTelemetryConfiguration model, SpiHelper spiHelper, List<Closeable> closeables) {
OpenTelemetrySdkBuilder builder = OpenTelemetrySdk.builder();
if (model == null) {
return FileConfigUtil.addAndReturn(closeables, OpenTelemetrySdk.builder().build());
return FileConfigUtil.addAndReturn(closeables, builder.build());
}

if (!"0.1".equals(model.getFileFormat())) {
throw new ConfigurationException("Unsupported file format. Supported formats include: 0.1");
}

OpenTelemetrySdkBuilder builder = OpenTelemetrySdk.builder();

if (Objects.equals(Boolean.TRUE, model.getDisabled())) {
return builder.build();
}
Expand All @@ -56,7 +55,11 @@ public OpenTelemetrySdk create(
FileConfigUtil.addAndReturn(
closeables,
LoggerProviderFactory.getInstance()
.create(model.getLoggerProvider(), spiHelper, closeables)
.create(
LoggerProviderAndAttributeLimits.create(
model.getAttributeLimits(), model.getLoggerProvider()),
spiHelper,
closeables)
.setResource(resource)
.build()));
}
Expand All @@ -66,7 +69,11 @@ public OpenTelemetrySdk create(
FileConfigUtil.addAndReturn(
closeables,
TracerProviderFactory.getInstance()
.create(model.getTracerProvider(), spiHelper, closeables)
.create(
TracerProviderAndAttributeLimits.create(
model.getAttributeLimits(), model.getTracerProvider()),
spiHelper,
closeables)
.setResource(resource)
.build()));
}
Expand All @@ -81,8 +88,6 @@ public OpenTelemetrySdk create(
.build()));
}

// TODO(jack-berg): add support for general attribute limits

return FileConfigUtil.addAndReturn(closeables, builder.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import com.google.auto.value.AutoValue;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeLimits;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanLimits;
import javax.annotation.Nullable;

@AutoValue
abstract class SpanLimitsAndAttributeLimits {

static SpanLimitsAndAttributeLimits create(
@Nullable AttributeLimits attributeLimits, @Nullable SpanLimits spanLimits) {
return new AutoValue_SpanLimitsAndAttributeLimits(attributeLimits, spanLimits);
}

@Nullable
abstract AttributeLimits getAttributeLimits();

@Nullable
abstract SpanLimits getSpanLimits();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
package io.opentelemetry.sdk.extension.incubator.fileconfig;

import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeLimits;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanLimits;
import io.opentelemetry.sdk.trace.SpanLimitsBuilder;
import java.io.Closeable;
import java.util.List;
import javax.annotation.Nullable;

final class SpanLimitsFactory
implements Factory<SpanLimits, io.opentelemetry.sdk.trace.SpanLimits> {
implements Factory<SpanLimitsAndAttributeLimits, io.opentelemetry.sdk.trace.SpanLimits> {

private static final SpanLimitsFactory INSTANCE = new SpanLimitsFactory();

Expand All @@ -25,29 +26,45 @@ static SpanLimitsFactory getInstance() {

@Override
public io.opentelemetry.sdk.trace.SpanLimits create(
@Nullable SpanLimits model, SpiHelper spiHelper, List<Closeable> closeables) {
@Nullable SpanLimitsAndAttributeLimits model,
SpiHelper spiHelper,
List<Closeable> closeables) {
if (model == null) {
return io.opentelemetry.sdk.trace.SpanLimits.getDefault();
}

SpanLimitsBuilder builder = io.opentelemetry.sdk.trace.SpanLimits.builder();
if (model.getAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributes(model.getAttributeCountLimit());
}
if (model.getAttributeValueLengthLimit() != null) {
builder.setMaxAttributeValueLength(model.getAttributeValueLengthLimit());
}
if (model.getEventCountLimit() != null) {
builder.setMaxNumberOfEvents(model.getEventCountLimit());
}
if (model.getLinkCountLimit() != null) {
builder.setMaxNumberOfLinks(model.getLinkCountLimit());
}
if (model.getEventAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributesPerEvent(model.getEventAttributeCountLimit());

AttributeLimits attributeLimitsModel = model.getAttributeLimits();
if (attributeLimitsModel != null) {
if (attributeLimitsModel.getAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributes(attributeLimitsModel.getAttributeCountLimit());
}
if (attributeLimitsModel.getAttributeValueLengthLimit() != null) {
builder.setMaxAttributeValueLength(attributeLimitsModel.getAttributeValueLengthLimit());
}
}
if (model.getLinkAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributesPerLink(model.getLinkAttributeCountLimit());

SpanLimits spanLimitsModel = model.getSpanLimits();
if (spanLimitsModel != null) {
if (spanLimitsModel.getAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributes(spanLimitsModel.getAttributeCountLimit());
}
if (spanLimitsModel.getAttributeValueLengthLimit() != null) {
builder.setMaxAttributeValueLength(spanLimitsModel.getAttributeValueLengthLimit());
}
if (spanLimitsModel.getEventCountLimit() != null) {
builder.setMaxNumberOfEvents(spanLimitsModel.getEventCountLimit());
}
if (spanLimitsModel.getLinkCountLimit() != null) {
builder.setMaxNumberOfLinks(spanLimitsModel.getLinkCountLimit());
}
if (spanLimitsModel.getEventAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributesPerEvent(spanLimitsModel.getEventAttributeCountLimit());
}
if (spanLimitsModel.getLinkAttributeCountLimit() != null) {
builder.setMaxNumberOfAttributesPerLink(spanLimitsModel.getLinkAttributeCountLimit());
}
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.extension.incubator.fileconfig;

import com.google.auto.value.AutoValue;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.AttributeLimits;
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TracerProvider;
import javax.annotation.Nullable;

@AutoValue
abstract class TracerProviderAndAttributeLimits {

static TracerProviderAndAttributeLimits create(
@Nullable AttributeLimits attributeLimits, @Nullable TracerProvider tracerProvider) {
return new AutoValue_TracerProviderAndAttributeLimits(attributeLimits, tracerProvider);
}

@Nullable
abstract AttributeLimits getAttributeLimits();

@Nullable
abstract TracerProvider getTracerProvider();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import java.util.List;
import javax.annotation.Nullable;

final class TracerProviderFactory implements Factory<TracerProvider, SdkTracerProviderBuilder> {
final class TracerProviderFactory
implements Factory<TracerProviderAndAttributeLimits, SdkTracerProviderBuilder> {

private static final TracerProviderFactory INSTANCE = new TracerProviderFactory();

Expand All @@ -28,22 +29,33 @@ static TracerProviderFactory getInstance() {

@Override
public SdkTracerProviderBuilder create(
@Nullable TracerProvider model, SpiHelper spiHelper, List<Closeable> closeables) {
@Nullable TracerProviderAndAttributeLimits model,
SpiHelper spiHelper,
List<Closeable> closeables) {
SdkTracerProviderBuilder builder = SdkTracerProvider.builder();
if (model == null) {
return SdkTracerProvider.builder();
return builder;
}
TracerProvider tracerProviderModel = model.getTracerProvider();
if (tracerProviderModel == null) {
return builder;
}

SdkTracerProviderBuilder builder = SdkTracerProvider.builder();

SpanLimits spanLimits =
SpanLimitsFactory.getInstance().create(model.getLimits(), spiHelper, closeables);
SpanLimitsFactory.getInstance()
.create(
SpanLimitsAndAttributeLimits.create(
model.getAttributeLimits(), tracerProviderModel.getLimits()),
spiHelper,
closeables);
builder.setSpanLimits(spanLimits);

Sampler sampler =
SamplerFactory.getInstance().create(model.getSampler(), spiHelper, closeables);
SamplerFactory.getInstance()
.create(tracerProviderModel.getSampler(), spiHelper, closeables);
builder.setSampler(sampler);

List<SpanProcessor> processors = model.getProcessors();
List<SpanProcessor> processors = tracerProviderModel.getProcessors();
if (processors != null) {
processors.forEach(
processor ->
Expand Down
Loading

0 comments on commit da79278

Please sign in to comment.