Skip to content
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

chore(deps): bump protobufjs & tape patch versions. Fix load of .proto files #66

Merged
merged 4 commits into from
Feb 9, 2024
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
71 changes: 37 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 38 additions & 16 deletions packages/mockotlpserver/lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,51 @@
* Some utilities for working with the OpenTelemetry proto files.
*/

const path = require('path');
const {resolve} = require('path');

const protobuf = require('protobufjs');
const {Root} = require('protobufjs');

// TODO: for now `proto` files are copied from
// https://github.com/open-telemetry/opentelemetry-proto
// https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v1.0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we really using v1.0.0?
I thought it looked like opentelemetry-js.git was using a pre-1.0.0 tag version of the protos. I've no idea if it matters, however.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be compatible but I'll sync up in a follow up PR

// but maybe its better to have a submodule like otel-js does
const prefix = path.resolve(__dirname, '../opentelemetry/proto/');
const prefix = resolve(__dirname, '..');
const paths = [
'/common/v1/common.proto',
'/resource/v1/resource.proto',
'/logs/v1/logs.proto',
'/metrics/v1/metrics.proto',
'/trace/v1/trace.proto',
'/collector/logs/v1/logs_service.proto',
'/collector/metrics/v1/metrics_service.proto',
'/collector/trace/v1/trace_service.proto',
'/opentelemetry/proto/common/v1/common.proto',
'/opentelemetry/proto/resource/v1/resource.proto',
'/opentelemetry/proto/logs/v1/logs.proto',
'/opentelemetry/proto/metrics/v1/metrics.proto',
'/opentelemetry/proto/trace/v1/trace.proto',
'/opentelemetry/proto/collector/logs/v1/logs_service.proto',
'/opentelemetry/proto/collector/metrics/v1/metrics_service.proto',
'/opentelemetry/proto/collector/trace/v1/trace_service.proto',
];
let root;
for (const p of paths) {
root = protobuf.loadSync(`${prefix}${p}`, root);
}

// Craete a new Root so we can patch it
const root = new Root();

// This function is patched because the Root class does not have any
// referece of which is the root path of the proto files. Instead it
// takes the folder of current file being processed as the root path
// resulting in duplicated subpaths
// Example: resource.proto file importing common.proto results in
// /Users/.../mockotlpserver/opentelemetry/proto/resource/v1/opentelemetry/proto/common/v1/common.proto
root.resolvePath = function patchResolvePath(filename) {
let path = Root.prototype.resolvePath.apply(root, arguments);
if (filename) {
const folder = resolve(filename, '..');
path = prefix + path.replace(folder, '');
}
return path;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Was the intent to move away from this hack eventually once we have a process to build/commit a proto.json and load that instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! I think as part of the process of updating the proto files we could change the imports to use relative paths until protobufjs/protobuf.js#1971 is fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it possibly work to add a mostly-empty root.proto file at the top-level dir, load that first, then use its returned root for loading all the other .proto files?


// Load the files at once
root.loadSync(paths.map((p) => `${prefix}${p}`));

/**
* Return `any` for now itherwise we get type errors when using
* `root.lookupType(...)` in `normalize.js
* @returns {any}
*/
function getProtoRoot() {
return root;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mockotlpserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@grpc/proto-loader": "^0.7.10",
"dashdash": "^2.0.0",
"long": "^5.2.3",
"protobufjs": "^7.2.5",
"protobufjs": "^7.2.6",
"safe-stable-stringify": "^2.4.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"express": "^4.18.2",
"module-details-from-path": "^1.0.3",
"semver": "^7.6.0",
"tape": "^5.7.2"
"tape": "^5.7.4"
}
}