constructor()

in lib/instrumentation/azure-functions.js [65:178]


  constructor(bindingDefinitions, executionContext, log) {
    // Example `bindingDefinitions`:
    //    [{"name":"req","type":"httpTrigger","direction":"in"},
    //    {"name":"res","type":"http","direction":"out"}]
    this.triggerType = TRIGGER_OTHER;
    this.httpOutputName = '';
    this.hasHttpTrigger = false;
    this.hasReturnBinding = false;
    this.outputBindingNames = [];
    for (const bd of bindingDefinitions) {
      if (bd.direction !== 'in') {
        if (bd.type && bd.type.toLowerCase() === 'http') {
          this.httpOutputName = bd.name;
        }
        this.outputBindingNames.push(bd.name);
        if (bd.name === '$return') {
          this.hasReturnBinding = true;
        }
      }
      if (bd.type) {
        const typeLc = bd.type.toLowerCase();
        switch (typeLc) {
          case 'httptrigger': // "type": "httpTrigger"
            this.triggerType = TRIGGER_HTTP;
            break;
          case 'timertrigger':
            this.triggerType = TRIGGER_TIMER;
            break;
        }
      }
    }

    // If this is an HTTP triggered-function, then get its route template and
    // route prefix.
    // https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger#customize-the-http-endpoint
    // A possible custom "route" is not included in the given context, so we
    // attempt to load the "function.json" file. A possible custom route prefix
    // is in "host.json".
    this.httpRoute = null;
    this.routePrefix = null;
    if (this.triggerType === TRIGGER_HTTP) {
      const funcDir = executionContext.functionDirectory;
      if (!funcDir) {
        this.httpRoute = executionContext.functionName;
      } else if (gHttpRouteFromFuncDir.has(funcDir)) {
        this.httpRoute = gHttpRouteFromFuncDir.get(funcDir);
      } else {
        try {
          const fj = JSON.parse(
            fs.readFileSync(path.join(funcDir, 'function.json')),
          );
          for (let i = 0; i < fj.bindings.length; i++) {
            const binding = fj.bindings[i];
            if (
              binding.direction === 'in' &&
              binding.type &&
              binding.type.toLowerCase() === 'httptrigger'
            ) {
              if (binding.route !== undefined) {
                this.httpRoute = binding.route;
              } else {
                this.httpRoute = executionContext.functionName;
              }
              gHttpRouteFromFuncDir.set(funcDir, this.httpRoute);
            }
          }
          log.trace(
            { funcDir, httpRoute: this.httpRoute },
            'azure-functions: loaded route',
          );
        } catch (httpRouteErr) {
          log.debug(
            'azure-functions: could not determine httpRoute for function %s: %s',
            executionContext.functionName,
            httpRouteErr.message,
          );
          this.httpRoute = executionContext.functionName;
        }
      }

      if (gRoutePrefix) {
        this.routePrefix = gRoutePrefix;
      } else if (!funcDir) {
        this.routePrefix = gRoutePrefix = DEFAULT_ROUTE_PREFIX;
      } else {
        try {
          const hj = JSON.parse(
            fs.readFileSync(path.join(path.dirname(funcDir), 'host.json')),
          );
          if (
            hj &&
            hj.extensions &&
            hj.extensions.http &&
            hj.extensions.http.routePrefix !== undefined
          ) {
            const rawRoutePrefix = hj.extensions.http.routePrefix;
            this.routePrefix = gRoutePrefix = normRoutePrefix(rawRoutePrefix);
            log.trace(
              { hj, routePrefix: this.routePrefix, rawRoutePrefix },
              'azure-functions: loaded route prefix',
            );
          } else {
            this.routePrefix = gRoutePrefix = DEFAULT_ROUTE_PREFIX;
          }
        } catch (routePrefixErr) {
          log.debug(
            'azure-functions: could not determine routePrefix: %s',
            routePrefixErr.message,
          );
          this.routePrefix = gRoutePrefix = DEFAULT_ROUTE_PREFIX;
        }
      }
    }
  }