Skip to content

Commit

Permalink
Inline usage reporting protobuf into main package (#6515)
Browse files Browse the repository at this point in the history
Keeping it separate mostly just means we have to worry about version
skew. It doesn't let us avoid the protobufjs dependency (it was just
transitive). Other packages that may need these types (like
`@apollo/gateway`) will have a peer dependency on `@apollo/server`
anyway.

Note that this package consists of .js and .d.ts files that are not
generated from .ts files, so they exist outside of `src`. We ensure they
end up in the published package via a change to `.npmignore`.

As before, the only code that can import the protobuf code should be
tests, `import type`s, or code under `src/plugin` that is only evaluated
if the plugin is actually used.

Also fixes missing dependencies on cors and body-parser needed for
the standalone server. (As part of #6243 we may make sure that these are
only needed if you actually import the standalone server directory.)

Fixes #6510.
  • Loading branch information
glasser committed Jun 3, 2022
1 parent 0309ed6 commit 0b58585
Show file tree
Hide file tree
Showing 31 changed files with 1,443 additions and 4,774 deletions.
1 change: 0 additions & 1 deletion .codesandbox/ci.json
Expand Up @@ -2,7 +2,6 @@
"buildCommand": "compile",
"installCommand": "install-with-npm-8.5",
"packages": [
"packages/usage-reporting-protobuf",
"packages/server"
],
"sandboxes": ["apollo-server-typescript-3opde","apollo-server"],
Expand Down
6,031 changes: 1,377 additions & 4,654 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Expand Up @@ -21,7 +21,11 @@
"codegen": "graphql-codegen",
"prettier-check": "prettier --check .",
"prettier-fix": "prettier --write .",
"spell-check": "cspell lint '**' --no-progress || (echo 'Add any real words to cspell-dict.txt.'; exit 1)"
"spell-check": "cspell lint '**' --no-progress || (echo 'Add any real words to cspell-dict.txt.'; exit 1)",
"protobuf-generate": "rm -rf packages/server/usageReportingProtobuf/generated && mkdir packages/server/usageReportingProtobuf/generated && npm run protobuf-pbjs && npm run protobuf-pbts",
"protobuf-pbjs": "apollo-pbjs --target static-module --out packages/server/usageReportingProtobuf/generated/protobuf.js --wrap commonjs --force-number --no-from-object packages/server/usageReportingProtobuf/reports.proto",
"protobuf-pbts": "apollo-pbts -o packages/server/usageReportingProtobuf/generated/protobuf.d.ts packages/server/usageReportingProtobuf/generated/protobuf.js",
"protobuf-update": "curl -sSfo packages/server/usageReportingProtobuf/reports.proto https://usage-reporting.api.apollographql.com/proto/reports.proto"
},
"workspaces": [
"packages/*"
Expand Down
1 change: 1 addition & 0 deletions packages/server/.npmignore
Expand Up @@ -5,3 +5,4 @@ src/**/__tests__/**
dist/**/__tests__/**
!package.json
!README.md
!usageReportingProtobuf/**/*
4 changes: 3 additions & 1 deletion packages/server/package.json
Expand Up @@ -25,7 +25,7 @@
"node": ">=12.0"
},
"dependencies": {
"@apollo/usage-reporting-protobuf": "^3.3.0",
"@apollo/protobufjs": "1.2.2",
"@apollo/utils.createhash": "^1.1.0",
"@apollo/utils.isnodelike": "^1.1.0",
"@apollo/utils.logger": "^1.0.0",
Expand All @@ -40,6 +40,8 @@
"@types/lru-cache": "^7.6.1",
"@types/node-fetch": "^2.6.1",
"async-retry": "^1.2.1",
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"graphql-tag": "^2.11.0",
"keyv": "^4.2.2",
Expand Down
Expand Up @@ -5,7 +5,7 @@ import express from 'express';
import bodyParser from 'body-parser';
import loglevel from 'loglevel';

import { Report, Trace } from '@apollo/usage-reporting-protobuf';
import { Report, Trace } from '../../../usageReportingProtobuf';

import {
GraphQLSchema,
Expand Down
Expand Up @@ -11,7 +11,7 @@ import {
ITrace,
ITracesAndStats,
ContextualizedStats,
} from '@apollo/usage-reporting-protobuf';
} from '../../../../usageReportingProtobuf';
import { pluginsEnabledForSchemaResolvers } from '../../../utils/schemaInstrumentation';
import nock from 'nock';
import sumBy from 'lodash.sumby';
Expand Down
@@ -1,4 +1,4 @@
import { Trace } from '@apollo/usage-reporting-protobuf';
import { Trace } from '../../../../usageReportingProtobuf';
import { dateToProtoTimestamp } from '../../../plugin/traceTreeBuilder';
import {
OurContextualizedStats,
Expand Down
@@ -1,5 +1,5 @@
import { makeTraceDetails } from '../../../plugin/usageReporting/traceDetails';
import { Trace } from '@apollo/usage-reporting-protobuf';
import { Trace } from '../../../../usageReportingProtobuf';
import { GraphQLError } from 'graphql';

const variables: Record<string, any> = {
Expand Down
3 changes: 1 addition & 2 deletions packages/server/src/externalTypes/graphql.ts
@@ -1,5 +1,4 @@
// TODO(AS4): This seems like it could live in this package too.
import type { Trace } from '@apollo/usage-reporting-protobuf';
import type { Trace } from '../../usageReportingProtobuf';
import type { Logger } from '@apollo/utils.logger';
import type {
DocumentNode,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/plugin/inlineTrace/index.ts
@@ -1,4 +1,4 @@
import { Trace } from '@apollo/usage-reporting-protobuf';
import { Trace } from '../../../usageReportingProtobuf';
import { TraceTreeBuilder } from '../traceTreeBuilder';
import type { ApolloServerPluginUsageReportingOptions } from '../usageReporting/options';
import type { InternalApolloServerPlugin } from '../../internalPlugin';
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/plugin/traceTreeBuilder.ts
@@ -1,7 +1,7 @@
// This class is a helper for ApolloServerPluginUsageReporting and
// ApolloServerPluginInlineTrace.
import { GraphQLError, GraphQLResolveInfo, ResponsePath } from 'graphql';
import { Trace, google } from '@apollo/usage-reporting-protobuf';
import { Trace, google } from '../../usageReportingProtobuf';
import type { Logger } from '@apollo/utils.logger';

function internalError(message: string) {
Expand Down
@@ -1,5 +1,5 @@
import LRUCache from 'lru-cache';
import type { Trace } from '@apollo/usage-reporting-protobuf';
import type { Trace } from '../../../usageReportingProtobuf';
import { iterateOverTrace } from './iterateOverTrace';
import { DurationHistogram } from './durationHistogram';

Expand Down
@@ -1,4 +1,4 @@
import type { Trace } from '@apollo/usage-reporting-protobuf';
import type { Trace } from '../../../usageReportingProtobuf';

/**
* Iterates over the entire trace, calling `f` on each Trace.Node found. It
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/plugin/usageReporting/options.ts
Expand Up @@ -6,7 +6,7 @@ import type {
BaseContext,
} from '../../externalTypes';
import type { Logger } from '@apollo/utils.logger';
import type { Trace } from '@apollo/usage-reporting-protobuf';
import type { Trace } from '../../../usageReportingProtobuf';
import type { Fetcher } from '@apollo/utils.fetcher';

export interface ApolloServerPluginUsageReportingOptions<
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/plugin/usageReporting/plugin.ts
@@ -1,4 +1,4 @@
import { Report, ReportHeader, Trace } from '@apollo/usage-reporting-protobuf';
import { Report, ReportHeader, Trace } from '../../../usageReportingProtobuf';
import type { Fetcher, FetcherResponse } from '@apollo/utils.fetcher';
import {
usageReportingSignature,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/plugin/usageReporting/stats.ts
Expand Up @@ -10,7 +10,7 @@ import {
ITypeStat,
ReportHeader,
Trace,
} from '@apollo/usage-reporting-protobuf';
} from '../../../usageReportingProtobuf';
import type { ReferencedFieldsByType } from '@apollo/utils.usagereporting';
import { DurationHistogram } from './durationHistogram';
import { iterateOverTrace, ResponseNamePath } from './iterateOverTrace';
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/plugin/usageReporting/traceDetails.ts
@@ -1,4 +1,4 @@
import { Trace } from '@apollo/usage-reporting-protobuf';
import { Trace } from '../../../usageReportingProtobuf';
import type { VariableValueOptions } from './options';

// Creates trace details from request variables, given a specification for modifying
Expand Down
41 changes: 41 additions & 0 deletions packages/server/usageReportingProtobuf/README.md
@@ -0,0 +1,41 @@
# Usage reporting protobuf

> **Note:** The Apollo usage reporting API is subject to change. We strongly
> encourage developers to contact Apollo support at `support@apollographql.com`
> to discuss their use case prior to building their own reporting agent using
> this module.
This subdirectory provides JavaScript/TypeScript [Protocol
buffer](https://developers.google.com/protocol-buffers/) definitions for the
Apollo usage reporting API. These definitions are generated for consumption
from the `reports.proto` file which is defined internally within Apollo.

## Development

> **Note:** Due to a dependency on Unix tools (e.g. `bash`, `grep`, etc.), the
> development of this module requires a Unix system. There is no reason why
> this can't be avoided, the time just hasn't been taken to make those changes.
> We'd happily accept a PR which makes the appropriate changes!
This directory contains .js and .d.ts files. The index files are written
manually and the files in `generated` are automatically generated with
`@apollo/protobufjs` (a fork of
[`protobufjs`](https://www.npmjs.com/package/protobufjs) that we maintain
specifically for this package) based on the `reports.proto` file. None of these
files are generated from .ts files by tsc, which is why this directory is not
under `src`.

To update `reports.proto` to the current version recognized by the Studio usage
reporting ingress, run `npm run protobuf-update`. To then regenerate the JS and
TS files, run `npm run protobuf-generate`. We check in the generated code and only
regenerate it manually, partially to make builds faster (no need to run pbjs on
every `npm install`) and partially so that we don't have to make sure that
`pbjs` runs on every Node version that we support.

The files in this subdirectory are large, so we want to avoid loading them at
runtime unless necessary. Thus, files in this directory should only be imported
in three contexts:
- Inside `src/plugin`, in files that will only be loaded indirectly via the
runtime require calls in `src/plugin/index.ts`
- In tests
- At compile-time only via `import type` calls
2 changes: 2 additions & 0 deletions packages/server/usageReportingProtobuf/index.d.ts
@@ -0,0 +1,2 @@
import * as protobuf from './generated/protobuf';
export = protobuf;
@@ -1,4 +1,4 @@
const protobuf = require('./protobuf');
const protobuf = require('./generated/protobuf');
const protobufJS = require('@apollo/protobufjs/minimal');

// Remove Long support. Our uint64s tend to be small (less
Expand Down
11 changes: 0 additions & 11 deletions packages/usage-reporting-protobuf/.npmignore

This file was deleted.

39 changes: 0 additions & 39 deletions packages/usage-reporting-protobuf/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions packages/usage-reporting-protobuf/generated/index.d.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/usage-reporting-protobuf/generated/index.js

This file was deleted.

36 changes: 0 additions & 36 deletions packages/usage-reporting-protobuf/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions packages/usage-reporting-protobuf/src/index.d.ts

This file was deleted.

0 comments on commit 0b58585

Please sign in to comment.