Skip to content

Inline usage reporting protobuf into main package #6515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Trace } from '@apollo/usage-reporting-protobuf';
import { Trace } from '../../../../usageReportingProtobuf';
import { dateToProtoTimestamp } from '../../../plugin/traceTreeBuilder';
import {
OurContextualizedStats,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 `[email protected]`
> 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as protobuf from './generated/protobuf';
export = protobuf;
Original file line number Diff line number Diff line change
@@ -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.