protected HttpURLConnection startRequest()

in apm-agent-core/src/main/java/co/elastic/apm/agent/report/AbstractIntakeApiHandler.java [94:157]


    protected HttpURLConnection startRequest(String endpoint) throws Exception {
        payloadSerializer.blockUntilReady();
        final HttpURLConnection connection = apmServerClient.startRequest(endpoint);
        if (connection == null) {
            return null;
        }
        try (UrlConnectionUtils.ContextClassloaderScope clScope = UrlConnectionUtils.withContextClassloaderOf(connection)){
            if (logger.isDebugEnabled()) {
                logger.debug("Starting new request to {}", connection.getURL());
            }
            boolean useCompression = !isLocalhost(connection);
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setChunkedStreamingMode(SerializationConstants.BUFFER_SIZE);
            if (useCompression) {
                connection.setRequestProperty("Content-Encoding", "deflate");
            }
            connection.setRequestProperty("Content-Type", "application/x-ndjson");
            connection.setUseCaches(false);
            connection.connect();
            countingOs = new CountingOutputStream(connection.getOutputStream()); // TODO : here
            if (useCompression) {
                os = new DeflaterOutputStream(countingOs, deflater, true);
            } else {
                os = countingOs;
            }
            payloadSerializer.setOutputStream(os);
            payloadSerializer.appendMetaDataNdJsonToStream();
            payloadSerializer.flushToOutputStream();
            requestStartedNanos = System.nanoTime();
        } catch (IOException e) {
            try {
                logger.error("Error trying to connect to APM Server at {}. Although not necessarily related to SSL, some related SSL " +
                    "configurations corresponding the current connection are logged at INFO level.", connection.getURL());
                if (logger.isInfoEnabled() && connection instanceof HttpsURLConnection) {
                    HttpsURLConnection httpsURLConnection = (HttpsURLConnection) connection;
                    try {
                        logger.info("Cipher suite used for this connection: {}", httpsURLConnection.getCipherSuite());
                    } catch (Exception e1) {
                        SSLSocketFactory sslSocketFactory = httpsURLConnection.getSSLSocketFactory();
                        logger.info("Default cipher suites: {}", Arrays.toString(sslSocketFactory.getDefaultCipherSuites()));
                        logger.info("Supported cipher suites: {}", Arrays.toString(sslSocketFactory.getSupportedCipherSuites()));
                    }
                    try {
                        logger.info("APM Server certificates: {}", Arrays.toString(httpsURLConnection.getServerCertificates()));
                    } catch (Exception e1) {
                        // ignore - invalid
                    }
                    try {
                        logger.info("Local certificates: {}", Arrays.toString(httpsURLConnection.getLocalCertificates()));
                    } catch (Exception e1) {
                        // ignore - invalid
                    }
                }
            } finally {
                closeAndSuppressErrors(connection);
            }
            throw e;
        } catch (Throwable t) {
            closeAndSuppressErrors(connection);
            throw t;
        }
        return connection;
    }