constructor()

in lib/config/config.js [123:226]


  constructor(opts, logger) {
    // TODO: remove this call once config and logging-preamble tests are refactored
    readEnvOptions();

    const isLambda = isLambdaExecutionEnvironment();
    const envOptions = getEnvironmentOptions();

    setStartOptions(opts);

    Object.assign(
      this,
      confDefaultOptions, // default options
      getFileOptions(), // options read from config file
      opts, // options passed in to agent.start()
      envOptions, // options read from environment variables
    );

    // The logger is used later in this function, so create/update it first.
    // Unless a new custom `logger` was provided, we use the one created earlier
    // in `configLogger()`.
    const customLogger =
      process.env.ELASTIC_APM_LOGGER === 'false' ? null : this.logger;
    if (!customLogger && logger) {
      logging.setLogLevel(logger, this.logLevel);
      this.logger = logger;
    } else {
      this.logger = logging.createLogger(this.logLevel, customLogger);
    }

    // Fallback and validation handling for `serviceName` and `serviceVersion`.
    if (this.serviceName) {
      // A value here means an explicit value was given. Error out if invalid.
      try {
        validateServiceName(this.serviceName);
      } catch (err) {
        this.logger.error(
          'serviceName "%s" is invalid: %s',
          this.serviceName,
          err.message,
        );
        this.serviceName = null;
      }
    } else {
      if (isLambda) {
        this.serviceName = process.env.AWS_LAMBDA_FUNCTION_NAME;
      } else if (isAzureFunctionsEnvironment && process.env.WEBSITE_SITE_NAME) {
        this.serviceName = process.env.WEBSITE_SITE_NAME;
      }
      if (this.serviceName) {
        try {
          validateServiceName(this.serviceName);
        } catch (err) {
          this.logger.warn(
            '"%s" is not a valid serviceName: %s',
            this.serviceName,
            err.message,
          );
          this.serviceName = null;
        }
      }
      if (!this.serviceName) {
        // Zero-conf support: use package.json#name, else
        // `unknown-${service.agent.name}-service`.
        try {
          this.serviceName = serviceNameFromPackageJson();
        } catch (err) {
          this.logger.warn(err.message);
        }
        if (!this.serviceName) {
          this.serviceName = 'unknown-nodejs-service';
        }
      }
    }
    if (this.serviceVersion) {
      // pass
    } else if (isLambda) {
      this.serviceVersion = process.env.AWS_LAMBDA_FUNCTION_VERSION;
    } else if (isAzureFunctionsEnvironment && process.env.WEBSITE_SITE_NAME) {
      // Leave this empty. There isn't a meaningful service version field
      // in Azure Functions envvars, and falling back to package.json ends up
      // finding the version of the "azure-functions-core-tools" package.
    } else {
      // Zero-conf support: use package.json#version, if possible.
      try {
        this.serviceVersion = serviceVersionFromPackageJson();
      } catch (err) {
        // pass
      }
    }

    normalize(this, this.logger);

    if (isLambda || isAzureFunctionsEnvironment) {
      // Override some config in AWS Lambda or Azure Functions environments.
      this.metricsInterval = 0;
      this.cloudProvider = 'none';
      this.centralConfig = false;
    }
    if (this.metricsInterval === 0) {
      this.breakdownMetrics = false;
    }

    this.loggingPreambleData = getPreambleData(this);
  }