Skip to content

Commit

Permalink
Manual protobuf encoding for OTLP (#81)
Browse files Browse the repository at this point in the history
* Manual protobuf encoding for OTLP

* fix lints

* changeset
  • Loading branch information
dvoytenko authored Apr 25, 2024
1 parent aab6db5 commit 6cf1e79
Show file tree
Hide file tree
Showing 9 changed files with 745 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-starfishes-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/otel": patch
---

Manual protobuf encoding for OTLP to save about 180K binary size
36 changes: 36 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Unit Tests

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
test:
name: "unit tests"
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup pnpm
uses: pnpm/action-setup@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"

- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm build

- name: Run unit tests
run: pnpm unit-test
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"eslint-fix": "turbo eslint -- --fix",
"release": "pnpm build && changeset publish",
"type-check": "turbo --continue type-check",
"unit-test": "turbo unit-test",
"version-packages": "changeset version && pnpm i --no-frozen-lockfile && git add ."
},
"keywords": [],
Expand Down
25 changes: 25 additions & 0 deletions packages/otel/build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { stat } from "node:fs/promises";
import type { Plugin } from "esbuild";
import { build } from "esbuild";

const MINIFY = true;
const SOURCEMAP = false;

const MAX_SIZES = {
"dist/node/index.js": 210_000,
"dist/edge/index.js": 180_000,
};

type ExternalPluginFactory = (external: string[]) => Plugin;
const externalCjsToEsmPlugin: ExternalPluginFactory = (external) => ({
name: "external",
Expand Down Expand Up @@ -93,6 +99,25 @@ async function buildAll(): Promise<void> {
],
}),
]);

// Check max size.
const errors: string[] = [];
for (const [file, maxSize] of Object.entries(MAX_SIZES)) {
// eslint-disable-next-line no-await-in-loop
const s = await stat(file);
if (s.size > maxSize) {
errors.push(
`${file}: the size of ${s.size} is over the maximum allowed size of ${maxSize}`
);
}
}
if (errors.length > 0) {
for (const error of errors) {
// eslint-disable-next-line no-console
console.error(error);
}
process.exit(1);
}
}

void buildAll();
6 changes: 4 additions & 2 deletions packages/otel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
},
"scripts": {
"build": "pnpm clean && pnpm tsx build.ts && pnpm build-types && pnpm build-site",
"build-only": "pnpm tsx build.ts",
"build-types": "tsc --noEmit false --declaration --emitDeclarationOnly --stripInternal --declarationDir dist/types src/index.ts",
"build-site": "pnpm build-typedoc && cp index.html LICENSE CHANGELOG.md dist-site",
"build-typedoc": "pnpm typedoc src/index.ts",
Expand All @@ -71,15 +72,16 @@
"@opentelemetry/instrumentation": "^0.46.0",
"@opentelemetry/otlp-exporter-base": "^0.46.0",
"@opentelemetry/otlp-proto-exporter-base": "^0.46.0",
"@opentelemetry/otlp-transformer": "^0.46.0",
"@opentelemetry/resources": "^1.19.0",
"@opentelemetry/sdk-logs": "^0.46.0",
"@opentelemetry/sdk-metrics": "^1.19.0",
"@opentelemetry/semantic-conventions": "^1.19.0",
"@opentelemetry/otlp-transformer": "^0.46.0",
"@opentelemetry/sdk-trace-base": "^1.19.0",
"@opentelemetry/semantic-conventions": "^1.19.0",
"@types/node": "18.15.11",
"esbuild": "^0.19.4",
"eslint-config": "workspace:*",
"protobufjs": "^7.2.6",
"rimraf": "3.0.2",
"tsx": "^4.6.2",
"typedoc": "^0.25.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { ReadableSpan, SpanExporter } from "@opentelemetry/sdk-trace-base";
import type { IExportTraceServiceRequest } from "@opentelemetry/otlp-transformer";
import { createExportTraceServiceRequest } from "@opentelemetry/otlp-transformer/build/src/trace";
import { ServiceClientType } from "@opentelemetry/otlp-proto-exporter-base/build/src/platform/index";
import { getExportRequestProto } from "@opentelemetry/otlp-proto-exporter-base/build/src/platform/util";
import type { ExportResult } from "@opentelemetry/core";
import { OTLPExporterEdgeBase } from "./otlp-exporter-base";
import { getDefaultUrl } from "./trace-config";
import type { OTLPExporterConfig } from "./config";
import { encodeTraceServiceRequest } from "./proto";

/**
* OTLP exporter for the `http/protobuf` protocol. Compatible with the "edge" runtime.
Expand Down Expand Up @@ -52,10 +51,7 @@ class Impl extends OTLPExporterEdgeBase<
contentType: string;
headers?: Record<string, string> | undefined;
} {
const serviceClientType = ServiceClientType.SPANS;
const exportRequestType = getExportRequestProto(serviceClientType);
const message = exportRequestType.create(serviceRequest);
const body = exportRequestType.encode(message).finish();
const body = encodeTraceServiceRequest(serviceRequest);
return {
body,
contentType: "application/x-protobuf",
Expand Down
Loading

0 comments on commit 6cf1e79

Please sign in to comment.