From a46e7b6d94510c2eb66bb788f9d1b8b0a3cff120 Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 18 Jul 2022 10:52:07 +0800 Subject: [PATCH] feat(otlp-proto): pre-compile proto files --- .../otlp-proto-exporter-base/.gitignore | 2 + .../otlp-proto-exporter-base/package.json | 13 ++--- .../scripts/protos.js | 58 +++++++++++++++++++ .../src/generated/.gitkeep | 0 .../otlp-proto-exporter-base/src/util.ts | 42 +++----------- .../otlp-proto-exporter-base/tsconfig.json | 2 + package.json | 2 +- 7 files changed, 75 insertions(+), 44 deletions(-) create mode 100644 experimental/packages/otlp-proto-exporter-base/.gitignore create mode 100644 experimental/packages/otlp-proto-exporter-base/scripts/protos.js create mode 100644 experimental/packages/otlp-proto-exporter-base/src/generated/.gitkeep diff --git a/experimental/packages/otlp-proto-exporter-base/.gitignore b/experimental/packages/otlp-proto-exporter-base/.gitignore new file mode 100644 index 00000000000..c82683cbd73 --- /dev/null +++ b/experimental/packages/otlp-proto-exporter-base/.gitignore @@ -0,0 +1,2 @@ +src/generated/* +!src/generated/.gitkeep diff --git a/experimental/packages/otlp-proto-exporter-base/package.json b/experimental/packages/otlp-proto-exporter-base/package.json index 97cbb4dcdd0..6a2379ff3e8 100644 --- a/experimental/packages/otlp-proto-exporter-base/package.json +++ b/experimental/packages/otlp-proto-exporter-base/package.json @@ -7,15 +7,14 @@ "repository": "open-telemetry/opentelemetry-js", "scripts": { "prepublishOnly": "npm run compile", - "compile": "tsc --build", + "compile": "npm run protos && tsc --build", "clean": "tsc --build --clean", "lint": "eslint . --ext .ts", "lint:fix": "eslint . --ext .ts --fix", - "postcompile": "npm run submodule && npm run protos:copy", - "protos:copy": "cpx protos/opentelemetry/**/*.* build/protos/opentelemetry", + "protos": "npm run submodule && node scripts/protos.js", "submodule": "git submodule sync --recursive && git submodule update --init --recursive", "version": "node ../../../scripts/version-update.js", - "watch": "npm run protos:copy && tsc -w", + "watch": "npm run protos && tsc -w", "precompile": "lerna run version --scope $(npm pkg get name) --include-dependencies", "prewatch": "npm run precompile" }, @@ -37,7 +36,6 @@ "build/src/**/*.js", "build/src/**/*.js.map", "build/src/**/*.d.ts", - "build/protos/**/*.proto", "doc", "LICENSE", "README.md" @@ -52,9 +50,9 @@ "@types/node": "14.17.33", "@types/sinon": "10.0.6", "codecov": "3.8.3", - "cpx": "1.5.0", "mocha": "7.2.0", "nyc": "15.1.0", + "protobufjs": "^6.9.0", "rimraf": "3.0.2", "sinon": "12.0.1", "ts-loader": "8.3.0", @@ -67,7 +65,6 @@ "dependencies": { "@grpc/proto-loader": "^0.6.9", "@opentelemetry/core": "1.4.0", - "@opentelemetry/otlp-exporter-base": "0.30.0", - "protobufjs": "^6.9.0" + "@opentelemetry/otlp-exporter-base": "0.30.0" } } diff --git a/experimental/packages/otlp-proto-exporter-base/scripts/protos.js b/experimental/packages/otlp-proto-exporter-base/scripts/protos.js new file mode 100644 index 00000000000..81fae217fcc --- /dev/null +++ b/experimental/packages/otlp-proto-exporter-base/scripts/protos.js @@ -0,0 +1,58 @@ +'use strict'; + +const cp = require('child_process'); +const path = require('path'); + +const generatedPath = path.resolve(__dirname, '../src/generated'); +const protosPath = path.resolve(__dirname, '../protos'); +const protos = [ + 'opentelemetry/proto/common/v1/common.proto', + 'opentelemetry/proto/resource/v1/resource.proto', + 'opentelemetry/proto/trace/v1/trace.proto', + 'opentelemetry/proto/collector/trace/v1/trace_service.proto', + 'opentelemetry/proto/metrics/v1/metrics.proto', + 'opentelemetry/proto/collector/metrics/v1/metrics_service.proto', +].map(it => { + return path.join(protosPath, it); +}); + +function exec(command, argv) { + return new Promise((resolve, reject) => { + const child = cp.spawn(process.execPath, [command, ...argv], { + stdio: ['ignore', 'inherit', 'inherit'], + }); + child.on('exit', (code, signal) => { + if (code !== 0) { + reject(new Error(`${command} exited with non-zero code(${code}, ${signal})`)); + return; + } + resolve(); + }) + }); +} + +function pbts(pbjsOutFile) { + const pbtsPath = path.resolve(__dirname, '../node_modules/.bin/pbts'); + const pbtsOptions = [ + '-o', path.join(generatedPath, 'root.d.ts'), + ] + return exec(pbtsPath, [...pbtsOptions, pbjsOutFile]); +} + +async function pbjs(files) { + const pbjsPath = path.resolve(__dirname, '../node_modules/.bin/pbjs'); + const outFile = path.join(generatedPath, 'root.js'); + const pbjsOptions = [ + '-t', 'static-module', + '-w', 'commonjs', + '--null-defaults', + '-o', outFile, + ]; + await exec(pbjsPath, [...pbjsOptions, ...files]); + return outFile; +} + +;(async function main() { + const pbjsOut = await pbjs(protos); + await pbts(pbjsOut); +})(); diff --git a/experimental/packages/otlp-proto-exporter-base/src/generated/.gitkeep b/experimental/packages/otlp-proto-exporter-base/src/generated/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/experimental/packages/otlp-proto-exporter-base/src/util.ts b/experimental/packages/otlp-proto-exporter-base/src/util.ts index 0ab12f2e3e2..d5c99ba5574 100644 --- a/experimental/packages/otlp-proto-exporter-base/src/util.ts +++ b/experimental/packages/otlp-proto-exporter-base/src/util.ts @@ -14,50 +14,22 @@ * limitations under the License. */ -import * as path from 'path'; - import { ServiceClientType } from './types'; import { OTLPProtoExporterNodeBase } from './OTLPProtoExporterNodeBase'; -import type { Type } from 'protobufjs'; -import * as protobufjs from 'protobufjs'; import { CompressionAlgorithm, OTLPExporterError, - OTLPExporterNodeConfigBase, sendWithHttp } from '@opentelemetry/otlp-exporter-base'; +import * as root from './generated/root'; -let ExportRequestProto: Type | undefined; - -export function getExportRequestProto(): Type | undefined { - return ExportRequestProto; -} - -export function onInit( +export function getExportRequestProto( collector: OTLPProtoExporterNodeBase, - _config: OTLPExporterNodeConfigBase -): void { - const dir = path.resolve(__dirname, '..', 'protos'); - const root = new protobufjs.Root(); - root.resolvePath = function (origin, target) { - return `${dir}/${target}`; - }; +) { if (collector.getServiceClientType() === ServiceClientType.SPANS) { - const proto = root.loadSync([ - 'opentelemetry/proto/common/v1/common.proto', - 'opentelemetry/proto/resource/v1/resource.proto', - 'opentelemetry/proto/trace/v1/trace.proto', - 'opentelemetry/proto/collector/trace/v1/trace_service.proto', - ]); - ExportRequestProto = proto?.lookupType('ExportTraceServiceRequest'); + return root.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest; } else { - const proto = root.loadSync([ - 'opentelemetry/proto/common/v1/common.proto', - 'opentelemetry/proto/resource/v1/resource.proto', - 'opentelemetry/proto/metrics/v1/metrics.proto', - 'opentelemetry/proto/collector/metrics/v1/metrics_service.proto', - ]); - ExportRequestProto = proto?.lookupType('ExportMetricsServiceRequest'); + return root.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest; } } @@ -70,9 +42,9 @@ export function send( ): void { const serviceRequest = collector.convert(objects); - const message = getExportRequestProto()?.create(serviceRequest); + const message = getExportRequestProto(collector).create(serviceRequest); if (message) { - const body = getExportRequestProto()?.encode(message).finish(); + const body = getExportRequestProto(collector).encode(message).finish(); if (body) { sendWithHttp( collector, diff --git a/experimental/packages/otlp-proto-exporter-base/tsconfig.json b/experimental/packages/otlp-proto-exporter-base/tsconfig.json index 0282e8dc835..3e05eef24c0 100644 --- a/experimental/packages/otlp-proto-exporter-base/tsconfig.json +++ b/experimental/packages/otlp-proto-exporter-base/tsconfig.json @@ -1,11 +1,13 @@ { "extends": "../../../tsconfig.base.json", "compilerOptions": { + "allowJs": true, "rootDir": ".", "outDir": "build" }, "include": [ "src/**/*.ts", + "src/generated/*.js", "test/**/*.ts" ], "references": [ diff --git a/package.json b/package.json index 5aee41c22e8..4222ca52189 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "OpenTelemetry is a distributed tracing and stats collection framework.", "scripts": { "precompile": "lerna run version", - "compile": "tsc --build", + "compile": "lerna run protos && tsc --build", "prewatch": "npm run precompile", "watch": "tsc --build --watch", "clean": "tsc --build --clean",