Search in sources :

Example 1 with StatsbeatModule

use of com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule in project ApplicationInsights-Java by microsoft.

the class IntegrationTests method setup.

@BeforeEach
public void setup() throws Exception {
    HttpClient mockedClient = mock(HttpClient.class);
    if (testWithException) {
        when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.error(() -> new Exception("this is expected to be logged by the operation logger")));
    } else {
        // 401, 403, 408, 429, 500, and 503 response codes result in storing to disk
        when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.just(new MockHttpResponse(invocation.getArgument(0, HttpRequest.class), 500)));
    }
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
    localFileCache = new LocalFileCache(tempFolder);
    localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
    StatsbeatModule statsbeatModule = Mockito.mock(StatsbeatModule.class);
    when(statsbeatModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
    telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), new LocalFileWriter(localFileCache, tempFolder, null), statsbeatModule, false);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) Context(com.azure.core.util.Context) NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) TelemetryChannel(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel) URL(java.net.URL) HttpClient(com.azure.core.http.HttpClient) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) MockHttpResponse(com.azure.core.test.http.MockHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with StatsbeatModule

use of com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule 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;
}
Also used : InvalidConnectionStringException(com.microsoft.applicationinsights.agent.internal.telemetry.InvalidConnectionStringException) AiLazyConfiguration(io.opentelemetry.instrumentation.api.aisdk.AiLazyConfiguration) Configuration(com.microsoft.applicationinsights.agent.internal.configuration.Configuration) RpConfiguration(com.microsoft.applicationinsights.agent.internal.configuration.RpConfiguration) ProfilerConfiguration(com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProfilerConfiguration) ConnectionString(com.microsoft.applicationinsights.agent.internal.telemetry.ConnectionString) CountDownLatch(java.util.concurrent.CountDownLatch) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) FriendlyException(com.microsoft.applicationinsights.agent.internal.common.FriendlyException) ProcessorConfig(com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig) BytecodeUtilImpl(com.microsoft.applicationinsights.agent.internal.legacysdk.BytecodeUtilImpl) MetricFilter(com.microsoft.applicationinsights.agent.internal.telemetry.MetricFilter) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) RpConfiguration(com.microsoft.applicationinsights.agent.internal.configuration.RpConfiguration) File(java.io.File)

Example 3 with StatsbeatModule

use of com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule in project ApplicationInsights-Java by microsoft.

the class TelemetryChannelTest method getTelemetryChannel.

private TelemetryChannel getTelemetryChannel() throws MalformedURLException {
    List<HttpPipelinePolicy> policies = new ArrayList<>();
    policies.add(new RedirectPolicy(Cache.bounded(5)));
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(recordingHttpClient);
    LocalFileCache localFileCache = new LocalFileCache(tempFolder);
    StatsbeatModule mockedStatsModule = Mockito.mock(StatsbeatModule.class);
    when(mockedStatsModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
    return new TelemetryChannel(pipelineBuilder.build(), new URL(END_POINT_URL), new LocalFileWriter(localFileCache, tempFolder, null), mockedStatsModule, false);
}
Also used : HttpPipelinePolicy(com.azure.core.http.policy.HttpPipelinePolicy) RedirectPolicy(com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy) LocalFileCache(com.microsoft.applicationinsights.agent.internal.localstorage.LocalFileCache) NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) LocalFileWriter(com.microsoft.applicationinsights.agent.internal.localstorage.LocalFileWriter) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) ArrayList(java.util.ArrayList) URL(java.net.URL)

Example 4 with StatsbeatModule

use of com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule in project ApplicationInsights-Java by microsoft.

the class LocalFileLoaderTests method testDeleteFilePermanentlyOnSuccess.

@Test
public void testDeleteFilePermanentlyOnSuccess() throws Exception {
    HttpClient mockedClient = getMockHttpClientSuccess();
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
    LocalFileCache localFileCache = new LocalFileCache(tempFolder);
    LocalFileWriter localFileWriter = new LocalFileWriter(localFileCache, tempFolder, null);
    LocalFileLoader localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
    StatsbeatModule mockedStatsbeatModule = Mockito.mock(StatsbeatModule.class);
    when(mockedStatsbeatModule.getNetworkStatsbeat()).thenReturn(Mockito.mock(NetworkStatsbeat.class));
    TelemetryChannel telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), localFileWriter, mockedStatsbeatModule, false);
    // persist 10 files to disk
    for (int i = 0; i < 10; i++) {
        localFileWriter.writeToDisk(singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))), INSTRUMENTATION_KEY);
    }
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
    Collection<File> files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
    assertThat(files.size()).isEqualTo(10);
    int expectedCount = 10;
    // send persisted files one by one and then delete it permanently.
    for (int i = 0; i < 10; i++) {
        LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
        CompletableResultCode completableResultCode = telemetryChannel.sendRawBytes(persistedFile.rawBytes, persistedFile.instrumentationKey, () -> {
        }, retryable -> {
        });
        completableResultCode.join(10, SECONDS);
        assertThat(completableResultCode.isSuccess()).isEqualTo(true);
        localFileLoader.updateProcessedFileStatus(true, persistedFile.file);
        // sleep 1 second to wait for delete to complete
        Thread.sleep(1000);
        files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
        assertThat(files.size()).isEqualTo(--expectedCount);
    }
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(0);
}
Also used : NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) TelemetryChannel(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel) URL(java.net.URL) HttpClient(com.azure.core.http.HttpClient) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with StatsbeatModule

