datasource-rest icon indicating copy to clipboard operation
datasource-rest copied to clipboard

Feature request - be able to tell if API was resolved by memory, cache, or source

Open dragonfriend0013 opened this issue 7 years ago • 3 comments

I would like to be able to tell what resolved an API call. This can currently be resolved by either the in-memory cache, and external cache, or the actual source. This is needed for a billing log I am needing to write and I accumulate the data as the request is being processed (in my datasource code). This could be a feature that is 'turned on' somehow, so that users that do not need the feature will not enable it.

dragonfriend0013 avatar Oct 16 '18 13:10 dragonfriend0013

I was thinking about this topic recently. This would likely be a combination of things correct? Since each field is resolved independently (potentially). To handle the calculation, which could be based off more than source, but query time, transfer bandwidth etc. did you have any ideas on the output format? To get a precise tracking total, I guess you could have a per field tracking object, similar to how the apollo engine / tracing api works.

Interested in thoughts on this topic in general.

sbrichardson avatar Oct 16 '18 22:10 sbrichardson

I was just thinking about apollo-datasource-rest for now. It returns the rest API from the three different sources. I just need to track that source. I know tracing can give some individual source timing, and that apollo-engine can do some pretty sweet stuff with individual result caching. But this feature is specifically about the restful.datasource itself.

On Tue, Oct 16, 2018, 6:51 PM Stephen Richardson [email protected] wrote:

I was thinking about this topic recently. This would likely be a combination of things correct? Since each field is resolved independently (potentially). To handle the calculation, which could be based off more than source, but query time, transfer bandwidth etc. did you have any ideas on the output format? To get a precise tracking total, I guess you could have a per field tracking object, similar to how the apollo engine / tracing api works.

Interested in thoughts on this topic in general.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/apollographql/apollo-server/issues/1825#issuecomment-430428645, or mute the thread https://github.com/notifications/unsubscribe-auth/Al_FOSPy6aXJaHfvv80sfIpH1RDQF8TDks5ulmL1gaJpZM4XeUk7 .

dragonfriend0013 avatar Oct 16 '18 23:10 dragonfriend0013

You can do that already with a simple trick overide the trace function and check if it has the age header in the response

  protected override async trace<TResult>(
    url: URL,
    request: RequestOptions,
    fn: () => Promise<any>,
  ): Promise<TResult> {
    if (process.env.NODE_ENV === "development") {
      const startTime = Date.now();
      try {
        const res = await fn();
        if (!res?.response.headers.get("age")) {
          const duration = Date.now() - startTime;
          const label = `${this.currentReq.method || "GET"} ${this.baseURL}${this.currentReq.params.toString()}`;
          logger.info(`${label} (${duration}ms)`);
        }
        return res;
      } catch (err) {
        logger.error(err);
      }
    }
    return fn();
  }
}

LadghemSaid avatar Jun 06 '23 07:06 LadghemSaid