autotel-devtools 5.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -5,9 +5,14 @@ var ws = require('ws');
5
5
  var fs = require('fs');
6
6
  var path = require('path');
7
7
  var url = require('url');
8
+ var protobuf = require('protobufjs');
8
9
  var core = require('@opentelemetry/core');
9
10
 
10
11
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
12
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
+
14
+ var protobuf__default = /*#__PURE__*/_interopDefault(protobuf);
15
+
11
16
  // src/index.ts
12
17
 
13
18
  // src/server/error-aggregator.ts
@@ -530,7 +535,7 @@ var SPAN_KIND_MAP = {
530
535
  function normalizeHexId(id) {
531
536
  if (!id) return "";
532
537
  const isBase64Like = /^[A-Za-z0-9+/=]+$/.test(id) && !/^[0-9a-f]+$/i.test(id);
533
- const isLikelyBase64Id = isBase64Like && (id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48);
538
+ const isLikelyBase64Id = isBase64Like && (id.length === 12 || id.length === 24 || id.length === 28 || id.length === 44 || id.length === 48);
534
539
  if (isLikelyBase64Id) {
535
540
  try {
536
541
  const bytes = Buffer.from(id, "base64");
@@ -662,13 +667,255 @@ async function readJsonBody(req) {
662
667
  req.on("error", reject);
663
668
  });
664
669
  }
670
+ async function readRawBody(req) {
671
+ return new Promise((resolve2, reject) => {
672
+ const chunks = [];
673
+ req.on("data", (chunk) => chunks.push(chunk));
674
+ req.on("end", () => resolve2(Buffer.concat(chunks)));
675
+ req.on("error", reject);
676
+ });
677
+ }
678
+ function isProtobufContentType(contentType) {
679
+ if (!contentType) return false;
680
+ const value = contentType.toLowerCase();
681
+ return value.includes("application/x-protobuf") || value.includes("application/protobuf");
682
+ }
665
683
  function sendJson(res, status, data) {
666
684
  const body = JSON.stringify(data);
667
685
  res.writeHead(status, { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(body) });
668
686
  res.end(body);
669
687
  }
688
+ var COMMON_PROTO = `
689
+ syntax = "proto3";
690
+ package opentelemetry.proto.common.v1;
691
+
692
+ message AnyValue {
693
+ oneof value {
694
+ string string_value = 1;
695
+ bool bool_value = 2;
696
+ int64 int_value = 3;
697
+ double double_value = 4;
698
+ ArrayValue array_value = 5;
699
+ KeyValueList kvlist_value = 6;
700
+ bytes bytes_value = 7;
701
+ }
702
+ }
703
+ message ArrayValue { repeated AnyValue values = 1; }
704
+ message KeyValueList { repeated KeyValue values = 1; }
705
+ message KeyValue {
706
+ string key = 1;
707
+ AnyValue value = 2;
708
+ }
709
+ message InstrumentationScope {
710
+ string name = 1;
711
+ string version = 2;
712
+ repeated KeyValue attributes = 3;
713
+ uint32 dropped_attributes_count = 4;
714
+ }
715
+ `;
716
+ var RESOURCE_PROTO = `
717
+ syntax = "proto3";
718
+ package opentelemetry.proto.resource.v1;
719
+
720
+ message Resource {
721
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 1;
722
+ uint32 dropped_attributes_count = 2;
723
+ }
724
+ `;
725
+ var TRACE_PROTO = `
726
+ syntax = "proto3";
727
+ package opentelemetry.proto.trace.v1;
728
+
729
+ message ResourceSpans {
730
+ opentelemetry.proto.resource.v1.Resource resource = 1;
731
+ repeated ScopeSpans scope_spans = 2;
732
+ string schema_url = 3;
733
+ }
734
+ message ScopeSpans {
735
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
736
+ repeated Span spans = 2;
737
+ string schema_url = 3;
738
+ }
739
+ message Span {
740
+ bytes trace_id = 1;
741
+ bytes span_id = 2;
742
+ string trace_state = 3;
743
+ bytes parent_span_id = 4;
744
+ fixed32 flags = 16;
745
+ string name = 5;
746
+ SpanKind kind = 6;
747
+ fixed64 start_time_unix_nano = 7;
748
+ fixed64 end_time_unix_nano = 8;
749
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 9;
750
+ uint32 dropped_attributes_count = 10;
751
+ repeated Event events = 11;
752
+ uint32 dropped_events_count = 12;
753
+ repeated Link links = 13;
754
+ uint32 dropped_links_count = 14;
755
+ Status status = 15;
756
+
757
+ enum SpanKind {
758
+ SPAN_KIND_UNSPECIFIED = 0;
759
+ SPAN_KIND_INTERNAL = 1;
760
+ SPAN_KIND_SERVER = 2;
761
+ SPAN_KIND_CLIENT = 3;
762
+ SPAN_KIND_PRODUCER = 4;
763
+ SPAN_KIND_CONSUMER = 5;
764
+ }
765
+ message Event {
766
+ fixed64 time_unix_nano = 1;
767
+ string name = 2;
768
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 3;
769
+ uint32 dropped_attributes_count = 4;
770
+ }
771
+ message Link {
772
+ bytes trace_id = 1;
773
+ bytes span_id = 2;
774
+ string trace_state = 3;
775
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 4;
776
+ uint32 dropped_attributes_count = 5;
777
+ fixed32 flags = 6;
778
+ }
779
+ }
780
+ message Status {
781
+ reserved 1;
782
+ string message = 2;
783
+ StatusCode code = 3;
784
+
785
+ enum StatusCode {
786
+ STATUS_CODE_UNSET = 0;
787
+ STATUS_CODE_OK = 1;
788
+ STATUS_CODE_ERROR = 2;
789
+ }
790
+ }
791
+ message ExportTraceServiceRequest {
792
+ repeated ResourceSpans resource_spans = 1;
793
+ }
794
+ `;
795
+ var LOGS_PROTO = `
796
+ syntax = "proto3";
797
+ package opentelemetry.proto.logs.v1;
798
+
799
+ enum SeverityNumber {
800
+ SEVERITY_NUMBER_UNSPECIFIED = 0;
801
+ SEVERITY_NUMBER_TRACE = 1;
802
+ SEVERITY_NUMBER_TRACE2 = 2;
803
+ SEVERITY_NUMBER_TRACE3 = 3;
804
+ SEVERITY_NUMBER_TRACE4 = 4;
805
+ SEVERITY_NUMBER_DEBUG = 5;
806
+ SEVERITY_NUMBER_DEBUG2 = 6;
807
+ SEVERITY_NUMBER_DEBUG3 = 7;
808
+ SEVERITY_NUMBER_DEBUG4 = 8;
809
+ SEVERITY_NUMBER_INFO = 9;
810
+ SEVERITY_NUMBER_INFO2 = 10;
811
+ SEVERITY_NUMBER_INFO3 = 11;
812
+ SEVERITY_NUMBER_INFO4 = 12;
813
+ SEVERITY_NUMBER_WARN = 13;
814
+ SEVERITY_NUMBER_WARN2 = 14;
815
+ SEVERITY_NUMBER_WARN3 = 15;
816
+ SEVERITY_NUMBER_WARN4 = 16;
817
+ SEVERITY_NUMBER_ERROR = 17;
818
+ SEVERITY_NUMBER_ERROR2 = 18;
819
+ SEVERITY_NUMBER_ERROR3 = 19;
820
+ SEVERITY_NUMBER_ERROR4 = 20;
821
+ SEVERITY_NUMBER_FATAL = 21;
822
+ SEVERITY_NUMBER_FATAL2 = 22;
823
+ SEVERITY_NUMBER_FATAL3 = 23;
824
+ SEVERITY_NUMBER_FATAL4 = 24;
825
+ }
826
+ message ResourceLogs {
827
+ opentelemetry.proto.resource.v1.Resource resource = 1;
828
+ repeated ScopeLogs scope_logs = 2;
829
+ string schema_url = 3;
830
+ }
831
+ message ScopeLogs {
832
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
833
+ repeated LogRecord log_records = 2;
834
+ string schema_url = 3;
835
+ }
836
+ message LogRecord {
837
+ reserved 4;
838
+ fixed64 time_unix_nano = 1;
839
+ fixed64 observed_time_unix_nano = 11;
840
+ SeverityNumber severity_number = 2;
841
+ string severity_text = 3;
842
+ opentelemetry.proto.common.v1.AnyValue body = 5;
843
+ repeated opentelemetry.proto.common.v1.KeyValue attributes = 6;
844
+ uint32 dropped_attributes_count = 7;
845
+ fixed32 flags = 8;
846
+ bytes trace_id = 9;
847
+ bytes span_id = 10;
848
+ }
849
+ message ExportLogsServiceRequest {
850
+ repeated ResourceLogs resource_logs = 1;
851
+ }
852
+ `;
853
+ var METRICS_PROTO = `
854
+ syntax = "proto3";
855
+ package opentelemetry.proto.metrics.v1;
856
+
857
+ message ResourceMetrics {
858
+ opentelemetry.proto.resource.v1.Resource resource = 1;
859
+ repeated ScopeMetrics scope_metrics = 2;
860
+ string schema_url = 3;
861
+ }
862
+ message ScopeMetrics {
863
+ opentelemetry.proto.common.v1.InstrumentationScope scope = 1;
864
+ repeated Metric metrics = 2;
865
+ string schema_url = 3;
866
+ }
867
+ message Metric {
868
+ string name = 1;
869
+ string description = 2;
870
+ string unit = 3;
871
+ }
872
+ message ExportMetricsServiceRequest {
873
+ repeated ResourceMetrics resource_metrics = 1;
874
+ }
875
+ `;
876
+ var TO_OBJECT_OPTIONS = {
877
+ longs: String,
878
+ bytes: String,
879
+ defaults: false
880
+ };
881
+ var cachedRoot = null;
882
+ function getRoot() {
883
+ if (cachedRoot) return cachedRoot;
884
+ const root = new protobuf__default.default.Root();
885
+ for (const source of [COMMON_PROTO, RESOURCE_PROTO, TRACE_PROTO, LOGS_PROTO, METRICS_PROTO]) {
886
+ protobuf__default.default.parse(source, root, { keepCase: false });
887
+ }
888
+ root.resolveAll();
889
+ cachedRoot = root;
890
+ return root;
891
+ }
892
+ function decodeRequest(typeName, body) {
893
+ const messageType = getRoot().lookupType(typeName);
894
+ const message = messageType.decode(body);
895
+ return messageType.toObject(message, TO_OBJECT_OPTIONS);
896
+ }
897
+ function decodeOtlpTraceRequest(body) {
898
+ return decodeRequest("opentelemetry.proto.trace.v1.ExportTraceServiceRequest", body);
899
+ }
900
+ function decodeOtlpLogsRequest(body) {
901
+ return decodeRequest("opentelemetry.proto.logs.v1.ExportLogsServiceRequest", body);
902
+ }
903
+ function decodeOtlpMetricsRequest(body) {
904
+ return decodeRequest("opentelemetry.proto.metrics.v1.ExportMetricsServiceRequest", body);
905
+ }
670
906
 
671
907
  // src/server/http.ts
908
+ var PROTOBUF_DECODERS = {
909
+ traces: decodeOtlpTraceRequest,
910
+ logs: decodeOtlpLogsRequest,
911
+ metrics: decodeOtlpMetricsRequest
912
+ };
913
+ async function readOtlpPayload(req, signal) {
914
+ if (isProtobufContentType(req.headers["content-type"])) {
915
+ return PROTOBUF_DECODERS[signal](await readRawBody(req));
916
+ }
917
+ return readJsonBody(req);
918
+ }
672
919
  function findPackageRoot() {
673
920
  let dir = path.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
674
921
  for (let i = 0; i < 5; i++) {
@@ -737,33 +984,33 @@ function attachDevtoolsRoutes(httpServer, devtools) {
737
984
  }
738
985
  if (req.method === "POST" && url === "/v1/traces") {
739
986
  try {
740
- const payload = await readJsonBody(req);
987
+ const payload = await readOtlpPayload(req, "traces");
741
988
  const traces = parseOtlpTraces(payload);
742
989
  devtools.addTraces(traces);
743
990
  sendJson(res, 200, { acceptedTraces: traces.length });
744
991
  } catch (e) {
745
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
992
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
746
993
  }
747
994
  return;
748
995
  }
749
996
  if (req.method === "POST" && url === "/v1/logs") {
750
997
  try {
751
- const payload = await readJsonBody(req);
998
+ const payload = await readOtlpPayload(req, "logs");
752
999
  const logs = parseOtlpLogs(payload);
753
1000
  devtools.addLogs(logs);
754
1001
  sendJson(res, 200, { acceptedLogs: logs.length });
755
1002
  } catch (e) {
756
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1003
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
757
1004
  }
758
1005
  return;
759
1006
  }
760
1007
  if (req.method === "POST" && url === "/v1/metrics") {
761
1008
  try {
762
- const payload = await readJsonBody(req);
1009
+ const payload = await readOtlpPayload(req, "metrics");
763
1010
  const count = countOtlpMetrics(payload);
764
1011
  sendJson(res, 200, { acceptedMetrics: count });
765
1012
  } catch (e) {
766
- sendJson(res, 400, { error: "Invalid OTLP JSON", message: e instanceof Error ? e.message : String(e) });
1013
+ sendJson(res, 400, { error: "Invalid OTLP payload", message: e instanceof Error ? e.message : String(e) });
767
1014
  }
768
1015
  return;
769
1016
  }