use of com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig in project ApplicationInsights-Java by microsoft.
the class ExporterWithSpanProcessorTest method simpleRenameSpanWithSeparatorTest.
@Test
void simpleRenameSpanWithSeparatorTest() {
MockExporter mockExporter = new MockExporter();
ProcessorConfig config = new ProcessorConfig();
config.type = ProcessorType.SPAN;
config.id = "SimpleRenameSpanWithSeparator";
config.name = new NameConfig();
config.name.fromAttributes = Arrays.asList("db.svc", "operation", "id");
config.name.separator = "::";
SpanExporter exampleExporter = new ExporterWithSpanProcessor(config, mockExporter);
Span span = tracer.spanBuilder("svcA").setAttribute("one", "1").setAttribute("two", 2L).setAttribute("db.svc", "location").setAttribute("operation", "get").setAttribute("id", "1234").startSpan();
SpanData spanData = ((ReadableSpan) span).toSpanData();
List<SpanData> spans = new ArrayList<>();
spans.add(spanData);
exampleExporter.export(spans);
// verify that resulting spans are filtered in the way we want
List<SpanData> result = mockExporter.getSpans();
SpanData resultSpan = result.get(0);
assertThat(resultSpan.getName()).isEqualTo("location::get::1234");
}
use of com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig in project ApplicationInsights-Java by microsoft.
the class OpenTelemetryConfigurer method createExporter.
private static BatchSpanProcessor createExporter(Configuration configuration) {
List<ProcessorConfig> processors = configuration.preview.processors.stream().filter(processor -> processor.type != Configuration.ProcessorType.METRIC_FILTER).collect(Collectors.toCollection(ArrayList::new));
// Reversing the order of processors before passing it to SpanProcessor
Collections.reverse(processors);
SpanExporter currExporter = new Exporter(TelemetryClient.getActive(), configuration.preview.captureHttpServer4xxAsError);
// flushing TelemetryClient
if (!processors.isEmpty()) {
for (ProcessorConfig processorConfig : processors) {
switch(processorConfig.type) {
case ATTRIBUTE:
currExporter = new ExporterWithAttributeProcessor(processorConfig, currExporter);
break;
case SPAN:
currExporter = new ExporterWithSpanProcessor(processorConfig, currExporter);
break;
case LOG:
currExporter = new ExporterWithLogProcessor(processorConfig, currExporter);
break;
default:
throw new IllegalStateException("Not an expected ProcessorType: " + processorConfig.type);
}
}
// this is temporary until semantic attributes stabilize and we make breaking change
// then can use java.util.functions.Predicate<Attributes>
currExporter = new BackCompatHttpUrlProcessor(currExporter);
}
// using BatchSpanProcessor in order to get off of the application thread as soon as possible
BatchSpanProcessorBuilder builder = BatchSpanProcessor.builder(currExporter);
String delayMillisStr = System.getenv("APPLICATIONINSIGHTS_PREVIEW_BSP_SCHEDULE_DELAY");
if (delayMillisStr != null) {
// experimenting with flushing at small interval instead of using batch size 1
// (suspect this may be better performance on small containers)
builder.setScheduleDelay(Duration.ofMillis(Integer.parseInt(delayMillisStr)));
} else {
// using batch size 1 because need to convert to SpanData as soon as possible to grab data for
// live metrics. the real batching is done at a lower level
builder.setMaxExportBatchSize(1);
}
return builder.build();
}
use of com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig in project ApplicationInsights-Java by microsoft.
the class AiComponentInstaller method start.
private static AppIdSupplier start(Instrumentation instrumentation) {
String codelessSdkNamePrefix = getCodelessSdkNamePrefix();
if (codelessSdkNamePrefix != null) {
PropertyHelper.setSdkNamePrefix(codelessSdkNamePrefix);
}
File javaTmpDir = new File(System.getProperty("java.io.tmpdir"));
boolean readOnlyFileSystem = false;
if (javaTmpDir.canRead() && !javaTmpDir.canWrite()) {
readOnlyFileSystem = true;
}
if (!readOnlyFileSystem) {
File tmpDir = new File(javaTmpDir, "applicationinsights-java");
if (!tmpDir.exists() && !tmpDir.mkdirs()) {
throw new IllegalStateException("Could not create directory: " + tmpDir.getAbsolutePath());
}
} else {
startupLogger.info("Detected running on a read-only file system, telemetry will not be stored to disk or retried later on sporadic network failures. If this is unexpected, please check that the process has write access to the temp directory: " + javaTmpDir.getAbsolutePath());
}
Configuration config = MainEntryPoint.getConfiguration();
if (!hasConnectionStringOrInstrumentationKey(config)) {
if (!"java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
throw new FriendlyException("No connection string or instrumentation key provided", "Please provide connection string or instrumentation key.");
}
}
// TODO (trask) should configuration validation be performed earlier?
for (Configuration.SamplingOverride samplingOverride : config.preview.sampling.overrides) {
samplingOverride.validate();
}
for (Configuration.InstrumentationKeyOverride instrumentationKeyOverride : config.preview.instrumentationKeyOverrides) {
instrumentationKeyOverride.validate();
}
for (ProcessorConfig processorConfig : config.preview.processors) {
processorConfig.validate();
}
// validate authentication configuration
config.preview.authentication.validate();
String jbossHome = System.getenv("JBOSS_HOME");
if (!Strings.isNullOrEmpty(jbossHome)) {
// this is used to delay SSL initialization because SSL initialization triggers loading of
// java.util.logging (starting with Java 8u231)
// and JBoss/Wildfly need to install their own JUL manager before JUL is initialized
LazyHttpClient.safeToInitLatch = new CountDownLatch(1);
instrumentation.addTransformer(new JulListeningClassFileTransformer(LazyHttpClient.safeToInitLatch));
}
if (config.proxy.host != null) {
LazyHttpClient.proxyHost = config.proxy.host;
LazyHttpClient.proxyPortNumber = config.proxy.port;
LazyHttpClient.proxyUsername = config.proxy.username;
LazyHttpClient.proxyPassword = config.proxy.password;
}
List<MetricFilter> metricFilters = config.preview.processors.stream().filter(processor -> processor.type == Configuration.ProcessorType.METRIC_FILTER).map(MetricFilter::new).collect(Collectors.toList());
Cache<String, String> ikeyEndpointMap = Cache.bounded(100);
StatsbeatModule statsbeatModule = new StatsbeatModule(ikeyEndpointMap);
TelemetryClient telemetryClient = TelemetryClient.builder().setCustomDimensions(config.customDimensions).setMetricFilters(metricFilters).setIkeyEndpointMap(ikeyEndpointMap).setStatsbeatModule(statsbeatModule).setReadOnlyFileSystem(readOnlyFileSystem).setGeneralExportQueueSize(config.preview.generalExportQueueCapacity).setMetricsExportQueueSize(config.preview.metricsExportQueueCapacity).setAadAuthentication(config.preview.authentication).build();
TelemetryClientInitializer.initialize(telemetryClient, config);
TelemetryClient.setActive(telemetryClient);
try {
ConnectionString.updateStatsbeatConnectionString(config.internal.statsbeat.instrumentationKey, config.internal.statsbeat.endpoint, telemetryClient);
} catch (InvalidConnectionStringException ex) {
startupLogger.warn("Statsbeat endpoint is invalid. {}", ex.getMessage());
}
BytecodeUtilImpl.samplingPercentage = config.sampling.percentage;
AppIdSupplier appIdSupplier = new AppIdSupplier(telemetryClient);
AiAppId.setSupplier(appIdSupplier);
if (config.preview.profiler.enabled) {
if (readOnlyFileSystem) {
throw new FriendlyException("Profile is not supported in a read-only file system.", "disable profiler or use a writable file system");
}
ProfilerServiceInitializer.initialize(appIdSupplier::get, SystemInformation.getProcessId(), formServiceProfilerConfig(config.preview.profiler), config.role.instance, config.role.name, telemetryClient, formApplicationInsightsUserAgent(), formGcEventMonitorConfiguration(config.preview.gcEvents));
}
// this is for Azure Function Linux consumption plan support.
if ("java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
AiLazyConfiguration.setAccessor(new LazyConfigurationAccessor(telemetryClient, appIdSupplier));
}
// this is currently used by Micrometer instrumentation in addition to 2.x SDK
BytecodeUtil.setDelegate(new BytecodeUtilImpl());
Runtime.getRuntime().addShutdownHook(new ShutdownHook(telemetryClient));
RpConfiguration rpConfiguration = MainEntryPoint.getRpConfiguration();
if (rpConfiguration != null) {
RpConfigurationPolling.startPolling(rpConfiguration, config, telemetryClient, appIdSupplier);
}
// initialize StatsbeatModule
statsbeatModule.start(telemetryClient, config);
// start local File purger scheduler task
if (!readOnlyFileSystem) {
LocalFilePurger.startPurging();
}
return appIdSupplier;
}
use of com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig in project ApplicationInsights-Java by microsoft.
the class ConfigurationTest method shouldParseProcessorConfiguration.
@Test
void shouldParseProcessorConfiguration() throws IOException {
Configuration configuration = loadConfiguration("ApplicationInsights_SpanProcessor.json");
PreviewConfiguration preview = configuration.preview;
assertThat(configuration.connectionString).isEqualTo("InstrumentationKey=00000000-0000-0000-0000-000000000000");
assertThat(preview.processors.size()).isEqualTo(10);
// insert config test
ProcessorConfig insertConfig = preview.processors.get(0);
assertThat(insertConfig.id).isEqualTo("attributes/insert");
assertThat(insertConfig.type).isEqualTo(ProcessorType.ATTRIBUTE);
assertThat(insertConfig.actions.get(0).action).isEqualTo(ProcessorActionType.INSERT);
assertThat(insertConfig.actions.get(0).value).isEqualTo("123");
assertThat(insertConfig.actions.get(0).key).isEqualTo(AttributeKey.stringKey("attribute1"));
assertThat(insertConfig.actions.get(1).fromAttribute).isEqualTo(AttributeKey.stringKey("anotherKey"));
// update config test
ProcessorConfig updateConfig = preview.processors.get(1);
assertThat(updateConfig.id).isEqualTo("attributes/update");
assertThat(updateConfig.type).isEqualTo(ProcessorType.ATTRIBUTE);
assertThat(updateConfig.actions.get(0).action).isEqualTo(ProcessorActionType.UPDATE);
assertThat(updateConfig.actions.get(0).key).isEqualTo(AttributeKey.stringKey("boo"));
assertThat(updateConfig.actions.get(0).fromAttribute).isEqualTo(AttributeKey.stringKey("foo"));
assertThat(updateConfig.actions.get(1).key).isEqualTo(AttributeKey.stringKey("db.secret"));
// selective processing test
ProcessorConfig selectiveConfig = preview.processors.get(2);
assertThat(selectiveConfig.type).isEqualTo(ProcessorType.ATTRIBUTE);
assertThat(selectiveConfig.id).isEqualTo("attributes/selectiveProcessing");
assertThat(selectiveConfig.include.matchType).isEqualTo(MatchType.STRICT);
assertThat(selectiveConfig.include.spanNames.size()).isEqualTo(2);
assertThat(selectiveConfig.include.spanNames.get(0)).isEqualTo("svcA");
assertThat(selectiveConfig.exclude.matchType).isEqualTo(MatchType.STRICT);
assertThat(selectiveConfig.exclude.attributes.size()).isEqualTo(1);
assertThat(selectiveConfig.exclude.attributes.get(0).key).isEqualTo("redact_trace");
assertThat(selectiveConfig.exclude.attributes.get(0).value).isEqualTo("false");
assertThat(selectiveConfig.actions.size()).isEqualTo(2);
assertThat(selectiveConfig.actions.get(0).key).isEqualTo(AttributeKey.stringKey("credit_card"));
assertThat(selectiveConfig.actions.get(0).action).isEqualTo(ProcessorActionType.DELETE);
// log/update name test
ProcessorConfig logUpdateNameConfig = preview.processors.get(3);
assertThat(logUpdateNameConfig.type).isEqualTo(ProcessorType.LOG);
assertThat(logUpdateNameConfig.id).isEqualTo("log/updateName");
assertThat(logUpdateNameConfig.body.fromAttributes.size()).isEqualTo(1);
assertThat(logUpdateNameConfig.body.fromAttributes.get(0)).isEqualTo("loggerName");
assertThat(logUpdateNameConfig.body.separator).isEqualTo("::");
// log/extractAttributes
ProcessorConfig logExtractAttributesConfig = preview.processors.get(4);
assertThat(logExtractAttributesConfig.type).isEqualTo(ProcessorType.LOG);
assertThat(logExtractAttributesConfig.id).isEqualTo("log/extractAttributes");
assertThat(logExtractAttributesConfig.body.toAttributes.rules.size()).isEqualTo(1);
assertThat(logExtractAttributesConfig.body.toAttributes.rules.get(0)).isEqualTo("^/api/v1/document/(?<documentId>.*)/update$");
// span/update name test
ProcessorConfig spanUpdateNameConfig = preview.processors.get(5);
assertThat(spanUpdateNameConfig.type).isEqualTo(ProcessorType.SPAN);
assertThat(spanUpdateNameConfig.id).isEqualTo("span/updateName");
assertThat(spanUpdateNameConfig.include.matchType).isEqualTo(MatchType.REGEXP);
assertThat(spanUpdateNameConfig.include.spanNames.size()).isEqualTo(1);
assertThat(spanUpdateNameConfig.include.spanNames.get(0)).isEqualTo(".*password.*");
assertThat(spanUpdateNameConfig.name.fromAttributes.size()).isEqualTo(1);
assertThat(spanUpdateNameConfig.name.fromAttributes.get(0)).isEqualTo("loggerName");
assertThat(spanUpdateNameConfig.name.separator).isEqualTo("::");
// span/extractAttributes
ProcessorConfig spanExtractAttributesConfig = preview.processors.get(6);
assertThat(spanExtractAttributesConfig.type).isEqualTo(ProcessorType.SPAN);
assertThat(spanExtractAttributesConfig.id).isEqualTo("span/extractAttributes");
assertThat(spanExtractAttributesConfig.name.toAttributes.rules.size()).isEqualTo(1);
assertThat(spanExtractAttributesConfig.name.toAttributes.rules.get(0)).isEqualTo("^/api/v1/document/(?<documentId>.*)/update$");
// attribute/extract
ProcessorConfig attributesExtractConfig = preview.processors.get(7);
assertThat(attributesExtractConfig.type).isEqualTo(ProcessorType.ATTRIBUTE);
assertThat(attributesExtractConfig.id).isEqualTo("attributes/extract");
assertThat(attributesExtractConfig.actions.size()).isEqualTo(1);
assertThat(attributesExtractConfig.actions.get(0).action).isEqualTo(ProcessorActionType.EXTRACT);
assertThat(attributesExtractConfig.actions.get(0).key).isEqualTo(AttributeKey.stringKey("http.url"));
assertThat(attributesExtractConfig.actions.get(0).extractAttribute).isNotNull();
assertThat(attributesExtractConfig.actions.get(0).extractAttribute.pattern).isNotNull();
assertThat(attributesExtractConfig.actions.get(0).extractAttribute.groupNames.size()).isEqualTo(4);
assertThat(attributesExtractConfig.actions.get(0).extractAttribute.groupNames.get(0)).isEqualTo("httpProtocol");
// metric-filter
ProcessorConfig metricFilterConfig = preview.processors.get(8);
assertThat(metricFilterConfig.type).isEqualTo(ProcessorType.METRIC_FILTER);
assertThat(metricFilterConfig.id).isEqualTo("metric-filter/exclude-two-metrics");
assertThat(metricFilterConfig.exclude.matchType).isEqualTo(MatchType.STRICT);
assertThat(metricFilterConfig.exclude.metricNames.size()).isEqualTo(2);
assertThat(metricFilterConfig.exclude.metricNames.get(0)).isEqualTo("a_test_metric");
assertThat(metricFilterConfig.exclude.metricNames.get(1)).isEqualTo("another_test_metric");
// attribute/mask
ProcessorConfig attributesMaskConfig = preview.processors.get(9);
assertThat(attributesMaskConfig.type).isEqualTo(ProcessorType.ATTRIBUTE);
assertThat(attributesMaskConfig.id).isEqualTo("attributes/mask");
assertThat(attributesMaskConfig.actions.size()).isEqualTo(1);
assertThat(attributesMaskConfig.actions.get(0).action).isEqualTo(ProcessorActionType.MASK);
assertThat(attributesMaskConfig.actions.get(0).key).isEqualTo(AttributeKey.stringKey("http.url"));
assertThat(attributesMaskConfig.actions.get(0).maskAttribute).isNotNull();
assertThat(attributesMaskConfig.actions.get(0).maskAttribute.pattern).isNotNull();
assertThat(attributesMaskConfig.actions.get(0).maskAttribute.groupNames.size()).isEqualTo(3);
assertThat(attributesMaskConfig.actions.get(0).maskAttribute.groupNames.get(0)).isEqualTo("uriNoCard");
assertThat(attributesMaskConfig.actions.get(0).maskAttribute.replace).isEqualTo("${uriNoCard}****${cardEnd}");
}
use of com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig in project ApplicationInsights-Java by microsoft.
the class ExporterWithAttributeProcessorTest method simpleExcludeRegexTest.
@Test
void simpleExcludeRegexTest() {
MockExporter mockExporter = new MockExporter();
ProcessorConfig config = new ProcessorConfig();
config.type = ProcessorType.ATTRIBUTE;
config.id = "simpleExcludeRegex";
config.exclude = new ProcessorIncludeExclude();
config.exclude.matchType = MatchType.REGEXP;
config.exclude.spanNames = Collections.singletonList("svc.*");
ProcessorAction action = new ProcessorAction("testKey", ProcessorActionType.UPDATE, "redacted", null, null, null);
List<ProcessorAction> actions = new ArrayList<>();
actions.add(action);
config.actions = actions;
SpanExporter exampleExporter = new ExporterWithAttributeProcessor(config, mockExporter);
Span spanA = tracer.spanBuilder("svcA").setAttribute("one", "1").setAttribute("two", 2L).setAttribute("testKey", "testValue").setAttribute("testKey2", "testValue2").startSpan();
Span spanB = tracer.spanBuilder("svcB").setAttribute("one", "1").setAttribute("testKey", "testValue").setAttribute("testKey2", "testValue2").startSpan();
Span spanC = tracer.spanBuilder("serviceC").setAttribute("two", 2L).setAttribute("testKey", "testValue").setAttribute("testKey2", "testValue2").startSpan();
Span spanD = tracer.spanBuilder("serviceD").setAttribute("one", "1").setAttribute("two", 2L).setAttribute("testKey", "testValue").setAttribute("testKey2", "testValue2").startSpan();
List<SpanData> spans = new ArrayList<>();
spans.add(((ReadableSpan) spanA).toSpanData());
spans.add(((ReadableSpan) spanB).toSpanData());
spans.add(((ReadableSpan) spanC).toSpanData());
spans.add(((ReadableSpan) spanD).toSpanData());
exampleExporter.export(spans);
// verify that resulting spans are filtered in the way we want
List<SpanData> result = mockExporter.getSpans();
SpanData resultSpanA = result.get(0);
SpanData resultSpanB = result.get(1);
SpanData resultSpanC = result.get(2);
SpanData resultSpanD = result.get(3);
assertThat(resultSpanA.getAttributes().get(AttributeKey.stringKey("testKey"))).isEqualTo("testValue");
assertThat(resultSpanB.getAttributes().get(AttributeKey.stringKey("testKey"))).isEqualTo("testValue");
assertThat(resultSpanC.getAttributes().get(AttributeKey.stringKey("testKey"))).isEqualTo("redacted");
assertThat(resultSpanD.getAttributes().get(AttributeKey.stringKey("testKey"))).isEqualTo("redacted");
}
Aggregations