Skip to content

Commit

Permalink
chore(deps): bump protobufjs & tape patch versions. Fix load of `…
Browse files Browse the repository at this point in the history
….proto` files (#66)

Contains a temporary workaround for protobufjs/protobuf.js#1971. Loading a file with non relative imports fail.
  • Loading branch information
dependabot[bot] authored Feb 9, 2024
1 parent 22d32ae commit cd9d4f0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 52 deletions.
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
// 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;
};

// 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"
}
}

0 comments on commit cd9d4f0

Please sign in to comment.