use of com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule in project ApplicationInsights-Java by microsoft.

the class LocalFileLoaderTests method testDeleteFilePermanentlyOnFailure.

@Test
public void testDeleteFilePermanentlyOnFailure() throws Exception {
    HttpClient mockedClient = mock(HttpClient.class);
    when(mockedClient.send(any(HttpRequest.class), any(Context.class))).then(invocation -> Mono.error(() -> new Exception("this is expected to be logged by the operation logger")));
    HttpPipelineBuilder pipelineBuilder = new HttpPipelineBuilder().httpClient(mockedClient);
    LocalFileCache localFileCache = new LocalFileCache(tempFolder);
    LocalFileLoader localFileLoader = new LocalFileLoader(localFileCache, tempFolder, null);
    LocalFileWriter localFileWriter = new LocalFileWriter(localFileCache, tempFolder, null);
    StatsbeatModule statsbeatModule = mock(StatsbeatModule.class);
    when(statsbeatModule.getNetworkStatsbeat()).thenReturn(mock(NetworkStatsbeat.class));
    TelemetryChannel telemetryChannel = new TelemetryChannel(pipelineBuilder.build(), new URL("http://foo.bar"), localFileWriter, statsbeatModule, false);
    // persist 10 files to disk
    for (int i = 0; i < 10; i++) {
        localFileWriter.writeToDisk(singletonList(ByteBuffer.wrap("hello world".getBytes(UTF_8))), INSTRUMENTATION_KEY);
    }
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
    Collection<File> files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
    assertThat(files.size()).isEqualTo(10);
    // fail to send persisted files and expect them to be kept on disk
    for (int i = 0; i < 10; i++) {
        LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
        assertThat(persistedFile.instrumentationKey).isEqualTo(INSTRUMENTATION_KEY);
        CompletableResultCode completableResultCode = telemetryChannel.sendRawBytes(persistedFile.rawBytes, persistedFile.instrumentationKey, () -> {
        }, retryable -> {
        });
        completableResultCode.join(10, SECONDS);
        assertThat(completableResultCode.isSuccess()).isEqualTo(false);
        localFileLoader.updateProcessedFileStatus(false, persistedFile.file);
    }
    files = FileUtils.listFiles(tempFolder, new String[] { "trn" }, false);
    assertThat(files.size()).isEqualTo(10);
    assertThat(localFileCache.getPersistedFilesCache().size()).isEqualTo(10);
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) Context(com.azure.core.util.Context) NetworkStatsbeat(com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) TelemetryChannel(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel) IOException(java.io.IOException) URL(java.net.URL) HttpClient(com.azure.core.http.HttpClient) StatsbeatModule(com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

StatsbeatModule (com.microsoft.applicationinsights.agent.internal.statsbeat.StatsbeatModule)5 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)4 NetworkStatsbeat (com.microsoft.applicationinsights.agent.internal.statsbeat.NetworkStatsbeat)4 URL (java.net.URL)4 HttpClient (com.azure.core.http.HttpClient)3 TelemetryChannel (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel)3 File (java.io.File)3 HttpRequest (com.azure.core.http.HttpRequest)2 Context (com.azure.core.util.Context)2 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)2 Test (org.junit.jupiter.api.Test)2 HttpPipelinePolicy (com.azure.core.http.policy.HttpPipelinePolicy)1 MockHttpResponse (com.azure.core.test.http.MockHttpResponse)1 FriendlyException (com.microsoft.applicationinsights.agent.internal.common.FriendlyException)1 Configuration (com.microsoft.applicationinsights.agent.internal.configuration.Configuration)1 ProcessorConfig (com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig)1 ProfilerConfiguration (com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProfilerConfiguration)1 RpConfiguration (com.microsoft.applicationinsights.agent.internal.configuration.RpConfiguration)1 RedirectPolicy (com.microsoft.applicationinsights.agent.internal.httpclient.RedirectPolicy)1 BytecodeUtilImpl (com.microsoft.applicationinsights.agent.internal.legacysdk.BytecodeUtilImpl)1