@faststore/api 2.0.163-alpha.0 → 2.0.173-alpha.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/api.cjs.development.js +254 -1
- package/dist/api.cjs.development.js.map +1 -1
- package/dist/api.cjs.production.min.js +1 -1
- package/dist/api.cjs.production.min.js.map +1 -1
- package/dist/api.esm.js +255 -3
- package/dist/api.esm.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/telemetry/index.d.ts +8 -0
- package/dist/telemetry/useFaststoreTelemetry.d.ts +24 -0
- package/package.json +11 -4
- package/src/index.ts +1 -0
- package/src/telemetry/index.ts +117 -0
- package/src/telemetry/useFaststoreTelemetry.ts +255 -0
|
@@ -11,9 +11,18 @@ var crypto = _interopDefault(require('crypto'));
|
|
|
11
11
|
var graphql = require('graphql');
|
|
12
12
|
var language = require('graphql/language');
|
|
13
13
|
var utils = require('@graphql-tools/utils');
|
|
14
|
+
var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
|
|
15
|
+
var resources = require('@opentelemetry/resources');
|
|
16
|
+
var exporterTraceOtlpGrpc = require('@opentelemetry/exporter-trace-otlp-grpc');
|
|
17
|
+
var sdkLogs = require('@opentelemetry/sdk-logs');
|
|
18
|
+
var exporterLogsOtlpGrpc = require('@opentelemetry/exporter-logs-otlp-grpc');
|
|
19
|
+
var core = require('@envelop/core');
|
|
20
|
+
var onResolve = require('@envelop/on-resolve');
|
|
21
|
+
var api = require('@opentelemetry/api');
|
|
22
|
+
var apiLogs = require('@opentelemetry/api-logs');
|
|
14
23
|
|
|
15
24
|
var name = "@faststore/api";
|
|
16
|
-
var version = "2.0.
|
|
25
|
+
var version = "2.0.165-alpha.0";
|
|
17
26
|
var license = "MIT";
|
|
18
27
|
var main = "dist/index.js";
|
|
19
28
|
var typings = "dist/index.d.ts";
|
|
@@ -35,7 +44,12 @@ var scripts = {
|
|
|
35
44
|
generate: "graphql-codegen --config codegen.yml"
|
|
36
45
|
};
|
|
37
46
|
var dependencies = {
|
|
47
|
+
"@envelop/on-resolve": "^2.0.6",
|
|
38
48
|
"@graphql-tools/schema": "^8.2.0",
|
|
49
|
+
"@opentelemetry/exporter-logs-otlp-grpc": "^0.39.1",
|
|
50
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.39.1",
|
|
51
|
+
"@opentelemetry/sdk-logs": "^0.39.1",
|
|
52
|
+
"@opentelemetry/sdk-trace-base": "^1.13.0",
|
|
39
53
|
"@rollup/plugin-graphql": "^1.0.0",
|
|
40
54
|
dataloader: "^2.1.0",
|
|
41
55
|
"fast-deep-equal": "^3.1.3",
|
|
@@ -43,6 +57,7 @@ var dependencies = {
|
|
|
43
57
|
"p-limit": "^3.1.0"
|
|
44
58
|
};
|
|
45
59
|
var devDependencies = {
|
|
60
|
+
"@envelop/core": "^2.6.0",
|
|
46
61
|
"@faststore/eslint-config": "^2.0.104-alpha.0",
|
|
47
62
|
"@faststore/shared": "^2.0.104-alpha.0",
|
|
48
63
|
"@graphql-codegen/cli": "2.2.0",
|
|
@@ -60,6 +75,7 @@ var devDependencies = {
|
|
|
60
75
|
typescript: "^4.4.2"
|
|
61
76
|
};
|
|
62
77
|
var peerDependencies = {
|
|
78
|
+
"@envelop/core": "^1 || ^2",
|
|
63
79
|
graphql: "^15.6.0"
|
|
64
80
|
};
|
|
65
81
|
var packageJson = {
|
|
@@ -2532,6 +2548,242 @@ const directive = {
|
|
|
2532
2548
|
})
|
|
2533
2549
|
};
|
|
2534
2550
|
|
|
2551
|
+
var AttributeName;
|
|
2552
|
+
(function (AttributeName) {
|
|
2553
|
+
AttributeName["EXECUTION_ERROR"] = "graphql.error";
|
|
2554
|
+
AttributeName["EXECUTION_RESULT"] = "graphql.result";
|
|
2555
|
+
AttributeName["RESOLVER_EXECUTION_ERROR"] = "graphql.resolver.error";
|
|
2556
|
+
AttributeName["RESOLVER_EXCEPTION"] = "graphql.resolver.exception";
|
|
2557
|
+
AttributeName["RESOLVER_FIELD_NAME"] = "graphql.resolver.fieldName";
|
|
2558
|
+
AttributeName["RESOLVER_TYPE_NAME"] = "graphql.resolver.typeName";
|
|
2559
|
+
AttributeName["RESOLVER_RESULT_TYPE"] = "graphql.resolver.resultType";
|
|
2560
|
+
AttributeName["RESOLVER_ARGS"] = "graphql.resolver.args";
|
|
2561
|
+
AttributeName["EXECUTION_OPERATION_NAME"] = "graphql.operation.name";
|
|
2562
|
+
AttributeName["EXECUTION_OPERATION_TYPE"] = "graphql.operation.type";
|
|
2563
|
+
AttributeName["EXECUTION_OPERATION_DOCUMENT"] = "graphql.document";
|
|
2564
|
+
AttributeName["EXECUTION_VARIABLES"] = "graphql.variables";
|
|
2565
|
+
})(AttributeName || (AttributeName = {}));
|
|
2566
|
+
const tracingSpanSymbol = /*#__PURE__*/Symbol('OPEN_TELEMETRY_GRAPHQL');
|
|
2567
|
+
function getResolverSpanKey(path) {
|
|
2568
|
+
const nodes = [];
|
|
2569
|
+
// If the first node (after reversed, it will be the last one) is an integer, that is, identifies a list,
|
|
2570
|
+
// we don't want to include it in the key. Note that this will only happen when analysing .prev paths in
|
|
2571
|
+
// a list of elements. We just want to remove the initial node that is a integer, not all of them.
|
|
2572
|
+
//
|
|
2573
|
+
// Nodes with keys 6bc73341b2a183fc::product::image::0::url would not be able to find
|
|
2574
|
+
// parents with key 6bc73341b2a183fc::product::image because of the "0" list index -
|
|
2575
|
+
// it would search for 6bc73341b2a183fc::product::image::0
|
|
2576
|
+
let currentPath = nodes.length === 0 && Number.isInteger(path.key) ? path.prev : path;
|
|
2577
|
+
while (currentPath) {
|
|
2578
|
+
nodes.push(currentPath.key);
|
|
2579
|
+
currentPath = currentPath.prev;
|
|
2580
|
+
}
|
|
2581
|
+
return [...nodes].reverse().join('.');
|
|
2582
|
+
}
|
|
2583
|
+
const getFaststoreTelemetryPlugin = (tracingProvider, loggerProvider, serviceName, experimentalSendLogs) => {
|
|
2584
|
+
return function useFaststoreTelemetry() {
|
|
2585
|
+
const tracer = tracingProvider.getTracer(serviceName);
|
|
2586
|
+
const logger = loggerProvider.getLogger(serviceName);
|
|
2587
|
+
const resolverContextsByRootSpans = {};
|
|
2588
|
+
return {
|
|
2589
|
+
onPluginInit({
|
|
2590
|
+
addPlugin
|
|
2591
|
+
}) {
|
|
2592
|
+
addPlugin(
|
|
2593
|
+
// eslint-disable-next-line
|
|
2594
|
+
onResolve.useOnResolve(({
|
|
2595
|
+
info,
|
|
2596
|
+
context
|
|
2597
|
+
}) => {
|
|
2598
|
+
if (context && typeof context === 'object' && context[tracingSpanSymbol]) {
|
|
2599
|
+
var _path$prev, _path$prev2;
|
|
2600
|
+
tracer.getActiveSpanProcessor();
|
|
2601
|
+
const rootContextSpanId = context[tracingSpanSymbol].spanContext().spanId;
|
|
2602
|
+
const {
|
|
2603
|
+
fieldName,
|
|
2604
|
+
returnType,
|
|
2605
|
+
parentType,
|
|
2606
|
+
path
|
|
2607
|
+
} = info;
|
|
2608
|
+
const previousResolverSpanKey = path.prev && getResolverSpanKey(path.prev);
|
|
2609
|
+
let ctx = null;
|
|
2610
|
+
if (previousResolverSpanKey && resolverContextsByRootSpans[rootContextSpanId][previousResolverSpanKey]) {
|
|
2611
|
+
ctx = resolverContextsByRootSpans[rootContextSpanId][previousResolverSpanKey];
|
|
2612
|
+
} else {
|
|
2613
|
+
var _resolverContextsByRo;
|
|
2614
|
+
ctx = api.trace.setSpan(api.context.active(), context[tracingSpanSymbol]);
|
|
2615
|
+
resolverContextsByRootSpans[rootContextSpanId] = (_resolverContextsByRo = resolverContextsByRootSpans[rootContextSpanId]) != null ? _resolverContextsByRo : {};
|
|
2616
|
+
}
|
|
2617
|
+
const resolverIndexInList = Number.isInteger((_path$prev = path.prev) == null ? void 0 : _path$prev.key) ? `[${(_path$prev2 = path.prev) == null ? void 0 : _path$prev2.key}]` : '';
|
|
2618
|
+
const resolverSpan = tracer.startSpan(`${parentType.toString()}.${fieldName}${resolverIndexInList}`, {
|
|
2619
|
+
attributes: {
|
|
2620
|
+
[AttributeName.RESOLVER_FIELD_NAME]: fieldName,
|
|
2621
|
+
[AttributeName.RESOLVER_TYPE_NAME]: parentType.toString(),
|
|
2622
|
+
[AttributeName.RESOLVER_RESULT_TYPE]: returnType.toString(),
|
|
2623
|
+
'meta.span.path': getResolverSpanKey(path)
|
|
2624
|
+
}
|
|
2625
|
+
}, ctx);
|
|
2626
|
+
const resolverCtx = api.trace.setSpan(ctx, resolverSpan);
|
|
2627
|
+
resolverContextsByRootSpans[rootContextSpanId][getResolverSpanKey(path)] = resolverCtx;
|
|
2628
|
+
return ({
|
|
2629
|
+
result
|
|
2630
|
+
}) => {
|
|
2631
|
+
if (result instanceof Error) {
|
|
2632
|
+
resolverSpan.setAttributes({
|
|
2633
|
+
error: true,
|
|
2634
|
+
'exception.category': AttributeName.RESOLVER_EXECUTION_ERROR,
|
|
2635
|
+
'exception.message': result.message,
|
|
2636
|
+
'exception.type': result.name
|
|
2637
|
+
});
|
|
2638
|
+
resolverSpan.recordException(result);
|
|
2639
|
+
}
|
|
2640
|
+
resolverSpan.end();
|
|
2641
|
+
};
|
|
2642
|
+
}
|
|
2643
|
+
return () => {};
|
|
2644
|
+
}));
|
|
2645
|
+
},
|
|
2646
|
+
onExecute({
|
|
2647
|
+
args,
|
|
2648
|
+
extendContext
|
|
2649
|
+
}) {
|
|
2650
|
+
var _args$document$defini, _args$operationName;
|
|
2651
|
+
const operationType = (_args$document$defini = args.document.definitions.filter(def => def.kind === graphql.Kind.OPERATION_DEFINITION).map(def => def.operation)) == null ? void 0 : _args$document$defini[0];
|
|
2652
|
+
// Span name according to Semantic Conventions
|
|
2653
|
+
// https://github.com/open-telemetry/semantic-conventions
|
|
2654
|
+
let spanName = 'GraphQL Operation';
|
|
2655
|
+
if (operationType && args.operationName) {
|
|
2656
|
+
spanName = `${operationType} ${args.operationName}`;
|
|
2657
|
+
} else if (operationType && !args.operationName) {
|
|
2658
|
+
spanName = operationType;
|
|
2659
|
+
}
|
|
2660
|
+
const executionSpan = tracer.startSpan(spanName, {
|
|
2661
|
+
kind: api.SpanKind.SERVER,
|
|
2662
|
+
attributes: {
|
|
2663
|
+
[AttributeName.EXECUTION_OPERATION_NAME]: (_args$operationName = args.operationName) != null ? _args$operationName : undefined,
|
|
2664
|
+
[AttributeName.EXECUTION_OPERATION_TYPE]: operationType != null ? operationType : undefined,
|
|
2665
|
+
[AttributeName.EXECUTION_OPERATION_DOCUMENT]: graphql.print(args.document)
|
|
2666
|
+
}
|
|
2667
|
+
});
|
|
2668
|
+
const executeContext = api.context.active();
|
|
2669
|
+
const resultCbs = {
|
|
2670
|
+
onExecuteDone({
|
|
2671
|
+
result
|
|
2672
|
+
}) {
|
|
2673
|
+
var _args$operationName2, _args$variableValues;
|
|
2674
|
+
if (core.isAsyncIterable(result)) {
|
|
2675
|
+
executionSpan.end();
|
|
2676
|
+
// eslint-disable-next-line no-console
|
|
2677
|
+
console.warn(`Plugin "newrelic" encountered a AsyncIterator which is not supported yet, so tracing data is not available for the operation.`);
|
|
2678
|
+
return;
|
|
2679
|
+
}
|
|
2680
|
+
const logRecord = {
|
|
2681
|
+
context: executeContext,
|
|
2682
|
+
attributes: {
|
|
2683
|
+
'service.name': 'faststore-api',
|
|
2684
|
+
'service.version': '1.12.38',
|
|
2685
|
+
'service.name_and_version': 'faststore-api@1.12.38',
|
|
2686
|
+
'vtex.search_index': 'faststore_beta_api',
|
|
2687
|
+
[AttributeName.EXECUTION_OPERATION_NAME]: (_args$operationName2 = args.operationName) != null ? _args$operationName2 : undefined,
|
|
2688
|
+
[AttributeName.EXECUTION_OPERATION_DOCUMENT]: graphql.print(args.document),
|
|
2689
|
+
[AttributeName.EXECUTION_VARIABLES]: JSON.stringify((_args$variableValues = args.variableValues) != null ? _args$variableValues : {})
|
|
2690
|
+
}
|
|
2691
|
+
};
|
|
2692
|
+
if (typeof result.data !== 'undefined' && !(result.errors && result.errors.length > 0)) {
|
|
2693
|
+
logRecord.severityNumber = apiLogs.SeverityNumber.INFO;
|
|
2694
|
+
logRecord.severityText = 'Info';
|
|
2695
|
+
logRecord.attributes[AttributeName.EXECUTION_RESULT] = JSON.stringify(result);
|
|
2696
|
+
}
|
|
2697
|
+
if (result.errors && result.errors.length > 0) {
|
|
2698
|
+
logRecord.severityNumber = apiLogs.SeverityNumber.ERROR;
|
|
2699
|
+
logRecord.severityText = 'Error';
|
|
2700
|
+
logRecord.attributes[AttributeName.EXECUTION_ERROR] = JSON.stringify(result.errors);
|
|
2701
|
+
executionSpan.setAttributes({
|
|
2702
|
+
error: true,
|
|
2703
|
+
'exception.category': AttributeName.EXECUTION_ERROR,
|
|
2704
|
+
'exception.message': JSON.stringify(result.errors)
|
|
2705
|
+
});
|
|
2706
|
+
}
|
|
2707
|
+
if (experimentalSendLogs) {
|
|
2708
|
+
logger.emit(logRecord);
|
|
2709
|
+
}
|
|
2710
|
+
executionSpan.end();
|
|
2711
|
+
}
|
|
2712
|
+
};
|
|
2713
|
+
extendContext({
|
|
2714
|
+
[tracingSpanSymbol]: executionSpan
|
|
2715
|
+
});
|
|
2716
|
+
return resultCbs;
|
|
2717
|
+
}
|
|
2718
|
+
};
|
|
2719
|
+
};
|
|
2720
|
+
};
|
|
2721
|
+
|
|
2722
|
+
const FASTSTORE_API_VERSION = packageJson.version;
|
|
2723
|
+
// TODO: These urls are hardcoded for now, but they should be configurable via ENV variables
|
|
2724
|
+
// They are only acessible from within the VTEX network, so they are not a security risk
|
|
2725
|
+
const TRACE_COLLECTOR_URL = 'opentelemetry-collector.vtex.systems';
|
|
2726
|
+
const TRACE_COLLECTOR_URL_DEV = 'opentelemetry-collector-beta.vtex.systems';
|
|
2727
|
+
const LOG_COLLECTOR_URL = 'opentelemetry-collector.vtex.systems';
|
|
2728
|
+
function getTelemetry(APIOptions, telemetryOptions) {
|
|
2729
|
+
var _telemetryOptions$exp;
|
|
2730
|
+
const honeycombCollectorOptions = {
|
|
2731
|
+
url: (telemetryOptions == null ? void 0 : telemetryOptions.mode) === 'dev' ? TRACE_COLLECTOR_URL_DEV : TRACE_COLLECTOR_URL
|
|
2732
|
+
};
|
|
2733
|
+
const openSearchCollectorOptions = {
|
|
2734
|
+
url: LOG_COLLECTOR_URL
|
|
2735
|
+
};
|
|
2736
|
+
// Create a new tracer provider
|
|
2737
|
+
const tracerProvider = new sdkTraceBase.BasicTracerProvider({
|
|
2738
|
+
resource: new resources.Resource({
|
|
2739
|
+
'service.name': 'faststore-api',
|
|
2740
|
+
'service.version': FASTSTORE_API_VERSION,
|
|
2741
|
+
'service.name_and_version': `faststore-api@${FASTSTORE_API_VERSION}`,
|
|
2742
|
+
platform: APIOptions.platform,
|
|
2743
|
+
[`${APIOptions.platform}.account`]: APIOptions.account,
|
|
2744
|
+
[`${APIOptions.platform}.environment`]: APIOptions.environment,
|
|
2745
|
+
// TODO: include the following properties in the logs
|
|
2746
|
+
// [`${APIOptions.platform}.options.hideUnavailableItems`]:
|
|
2747
|
+
// APIOptions.hideUnavailableItems,
|
|
2748
|
+
// [`${APIOptions.platform}.flags.enableOrderFormSync`]:
|
|
2749
|
+
// APIOptions.flags?.enableOrderFormSync,
|
|
2750
|
+
// channel: APIOptions.channel,
|
|
2751
|
+
locale: APIOptions.locale
|
|
2752
|
+
})
|
|
2753
|
+
});
|
|
2754
|
+
const loggerProvider = new sdkLogs.LoggerProvider();
|
|
2755
|
+
// Create trace exporter
|
|
2756
|
+
const honeycombExporter = new exporterTraceOtlpGrpc.OTLPTraceExporter(honeycombCollectorOptions);
|
|
2757
|
+
// Create log exporter
|
|
2758
|
+
const openSearchExporter = new exporterLogsOtlpGrpc.OTLPLogsExporter(openSearchCollectorOptions);
|
|
2759
|
+
// Set up a span processor to export spans to Honeycomb
|
|
2760
|
+
const honeyCombSpanProcessor = new sdkTraceBase.SimpleSpanProcessor(honeycombExporter);
|
|
2761
|
+
// Set up a log record processor to export spans to OpenSearch
|
|
2762
|
+
const openSearchLogProcessor = new sdkLogs.SimpleLogRecordProcessor(openSearchExporter);
|
|
2763
|
+
// Register the span processor with the tracer provider
|
|
2764
|
+
tracerProvider.addSpanProcessor(honeyCombSpanProcessor);
|
|
2765
|
+
// Register the log record processor with the log provider
|
|
2766
|
+
loggerProvider.addLogRecordProcessor(openSearchLogProcessor);
|
|
2767
|
+
if ((telemetryOptions == null ? void 0 : telemetryOptions.mode) === 'verbose' || (telemetryOptions == null ? void 0 : telemetryOptions.mode) === 'dev') {
|
|
2768
|
+
// Set up a console exporter for verbose mode
|
|
2769
|
+
const consoleExporter = new sdkTraceBase.ConsoleSpanExporter();
|
|
2770
|
+
const verboseTraceProcessor = new sdkTraceBase.SimpleSpanProcessor(consoleExporter);
|
|
2771
|
+
// Set up processors for verbose mode
|
|
2772
|
+
const consoleLogExporter = new sdkLogs.ConsoleLogRecordExporter();
|
|
2773
|
+
const veboseLogRecordExporter = new sdkLogs.SimpleLogRecordProcessor(consoleLogExporter);
|
|
2774
|
+
tracerProvider.addSpanProcessor(verboseTraceProcessor);
|
|
2775
|
+
loggerProvider.addLogRecordProcessor(veboseLogRecordExporter);
|
|
2776
|
+
}
|
|
2777
|
+
const useFaststoreTelemetry = getFaststoreTelemetryPlugin(
|
|
2778
|
+
// The @opentelemetry/sdk-trace-base was renamed from @opentelemetry/tracing but the
|
|
2779
|
+
// envelop plugin doesn't support this change yet. This causes the class type to be incompatible,
|
|
2780
|
+
// even if they are the same. https://github.com/n1ru4l/envelop/issues/1610
|
|
2781
|
+
tracerProvider, loggerProvider, 'faststore-api', (_telemetryOptions$exp = telemetryOptions == null ? void 0 : telemetryOptions.experimentalSendLogs) != null ? _telemetryOptions$exp : false);
|
|
2782
|
+
return {
|
|
2783
|
+
useFaststoreTelemetry
|
|
2784
|
+
};
|
|
2785
|
+
}
|
|
2786
|
+
|
|
2535
2787
|
const platforms = {
|
|
2536
2788
|
vtex: {
|
|
2537
2789
|
getResolvers: getResolvers,
|
|
@@ -2555,6 +2807,7 @@ exports.NotFoundError = NotFoundError;
|
|
|
2555
2807
|
exports.getContextFactory = getContextFactory$1;
|
|
2556
2808
|
exports.getResolvers = getResolvers$1;
|
|
2557
2809
|
exports.getSchema = getSchema;
|
|
2810
|
+
exports.getTelemetry = getTelemetry;
|
|
2558
2811
|
exports.getTypeDefs = getTypeDefs;
|
|
2559
2812
|
exports.isBadRequestError = isBadRequestError;
|
|
2560
2813
|
exports.isFastStoreError = isFastStoreError;
|