All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.undefinedlabs.scope.sender.internal.HttpSender Maven / Gradle / Ivy

package com.undefinedlabs.scope.sender.internal;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;
import com.undefinedlabs.scope.exceptions.SenderException;
import com.undefinedlabs.scope.exceptions.SerializerException;
import com.undefinedlabs.scope.logger.ScopeLogger;
import com.undefinedlabs.scope.logger.ScopeLoggerResolver;
import com.undefinedlabs.scope.network.HttpClient;
import com.undefinedlabs.scope.network.NetworkException;
import com.undefinedlabs.scope.network.NoopHttpClient;
import com.undefinedlabs.scope.reporter.internal.remote.model.ScopeData;
import com.undefinedlabs.scope.sender.Sender;
import com.undefinedlabs.scope.sender.internal.serializers.NoopSerializer;
import com.undefinedlabs.scope.sender.internal.serializers.Serializer;
import com.undefinedlabs.scope.statistics.Statistics;
import com.undefinedlabs.scope.utils.Endpoints;
import com.undefinedlabs.scope.utils.StringUtils;
import com.undefinedlabs.scope.utils.props.SystemProps;

public class HttpSender implements Sender {

  public static final String SCOPE_JAVA_USER_AGENT =
      "scope-agent-java"
          + (StringUtils.isNotEmpty(SystemProps.AGENT_VERSION)
              ? "/" + SystemProps.AGENT_VERSION
              : "");

  private static final ScopeLogger LOGGER = ScopeLoggerResolver.INSTANCE.get();

  private final Serializer serializer;
  private final HttpClient httpClient;

  HttpSender(final HttpSender.Builder builder) {
    serializer = builder.serializer;
    httpClient = builder.httpClient;
  }

  @Override
  public boolean send(final ScopeData scopeData) throws SenderException {
    final byte[] scopeDataBytes;

    try {
      scopeDataBytes = serializer.serializeAsBytes(scopeData);
    } catch (final SerializerException e) {
      Statistics.INSTANCE.registerFailedSentSpans(scopeData.getSpans());
      LOGGER.error("ScopeData could not be serialized: " + e);
      throw new SenderException("ScopeData: " + scopeData, e);
    }

    Statistics.INSTANCE.registerSendTaskExecuted();
    try (Response response =
        httpClient.post(
            Endpoints.API_AGENT_INGEST,
            RequestBody.create(MediaType.parse(serializer.getMediaType()), scopeDataBytes))) {
      if (!response.isSuccessful()) {
        Statistics.INSTANCE.registerFailedSentSpans(scopeData.getSpans());
      } else {
        Statistics.INSTANCE.registerSuccessSentSpans(scopeData.getSpans());
      }

      return response.code() < 400;
    } catch (final NetworkException e) {
      throw new SenderException("ScopeData: " + scopeData + ", message:" + e.getMessage(), e);
    }
  }

  @Override
  public void close() {
    // Nothing to do;
  }

  public static class Builder {

    private Serializer serializer;
    private HttpClient httpClient;

    public Builder withHttpClient(final HttpClient httpClient) {
      this.httpClient = httpClient;
      return this;
    }

    public Builder withSerializer(final Serializer serializer) {
      this.serializer = serializer;
      return this;
    }

    public HttpSender build() {
      return new HttpSender(this);
    }

    public static Builder newDefault() {
      try {
        return new Builder()
            .withHttpClient(NoopHttpClient.INSTANCE)
            .withSerializer(NoopSerializer.INSTANCE);
      } catch (final Exception e) {
        throw new IllegalArgumentException(e.getMessage());
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy