5월 10일, Google I/O에서 Firebase가 돌아옵니다. 지금 등록하기

오류 보고

컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.

오류 자동 보고

아래와 같이 Cloud 함수에서 Error Reporting으로 오류를 내보낼 수 있습니다.

Node.js

// These WILL be reported to Error Reporting
throw new Error('I failed you'); // Will cause a cold start if not caught

Error Reporting 클라이언트 라이브러리를 사용하면 보다 세밀하게 오류를 보고할 수 있습니다.

GCP 콘솔의 Error Reporting에서 보고된 오류를 확인할 수 있습니다. 또한 GCP 콘솔의 함수 목록에서 특정 함수를 선택하면 해당 함수에서 보고된 오류를 확인할 수 있습니다.

함수로 생성되었지만 발견되지 않은 예외는 Error Reporting에 표시됩니다. 일부 유형의 포착되지 않은 예외(예: 비동기적으로 발생)는 향후 함수 호출 시 콜드 스타트를 유발할 수 있습니다. 이로 인해 함수를 실행하는 데 걸리는 시간이 늘어납니다.

오류 수동 보고

함수에서 Error Reporting으로 오류를 보고하려면 Cloud Logging API를 사용합니다.

종속 항목 가져오기

functions 디렉터리에서 Node.js용 Cloud Logging 클라이언트 라이브러리를 설치합니다.

npm install --save @google-cloud/logging

Cloud Logging 클라이언트 라이브러리를 가져오고 초기화합니다.

const Logging = require('@google-cloud/logging');

// Instantiates a client
const logging = Logging();

Cloud Logging으로 전송

올바르게 구성된 로그 항목에는 MonitoredResource 객체와 ErrorEvent 객체가 필요합니다.

reportError 함수 예시는 Error Reporting에 오류를 보고하는 데 필요한 최소 데이터를 보여줍니다.

function reportError(err, context = {}) {
  // This is the name of the log stream that will receive the log
  // entry. This name can be any valid log stream name, but must contain "err"
  // in order for the error to be picked up by Error Reporting.
  const logName = 'errors';
  const log = logging.log(logName);

  // https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource
  const metadata = {
    resource: {
      type: 'cloud_function',
      labels: { function_name: process.env.FUNCTION_NAME },
    },
  };

  // https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent
  const errorEvent = {
    message: err.stack,
    serviceContext: {
      service: process.env.FUNCTION_NAME,
      resourceType: 'cloud_function',
    },
    context: context,
  };

  // Write the error log entry
  return new Promise((resolve, reject) => {
    log.write(log.entry(metadata, errorEvent), (error) => {
      if (error) {
        return reject(error);
      }
      return resolve();
    });
  });
}

reportError 함수를 사용하여 수동으로 오류를 보고할 수 있습니다.

exports.createStripePayment = functions.firestore
  .document('stripe_customers/{userId}/payments/{pushId}')
  .onCreate(async (snap, context) => {
    const { amount, currency, payment_method } = snap.data();
    try {
      // Look up the Stripe customer id.
      const customer = (await snap.ref.parent.parent.get()).data().customer_id;
      // Create a charge using the pushId as the idempotency key
      // to protect against double charges.
      const idempotencyKey = context.params.pushId;
      const payment = await stripe.paymentIntents.create(
        {
          amount,
          currency,
          customer,
          payment_method,
          off_session: false,
          confirm: true,
          confirmation_method: 'manual',
        },
        { idempotencyKey }
      );
      // If the result is successful, write it back to the database.
      await snap.ref.set(payment);
    } catch (error) {
      // We want to capture errors and render them in a user-friendly way, while
      // still logging an exception to Error Reporting.
      functions.logger.log(error);
      await snap.ref.set({ error: userFacingMessage(error) }, { merge: true });
      await reportError(error, { user: context.params.userId });
    }
  });

ErrorContext 매개변수를 통해 사용자 세부정보를 전달할 수 있습니다. Error Reporting UI는 이러한 세부정보를 표시하고 이를 사용해 영향을 받는 사용자의 수를 계산합니다.

ErrorContext를 통해 HTTP 요청에 대한 정보도 전달할 수 있습니다.

export.httpError = functions.https.onRequest((request, response) => {
  const error = new Error('Test error');
  const httpRequest = {
    method: request.method,
    url: request.originalUrl,
    userAgent: request.get('user-agent'),
    referrer: '',
    remoteIp: request.ip
  };
  reportError(error, {httpRequest}).then(() => {
    response.end();
  });
});