Client.prototype.config = function()

in lib/apm-client/http-apm-client/index.js [240:348]


Client.prototype.config = function (opts) {
  this._conf = Object.assign(this._conf || {}, opts);

  this._conf.globalLabels = normalizeGlobalLabels(this._conf.globalLabels);

  const missing = requiredOpts.filter((name) => !this._conf[name]);
  if (missing.length > 0)
    throw new Error('Missing required option(s): ' + missing.join(', '));

  // default values
  if (!this._conf.size && this._conf.size !== 0) this._conf.size = 750 * 1024;
  if (!this._conf.time && this._conf.time !== 0) this._conf.time = 10000;
  if (!this._conf.serverTimeout && this._conf.serverTimeout !== 0)
    this._conf.serverTimeout = 15000;
  if (!this._conf.serverUrl) this._conf.serverUrl = 'http://127.0.0.1:8200';
  if (!this._conf.truncateKeywordsAt) this._conf.truncateKeywordsAt = 1024;
  if (!this._conf.truncateStringsAt) this._conf.truncateStringsAt = 1024;
  if (!this._conf.truncateCustomKeysAt) this._conf.truncateCustomKeysAt = 1024;
  if (!this._conf.truncateLongFieldsAt) this._conf.truncateLongFieldsAt = 10000;
  // The deprecated `truncateErrorMessagesAt` will be honored if specified.
  if (!this._conf.bufferWindowTime) this._conf.bufferWindowTime = 20;
  if (!this._conf.bufferWindowSize) this._conf.bufferWindowSize = 50;
  if (!this._conf.maxQueueSize) this._conf.maxQueueSize = 1024;
  if (!this._conf.intakeResTimeout) this._conf.intakeResTimeout = 10000;
  if (!this._conf.intakeResTimeoutOnEnd)
    this._conf.intakeResTimeoutOnEnd = 1000;
  this._conf.keepAlive = this._conf.keepAlive !== false;
  this._conf.centralConfig = this._conf.centralConfig || false;
  if (!('keepAliveMsecs' in this._conf)) this._conf.keepAliveMsecs = 1000;
  if (!('maxSockets' in this._conf)) this._conf.maxSockets = Infinity;
  if (!('maxFreeSockets' in this._conf)) this._conf.maxFreeSockets = 256;
  if (!('freeSocketTimeout' in this._conf)) this._conf.freeSocketTimeout = 4000;

  // processed values
  this._conf.serverUrl = new URL(this._conf.serverUrl);

  this._conf.detectedHostname = detectHostname();

  if (containerInfo) {
    if (!this._conf.containerId && containerInfo.containerId) {
      this._conf.containerId = containerInfo.containerId;
    }
    if (!this._conf.kubernetesPodUID && containerInfo.podId) {
      this._conf.kubernetesPodUID = containerInfo.podId;
    }
    if (!this._conf.kubernetesPodName && containerInfo.podId) {
      // https://kubernetes.io/docs/concepts/workloads/pods/#working-with-pods
      // suggests a pod name should just be the shorter "DNS label", and my
      // guess is k8s defaults a pod name to just the *short* hostname, not
      // the FQDN.
      this._conf.kubernetesPodName = this._conf.detectedHostname.split(
        '.',
        1,
      )[0];
    }
  }

  let AgentKeepAlive;
  switch (this._conf.serverUrl.protocol) {
    case 'http:':
      this._transport = http;
      this._transportRequest = httpRequest;
      this._transportGet = httpGet;
      AgentKeepAlive = HttpAgentKeepAlive;
      break;
    case 'https:':
      this._transport = https;
      this._transportRequest = httpsRequest;
      this._transportGet = httpsGet;
      AgentKeepAlive = HttpsAgentKeepAlive;
      break;
    default:
      throw new Error('Unknown protocol ' + this._conf.serverUrl.protocol);
  }

  // Only reset `this._agent` if the serverUrl has changed to avoid
  // unnecessarily abandoning keep-alive connections.
  if (!this._agent || (opts && 'serverUrl' in opts)) {
    if (this._agent) {
      this._agent.destroy();
    }
    this._agent = new AgentKeepAlive({
      keepAlive: this._conf.keepAlive,
      keepAliveMsecs: this._conf.keepAliveMsecs,
      freeSocketTimeout: this._conf.freeSocketTimeout,
      timeout: this._conf.serverTimeout,
      maxSockets: this._conf.maxSockets,
      maxFreeSockets: this._conf.maxFreeSockets,
    });
  }

  // http request options
  this._conf.requestIntake = getIntakeRequestOptions(this._conf, this._agent);
  this._conf.requestConfig = getConfigRequestOptions(this._conf, this._agent);
  this._conf.requestSignalLambdaEnd = getSignalLambdaEndRequestOptions(
    this._conf,
    this._agent,
  );
  this._conf.requestRegisterTransaction = getRegisterTransactionRequestOptions(
    this._conf,
    this._agent,
  );

  // fixes bug where cached/memoized _encodedMetadata wouldn't be
  // updated when client was reconfigured
  if (this._encodedMetadata) {
    this._resetEncodedMetadata();
  }
};