@frengki0707/google-cloud-clone 1.33.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/LICENSE +203 -0
- package/README.md +83 -0
- package/lib/auth.d.mts +33 -0
- package/lib/auth.d.ts +33 -0
- package/lib/auth.js +70 -0
- package/lib/auth.js.map +1 -0
- package/lib/auth.mjs +45 -0
- package/lib/auth.mjs.map +1 -0
- package/lib/gcpLogger.d.mts +25 -0
- package/lib/gcpLogger.d.ts +25 -0
- package/lib/gcpLogger.js +118 -0
- package/lib/gcpLogger.js.map +1 -0
- package/lib/gcpLogger.mjs +82 -0
- package/lib/gcpLogger.mjs.map +1 -0
- package/lib/gcpOpenTelemetry.d.mts +59 -0
- package/lib/gcpOpenTelemetry.d.ts +59 -0
- package/lib/gcpOpenTelemetry.js +374 -0
- package/lib/gcpOpenTelemetry.js.map +1 -0
- package/lib/gcpOpenTelemetry.mjs +364 -0
- package/lib/gcpOpenTelemetry.mjs.map +1 -0
- package/lib/index.d.mts +36 -0
- package/lib/index.d.ts +36 -0
- package/lib/index.js +56 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +29 -0
- package/lib/index.mjs.map +1 -0
- package/lib/metrics.d.mts +65 -0
- package/lib/metrics.d.ts +65 -0
- package/lib/metrics.js +91 -0
- package/lib/metrics.js.map +1 -0
- package/lib/metrics.mjs +65 -0
- package/lib/metrics.mjs.map +1 -0
- package/lib/model-armor.d.mts +59 -0
- package/lib/model-armor.d.ts +59 -0
- package/lib/model-armor.js +205 -0
- package/lib/model-armor.js.map +1 -0
- package/lib/model-armor.mjs +181 -0
- package/lib/model-armor.mjs.map +1 -0
- package/lib/telemetry/action.d.mts +27 -0
- package/lib/telemetry/action.d.ts +27 -0
- package/lib/telemetry/action.js +92 -0
- package/lib/telemetry/action.js.map +1 -0
- package/lib/telemetry/action.mjs +73 -0
- package/lib/telemetry/action.mjs.map +1 -0
- package/lib/telemetry/defaults.d.mts +30 -0
- package/lib/telemetry/defaults.d.ts +30 -0
- package/lib/telemetry/defaults.js +70 -0
- package/lib/telemetry/defaults.js.map +1 -0
- package/lib/telemetry/defaults.mjs +46 -0
- package/lib/telemetry/defaults.mjs.map +1 -0
- package/lib/telemetry/engagement.d.mts +35 -0
- package/lib/telemetry/engagement.d.ts +35 -0
- package/lib/telemetry/engagement.js +106 -0
- package/lib/telemetry/engagement.js.map +1 -0
- package/lib/telemetry/engagement.mjs +85 -0
- package/lib/telemetry/engagement.mjs.map +1 -0
- package/lib/telemetry/feature.d.mts +35 -0
- package/lib/telemetry/feature.d.ts +35 -0
- package/lib/telemetry/feature.js +142 -0
- package/lib/telemetry/feature.js.map +1 -0
- package/lib/telemetry/feature.mjs +127 -0
- package/lib/telemetry/feature.mjs.map +1 -0
- package/lib/telemetry/generate.d.mts +53 -0
- package/lib/telemetry/generate.d.ts +53 -0
- package/lib/telemetry/generate.js +326 -0
- package/lib/telemetry/generate.js.map +1 -0
- package/lib/telemetry/generate.mjs +314 -0
- package/lib/telemetry/generate.mjs.map +1 -0
- package/lib/telemetry/path.d.mts +32 -0
- package/lib/telemetry/path.d.ts +32 -0
- package/lib/telemetry/path.js +91 -0
- package/lib/telemetry/path.js.map +1 -0
- package/lib/telemetry/path.mjs +78 -0
- package/lib/telemetry/path.mjs.map +1 -0
- package/lib/types.d.mts +121 -0
- package/lib/types.d.ts +121 -0
- package/lib/types.js +17 -0
- package/lib/types.js.map +1 -0
- package/lib/types.mjs +1 -0
- package/lib/types.mjs.map +1 -0
- package/lib/utils.d.mts +57 -0
- package/lib/utils.d.ts +57 -0
- package/lib/utils.js +143 -0
- package/lib/utils.js.map +1 -0
- package/lib/utils.mjs +104 -0
- package/lib/utils.mjs.map +1 -0
- package/package.json +89 -0
- package/src/auth.ts +89 -0
- package/src/gcpLogger.ts +124 -0
- package/src/gcpOpenTelemetry.ts +485 -0
- package/src/index.ts +59 -0
- package/src/metrics.ts +122 -0
- package/src/model-armor.ts +317 -0
- package/src/telemetry/action.ts +106 -0
- package/src/telemetry/defaults.ts +72 -0
- package/src/telemetry/engagement.ts +120 -0
- package/src/telemetry/feature.ts +170 -0
- package/src/telemetry/generate.ts +454 -0
- package/src/telemetry/path.ts +111 -0
- package/src/types.ts +133 -0
- package/src/utils.ts +175 -0
- package/tests/logs_no_input_output_test.ts +267 -0
- package/tests/logs_session_test.ts +219 -0
- package/tests/logs_test.ts +633 -0
- package/tests/metrics_test.ts +792 -0
- package/tests/model_armor_test.ts +336 -0
- package/tests/traces_test.ts +380 -0
- package/typedoc.json +3 -0
package/lib/types.d.mts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
|
|
2
|
+
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
3
|
+
import { Sampler } from '@opentelemetry/sdk-trace-base';
|
|
4
|
+
import { JWTInput } from 'google-auth-library';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Copyright 2024 Google LLC
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Configuration options for the Google Cloud plugin. */
|
|
23
|
+
interface GcpTelemetryConfigOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Google Cloud Project ID. If provided, will take precedence over the
|
|
26
|
+
* projectId inferred from the application credential and/or environment.
|
|
27
|
+
* Required when providing an external credential (e.g. Workload Identity
|
|
28
|
+
* Federation.)
|
|
29
|
+
*/
|
|
30
|
+
projectId?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Credentials for authenticating with Google Cloud. Primarily intended for
|
|
33
|
+
* use in environments outside of GCP. On GCP credentials will typically be
|
|
34
|
+
* inferred from the environment via Application Default Credentials (ADC).
|
|
35
|
+
*/
|
|
36
|
+
credentials?: JWTInput;
|
|
37
|
+
/**
|
|
38
|
+
* OpenTelemetry sampler; controls the number of traces collected and exported
|
|
39
|
+
* to Google Cloud. Defaults to AlwaysOnSampler, which will collect and export
|
|
40
|
+
* all traces.
|
|
41
|
+
*
|
|
42
|
+
* There are four built-in samplers to choose from:
|
|
43
|
+
*
|
|
44
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/AlwaysOnSampler.ts | AlwaysOnSampler} - samples all traces
|
|
45
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/AlwaysOffSampler.ts | AlwaysOffSampler} - samples no traces
|
|
46
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts | ParentBasedSampler} - samples based on parent span
|
|
47
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/TraceIdRatioBasedSampler.ts | TraceIdRatioBasedSampler} - samples a configurable percentage of traces
|
|
48
|
+
*/
|
|
49
|
+
sampler?: Sampler;
|
|
50
|
+
/**
|
|
51
|
+
* Enabled by default, OpenTelemetry will automatically collect telemetry for
|
|
52
|
+
* popular libraries via auto instrumentations without any additional code
|
|
53
|
+
* or configuration. All available instrumentations will be collected, unless
|
|
54
|
+
* otherwise specified via {@link autoInstrumentationConfig}.
|
|
55
|
+
*
|
|
56
|
+
* @see https://opentelemetry.io/docs/zero-code/js/
|
|
57
|
+
*/
|
|
58
|
+
autoInstrumentation?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Map of auto instrumentations and their configuration options. Available
|
|
61
|
+
* options will vary by instrumentation.
|
|
62
|
+
*
|
|
63
|
+
* @see https://opentelemetry.io/docs/zero-code/js/
|
|
64
|
+
*/
|
|
65
|
+
autoInstrumentationConfig?: InstrumentationConfigMap;
|
|
66
|
+
/**
|
|
67
|
+
* Additional OpenTelemetry instrumentations to include, beyond those
|
|
68
|
+
* provided by auto instrumentations.
|
|
69
|
+
*/
|
|
70
|
+
instrumentations?: Instrumentation[];
|
|
71
|
+
/**
|
|
72
|
+
* Metrics export interval in milliseconds; Google Cloud requires a minimum
|
|
73
|
+
* value of 5000ms.
|
|
74
|
+
*/
|
|
75
|
+
metricExportIntervalMillis?: number;
|
|
76
|
+
/**
|
|
77
|
+
* Timeout for the metrics export in milliseconds.
|
|
78
|
+
*/
|
|
79
|
+
metricExportTimeoutMillis?: number;
|
|
80
|
+
/**
|
|
81
|
+
* If set to true, metrics will not be exported to Google Cloud. Traces and
|
|
82
|
+
* logs may still be exported.
|
|
83
|
+
*/
|
|
84
|
+
disableMetrics?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* If set to true, traces will not be exported to Google Cloud. Metrics and
|
|
87
|
+
* logs may still be exported.
|
|
88
|
+
*/
|
|
89
|
+
disableTraces?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* If set to true, input and output logs will not be collected.
|
|
92
|
+
*/
|
|
93
|
+
disableLoggingInputAndOutput?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* If set to true, telemetry data will be exported in the Genkit `dev`
|
|
96
|
+
* environment. Useful for local testing and troubleshooting; default is
|
|
97
|
+
* false.
|
|
98
|
+
*/
|
|
99
|
+
forceDevExport?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/** Internal telemetry configuration. */
|
|
102
|
+
interface GcpTelemetryConfig {
|
|
103
|
+
projectId?: string;
|
|
104
|
+
credentials?: JWTInput;
|
|
105
|
+
sampler: Sampler;
|
|
106
|
+
autoInstrumentation: boolean;
|
|
107
|
+
autoInstrumentationConfig: InstrumentationConfigMap;
|
|
108
|
+
metricExportIntervalMillis: number;
|
|
109
|
+
metricExportTimeoutMillis: number;
|
|
110
|
+
instrumentations: Instrumentation[];
|
|
111
|
+
disableMetrics: boolean;
|
|
112
|
+
disableTraces: boolean;
|
|
113
|
+
exportInputAndOutput: boolean;
|
|
114
|
+
export: boolean;
|
|
115
|
+
}
|
|
116
|
+
interface GcpPrincipal {
|
|
117
|
+
projectId?: string;
|
|
118
|
+
serviceAccountEmail?: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type { GcpPrincipal, GcpTelemetryConfig, GcpTelemetryConfigOptions };
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
|
|
2
|
+
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
3
|
+
import { Sampler } from '@opentelemetry/sdk-trace-base';
|
|
4
|
+
import { JWTInput } from 'google-auth-library';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Copyright 2024 Google LLC
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** Configuration options for the Google Cloud plugin. */
|
|
23
|
+
interface GcpTelemetryConfigOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Google Cloud Project ID. If provided, will take precedence over the
|
|
26
|
+
* projectId inferred from the application credential and/or environment.
|
|
27
|
+
* Required when providing an external credential (e.g. Workload Identity
|
|
28
|
+
* Federation.)
|
|
29
|
+
*/
|
|
30
|
+
projectId?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Credentials for authenticating with Google Cloud. Primarily intended for
|
|
33
|
+
* use in environments outside of GCP. On GCP credentials will typically be
|
|
34
|
+
* inferred from the environment via Application Default Credentials (ADC).
|
|
35
|
+
*/
|
|
36
|
+
credentials?: JWTInput;
|
|
37
|
+
/**
|
|
38
|
+
* OpenTelemetry sampler; controls the number of traces collected and exported
|
|
39
|
+
* to Google Cloud. Defaults to AlwaysOnSampler, which will collect and export
|
|
40
|
+
* all traces.
|
|
41
|
+
*
|
|
42
|
+
* There are four built-in samplers to choose from:
|
|
43
|
+
*
|
|
44
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/AlwaysOnSampler.ts | AlwaysOnSampler} - samples all traces
|
|
45
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/AlwaysOffSampler.ts | AlwaysOffSampler} - samples no traces
|
|
46
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts | ParentBasedSampler} - samples based on parent span
|
|
47
|
+
* - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/TraceIdRatioBasedSampler.ts | TraceIdRatioBasedSampler} - samples a configurable percentage of traces
|
|
48
|
+
*/
|
|
49
|
+
sampler?: Sampler;
|
|
50
|
+
/**
|
|
51
|
+
* Enabled by default, OpenTelemetry will automatically collect telemetry for
|
|
52
|
+
* popular libraries via auto instrumentations without any additional code
|
|
53
|
+
* or configuration. All available instrumentations will be collected, unless
|
|
54
|
+
* otherwise specified via {@link autoInstrumentationConfig}.
|
|
55
|
+
*
|
|
56
|
+
* @see https://opentelemetry.io/docs/zero-code/js/
|
|
57
|
+
*/
|
|
58
|
+
autoInstrumentation?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Map of auto instrumentations and their configuration options. Available
|
|
61
|
+
* options will vary by instrumentation.
|
|
62
|
+
*
|
|
63
|
+
* @see https://opentelemetry.io/docs/zero-code/js/
|
|
64
|
+
*/
|
|
65
|
+
autoInstrumentationConfig?: InstrumentationConfigMap;
|
|
66
|
+
/**
|
|
67
|
+
* Additional OpenTelemetry instrumentations to include, beyond those
|
|
68
|
+
* provided by auto instrumentations.
|
|
69
|
+
*/
|
|
70
|
+
instrumentations?: Instrumentation[];
|
|
71
|
+
/**
|
|
72
|
+
* Metrics export interval in milliseconds; Google Cloud requires a minimum
|
|
73
|
+
* value of 5000ms.
|
|
74
|
+
*/
|
|
75
|
+
metricExportIntervalMillis?: number;
|
|
76
|
+
/**
|
|
77
|
+
* Timeout for the metrics export in milliseconds.
|
|
78
|
+
*/
|
|
79
|
+
metricExportTimeoutMillis?: number;
|
|
80
|
+
/**
|
|
81
|
+
* If set to true, metrics will not be exported to Google Cloud. Traces and
|
|
82
|
+
* logs may still be exported.
|
|
83
|
+
*/
|
|
84
|
+
disableMetrics?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* If set to true, traces will not be exported to Google Cloud. Metrics and
|
|
87
|
+
* logs may still be exported.
|
|
88
|
+
*/
|
|
89
|
+
disableTraces?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* If set to true, input and output logs will not be collected.
|
|
92
|
+
*/
|
|
93
|
+
disableLoggingInputAndOutput?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* If set to true, telemetry data will be exported in the Genkit `dev`
|
|
96
|
+
* environment. Useful for local testing and troubleshooting; default is
|
|
97
|
+
* false.
|
|
98
|
+
*/
|
|
99
|
+
forceDevExport?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/** Internal telemetry configuration. */
|
|
102
|
+
interface GcpTelemetryConfig {
|
|
103
|
+
projectId?: string;
|
|
104
|
+
credentials?: JWTInput;
|
|
105
|
+
sampler: Sampler;
|
|
106
|
+
autoInstrumentation: boolean;
|
|
107
|
+
autoInstrumentationConfig: InstrumentationConfigMap;
|
|
108
|
+
metricExportIntervalMillis: number;
|
|
109
|
+
metricExportTimeoutMillis: number;
|
|
110
|
+
instrumentations: Instrumentation[];
|
|
111
|
+
disableMetrics: boolean;
|
|
112
|
+
disableTraces: boolean;
|
|
113
|
+
exportInputAndOutput: boolean;
|
|
114
|
+
export: boolean;
|
|
115
|
+
}
|
|
116
|
+
interface GcpPrincipal {
|
|
117
|
+
projectId?: string;
|
|
118
|
+
serviceAccountEmail?: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export type { GcpPrincipal, GcpTelemetryConfig, GcpTelemetryConfigOptions };
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
|
17
|
+
//# sourceMappingURL=types.js.map
|
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';\nimport type { Instrumentation } from '@opentelemetry/instrumentation';\nimport type { Sampler } from '@opentelemetry/sdk-trace-base';\nimport type { JWTInput } from 'google-auth-library';\n\n/** Configuration options for the Google Cloud plugin. */\nexport interface GcpTelemetryConfigOptions {\n /**\n * Google Cloud Project ID. If provided, will take precedence over the\n * projectId inferred from the application credential and/or environment.\n * Required when providing an external credential (e.g. Workload Identity\n * Federation.)\n */\n projectId?: string;\n\n /**\n * Credentials for authenticating with Google Cloud. Primarily intended for\n * use in environments outside of GCP. On GCP credentials will typically be\n * inferred from the environment via Application Default Credentials (ADC).\n */\n credentials?: JWTInput;\n\n /**\n * OpenTelemetry sampler; controls the number of traces collected and exported\n * to Google Cloud. Defaults to AlwaysOnSampler, which will collect and export\n * all traces.\n *\n * There are four built-in samplers to choose from:\n *\n * - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/AlwaysOnSampler.ts | AlwaysOnSampler} - samples all traces\n * - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/AlwaysOffSampler.ts | AlwaysOffSampler} - samples no traces\n * - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts | ParentBasedSampler} - samples based on parent span\n * - {@link https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-sdk-trace-base/src/sampler/TraceIdRatioBasedSampler.ts | TraceIdRatioBasedSampler} - samples a configurable percentage of traces\n */\n sampler?: Sampler;\n\n /**\n * Enabled by default, OpenTelemetry will automatically collect telemetry for\n * popular libraries via auto instrumentations without any additional code\n * or configuration. All available instrumentations will be collected, unless\n * otherwise specified via {@link autoInstrumentationConfig}.\n *\n * @see https://opentelemetry.io/docs/zero-code/js/\n */\n autoInstrumentation?: boolean;\n\n /**\n * Map of auto instrumentations and their configuration options. Available\n * options will vary by instrumentation.\n *\n * @see https://opentelemetry.io/docs/zero-code/js/\n */\n autoInstrumentationConfig?: InstrumentationConfigMap;\n\n /**\n * Additional OpenTelemetry instrumentations to include, beyond those\n * provided by auto instrumentations.\n */\n instrumentations?: Instrumentation[];\n\n /**\n * Metrics export interval in milliseconds; Google Cloud requires a minimum\n * value of 5000ms.\n */\n metricExportIntervalMillis?: number;\n\n /**\n * Timeout for the metrics export in milliseconds.\n */\n metricExportTimeoutMillis?: number;\n\n /**\n * If set to true, metrics will not be exported to Google Cloud. Traces and\n * logs may still be exported.\n */\n disableMetrics?: boolean;\n\n /**\n * If set to true, traces will not be exported to Google Cloud. Metrics and\n * logs may still be exported.\n */\n disableTraces?: boolean;\n\n /**\n * If set to true, input and output logs will not be collected.\n */\n disableLoggingInputAndOutput?: boolean;\n\n /**\n * If set to true, telemetry data will be exported in the Genkit `dev`\n * environment. Useful for local testing and troubleshooting; default is\n * false.\n */\n forceDevExport?: boolean;\n}\n\n/** Internal telemetry configuration. */\nexport interface GcpTelemetryConfig {\n projectId?: string;\n credentials?: JWTInput;\n\n sampler: Sampler;\n autoInstrumentation: boolean;\n autoInstrumentationConfig: InstrumentationConfigMap;\n metricExportIntervalMillis: number;\n metricExportTimeoutMillis: number;\n instrumentations: Instrumentation[];\n disableMetrics: boolean;\n disableTraces: boolean;\n exportInputAndOutput: boolean;\n export: boolean;\n}\n\nexport interface GcpPrincipal {\n projectId?: string;\n serviceAccountEmail?: string;\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
package/lib/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/lib/utils.d.mts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Copyright 2024 Google LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
declare function extractOuterFlowNameFromPath(path: string): string;
|
|
20
|
+
declare function truncate(text: string, limit?: number): string;
|
|
21
|
+
declare function truncatePath(path: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Extract first feature name from a path
|
|
24
|
+
* e.g. for /{myFlow,t:flow}/{myStep,t:flowStep}/{googleai/gemini-pro,t:action,s:model}
|
|
25
|
+
* returns "myFlow"
|
|
26
|
+
*/
|
|
27
|
+
declare function extractOuterFeatureNameFromPath(path: string): string;
|
|
28
|
+
declare function extractErrorName(events: TimedEvent[]): string | undefined;
|
|
29
|
+
declare function extractErrorMessage(events: TimedEvent[]): string | undefined;
|
|
30
|
+
declare function extractErrorStack(events: TimedEvent[]): string | undefined;
|
|
31
|
+
declare function createCommonLogAttributes(span: ReadableSpan, projectId?: string): {
|
|
32
|
+
'logging.googleapis.com/spanId': string;
|
|
33
|
+
'logging.googleapis.com/trace': string;
|
|
34
|
+
'logging.googleapis.com/trace_sampled': string;
|
|
35
|
+
};
|
|
36
|
+
declare function requestDenied(err: Error & {
|
|
37
|
+
code?: number;
|
|
38
|
+
statusDetails?: Record<string, any>[];
|
|
39
|
+
}): boolean;
|
|
40
|
+
declare function loggingDenied(err: Error & {
|
|
41
|
+
code?: number;
|
|
42
|
+
statusDetails?: Record<string, any>[];
|
|
43
|
+
}): boolean | undefined;
|
|
44
|
+
declare function tracingDenied(err: Error & {
|
|
45
|
+
code?: number;
|
|
46
|
+
statusDetails?: Record<string, any>[];
|
|
47
|
+
}): boolean;
|
|
48
|
+
declare function metricsDenied(err: Error & {
|
|
49
|
+
code?: number;
|
|
50
|
+
statusDetails?: Record<string, any>[];
|
|
51
|
+
}): boolean;
|
|
52
|
+
declare function permissionDeniedHelpText(role: string): Promise<string>;
|
|
53
|
+
declare function loggingDeniedHelpText(): Promise<string>;
|
|
54
|
+
declare function tracingDeniedHelpText(): Promise<string>;
|
|
55
|
+
declare function metricsDeniedHelpText(): Promise<string>;
|
|
56
|
+
|
|
57
|
+
export { createCommonLogAttributes, extractErrorMessage, extractErrorName, extractErrorStack, extractOuterFeatureNameFromPath, extractOuterFlowNameFromPath, loggingDenied, loggingDeniedHelpText, metricsDenied, metricsDeniedHelpText, permissionDeniedHelpText, requestDenied, tracingDenied, tracingDeniedHelpText, truncate, truncatePath };
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Copyright 2024 Google LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
declare function extractOuterFlowNameFromPath(path: string): string;
|
|
20
|
+
declare function truncate(text: string, limit?: number): string;
|
|
21
|
+
declare function truncatePath(path: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Extract first feature name from a path
|
|
24
|
+
* e.g. for /{myFlow,t:flow}/{myStep,t:flowStep}/{googleai/gemini-pro,t:action,s:model}
|
|
25
|
+
* returns "myFlow"
|
|
26
|
+
*/
|
|
27
|
+
declare function extractOuterFeatureNameFromPath(path: string): string;
|
|
28
|
+
declare function extractErrorName(events: TimedEvent[]): string | undefined;
|
|
29
|
+
declare function extractErrorMessage(events: TimedEvent[]): string | undefined;
|
|
30
|
+
declare function extractErrorStack(events: TimedEvent[]): string | undefined;
|
|
31
|
+
declare function createCommonLogAttributes(span: ReadableSpan, projectId?: string): {
|
|
32
|
+
'logging.googleapis.com/spanId': string;
|
|
33
|
+
'logging.googleapis.com/trace': string;
|
|
34
|
+
'logging.googleapis.com/trace_sampled': string;
|
|
35
|
+
};
|
|
36
|
+
declare function requestDenied(err: Error & {
|
|
37
|
+
code?: number;
|
|
38
|
+
statusDetails?: Record<string, any>[];
|
|
39
|
+
}): boolean;
|
|
40
|
+
declare function loggingDenied(err: Error & {
|
|
41
|
+
code?: number;
|
|
42
|
+
statusDetails?: Record<string, any>[];
|
|
43
|
+
}): boolean | undefined;
|
|
44
|
+
declare function tracingDenied(err: Error & {
|
|
45
|
+
code?: number;
|
|
46
|
+
statusDetails?: Record<string, any>[];
|
|
47
|
+
}): boolean;
|
|
48
|
+
declare function metricsDenied(err: Error & {
|
|
49
|
+
code?: number;
|
|
50
|
+
statusDetails?: Record<string, any>[];
|
|
51
|
+
}): boolean;
|
|
52
|
+
declare function permissionDeniedHelpText(role: string): Promise<string>;
|
|
53
|
+
declare function loggingDeniedHelpText(): Promise<string>;
|
|
54
|
+
declare function tracingDeniedHelpText(): Promise<string>;
|
|
55
|
+
declare function metricsDeniedHelpText(): Promise<string>;
|
|
56
|
+
|
|
57
|
+
export { createCommonLogAttributes, extractErrorMessage, extractErrorName, extractErrorStack, extractOuterFeatureNameFromPath, extractOuterFlowNameFromPath, loggingDenied, loggingDeniedHelpText, metricsDenied, metricsDeniedHelpText, permissionDeniedHelpText, requestDenied, tracingDenied, tracingDeniedHelpText, truncate, truncatePath };
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var utils_exports = {};
|
|
20
|
+
__export(utils_exports, {
|
|
21
|
+
createCommonLogAttributes: () => createCommonLogAttributes,
|
|
22
|
+
extractErrorMessage: () => extractErrorMessage,
|
|
23
|
+
extractErrorName: () => extractErrorName,
|
|
24
|
+
extractErrorStack: () => extractErrorStack,
|
|
25
|
+
extractOuterFeatureNameFromPath: () => extractOuterFeatureNameFromPath,
|
|
26
|
+
extractOuterFlowNameFromPath: () => extractOuterFlowNameFromPath,
|
|
27
|
+
loggingDenied: () => loggingDenied,
|
|
28
|
+
loggingDeniedHelpText: () => loggingDeniedHelpText,
|
|
29
|
+
metricsDenied: () => metricsDenied,
|
|
30
|
+
metricsDeniedHelpText: () => metricsDeniedHelpText,
|
|
31
|
+
permissionDeniedHelpText: () => permissionDeniedHelpText,
|
|
32
|
+
requestDenied: () => requestDenied,
|
|
33
|
+
tracingDenied: () => tracingDenied,
|
|
34
|
+
tracingDeniedHelpText: () => tracingDeniedHelpText,
|
|
35
|
+
truncate: () => truncate,
|
|
36
|
+
truncatePath: () => truncatePath
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(utils_exports);
|
|
39
|
+
var import_api = require("@opentelemetry/api");
|
|
40
|
+
var import_auth = require("./auth.js");
|
|
41
|
+
const MAX_LOG_CONTENT_CHARS = 128e3;
|
|
42
|
+
const MAX_PATH_CHARS = 4096;
|
|
43
|
+
function extractOuterFlowNameFromPath(path) {
|
|
44
|
+
if (!path || path === "<unknown>") {
|
|
45
|
+
return "<unknown>";
|
|
46
|
+
}
|
|
47
|
+
const flowName = path.match("/{(.+),t:flow}+");
|
|
48
|
+
return flowName ? flowName[1] : "<unknown>";
|
|
49
|
+
}
|
|
50
|
+
function truncate(text, limit = MAX_LOG_CONTENT_CHARS) {
|
|
51
|
+
return text ? text.substring(0, limit) : text;
|
|
52
|
+
}
|
|
53
|
+
function truncatePath(path) {
|
|
54
|
+
return truncate(path, MAX_PATH_CHARS);
|
|
55
|
+
}
|
|
56
|
+
function extractOuterFeatureNameFromPath(path) {
|
|
57
|
+
if (!path || path === "<unknown>") {
|
|
58
|
+
return "<unknown>";
|
|
59
|
+
}
|
|
60
|
+
const first = path.split("/")[1];
|
|
61
|
+
const featureName = first?.match(
|
|
62
|
+
"{(.+),t:(flow|action|prompt|dotprompt|helper)"
|
|
63
|
+
);
|
|
64
|
+
return featureName ? featureName[1] : "<unknown>";
|
|
65
|
+
}
|
|
66
|
+
function extractErrorName(events) {
|
|
67
|
+
return events.filter((event) => event.name === "exception").map((event) => {
|
|
68
|
+
const attributes = event.attributes;
|
|
69
|
+
return attributes ? truncate(attributes["exception.type"], 1024) : "<unknown>";
|
|
70
|
+
}).at(0);
|
|
71
|
+
}
|
|
72
|
+
function extractErrorMessage(events) {
|
|
73
|
+
return events.filter((event) => event.name === "exception").map((event) => {
|
|
74
|
+
const attributes = event.attributes;
|
|
75
|
+
return attributes ? truncate(attributes["exception.message"], 4096) : "<unknown>";
|
|
76
|
+
}).at(0);
|
|
77
|
+
}
|
|
78
|
+
function extractErrorStack(events) {
|
|
79
|
+
return events.filter((event) => event.name === "exception").map((event) => {
|
|
80
|
+
const attributes = event.attributes;
|
|
81
|
+
return attributes ? truncate(attributes["exception.stacktrace"], 32768) : "<unknown>";
|
|
82
|
+
}).at(0);
|
|
83
|
+
}
|
|
84
|
+
function createCommonLogAttributes(span, projectId) {
|
|
85
|
+
const spanContext = span.spanContext();
|
|
86
|
+
const isSampled = !!(spanContext.traceFlags & import_api.TraceFlags.SAMPLED);
|
|
87
|
+
return {
|
|
88
|
+
"logging.googleapis.com/spanId": spanContext.spanId,
|
|
89
|
+
"logging.googleapis.com/trace": `projects/${projectId}/traces/${spanContext.traceId}`,
|
|
90
|
+
"logging.googleapis.com/trace_sampled": isSampled ? "1" : "0"
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function requestDenied(err) {
|
|
94
|
+
return err.code === 7;
|
|
95
|
+
}
|
|
96
|
+
function loggingDenied(err) {
|
|
97
|
+
return requestDenied(err) && err.statusDetails?.some((details) => {
|
|
98
|
+
return details?.metadata?.permission === "logging.logEntries.create";
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function tracingDenied(err) {
|
|
102
|
+
return requestDenied(err);
|
|
103
|
+
}
|
|
104
|
+
function metricsDenied(err) {
|
|
105
|
+
return requestDenied(err);
|
|
106
|
+
}
|
|
107
|
+
async function permissionDeniedHelpText(role) {
|
|
108
|
+
const principal = await (0, import_auth.resolveCurrentPrincipal)();
|
|
109
|
+
return `Add the role '${role}' to your Service Account in the IAM & Admin page on the Google Cloud console, or use the following command:
|
|
110
|
+
|
|
111
|
+
gcloud projects add-iam-policy-binding ${principal.projectId ?? "${PROJECT_ID}"} \\
|
|
112
|
+
--member=serviceAccount:${principal.serviceAccountEmail || "${SERVICE_ACCT}"} \\
|
|
113
|
+
--role=${role}`;
|
|
114
|
+
}
|
|
115
|
+
async function loggingDeniedHelpText() {
|
|
116
|
+
return permissionDeniedHelpText("roles/logging.logWriter");
|
|
117
|
+
}
|
|
118
|
+
async function tracingDeniedHelpText() {
|
|
119
|
+
return permissionDeniedHelpText("roles/cloudtrace.agent");
|
|
120
|
+
}
|
|
121
|
+
async function metricsDeniedHelpText() {
|
|
122
|
+
return permissionDeniedHelpText("roles/monitoring.metricWriter");
|
|
123
|
+
}
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
createCommonLogAttributes,
|
|
127
|
+
extractErrorMessage,
|
|
128
|
+
extractErrorName,
|
|
129
|
+
extractErrorStack,
|
|
130
|
+
extractOuterFeatureNameFromPath,
|
|
131
|
+
extractOuterFlowNameFromPath,
|
|
132
|
+
loggingDenied,
|
|
133
|
+
loggingDeniedHelpText,
|
|
134
|
+
metricsDenied,
|
|
135
|
+
metricsDeniedHelpText,
|
|
136
|
+
permissionDeniedHelpText,
|
|
137
|
+
requestDenied,
|
|
138
|
+
tracingDenied,
|
|
139
|
+
tracingDeniedHelpText,
|
|
140
|
+
truncate,
|
|
141
|
+
truncatePath
|
|
142
|
+
});
|
|
143
|
+
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { TraceFlags } from '@opentelemetry/api';\nimport type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';\nimport { resolveCurrentPrincipal } from './auth.js';\n\n/**\n * The maximum length (in characters) of a logged input or output.\n * This limit exists to align the logs with GCP logging size limits.\n * */\nconst MAX_LOG_CONTENT_CHARS = 128_000;\n\n/**\n * The maximum length (in characters) of a flow path.\n */\nconst MAX_PATH_CHARS = 4096;\n\nexport function extractOuterFlowNameFromPath(path: string) {\n if (!path || path === '<unknown>') {\n return '<unknown>';\n }\n\n const flowName = path.match('/{(.+),t:flow}+');\n return flowName ? flowName[1] : '<unknown>';\n}\n\nexport function truncate(\n text: string,\n limit: number = MAX_LOG_CONTENT_CHARS\n): string {\n return text ? text.substring(0, limit) : text;\n}\n\nexport function truncatePath(path: string) {\n return truncate(path, MAX_PATH_CHARS);\n}\n\n/**\n * Extract first feature name from a path\n * e.g. for /{myFlow,t:flow}/{myStep,t:flowStep}/{googleai/gemini-pro,t:action,s:model}\n * returns \"myFlow\"\n */\nexport function extractOuterFeatureNameFromPath(path: string) {\n if (!path || path === '<unknown>') {\n return '<unknown>';\n }\n const first = path.split('/')[1];\n const featureName = first?.match(\n '{(.+),t:(flow|action|prompt|dotprompt|helper)'\n );\n return featureName ? featureName[1] : '<unknown>';\n}\n\nexport function extractErrorName(events: TimedEvent[]): string | undefined {\n return events\n .filter((event) => event.name === 'exception')\n .map((event) => {\n const attributes = event.attributes;\n return attributes\n ? truncate(attributes['exception.type'] as string, 1024)\n : '<unknown>';\n })\n .at(0);\n}\n\nexport function extractErrorMessage(events: TimedEvent[]): string | undefined {\n return events\n .filter((event) => event.name === 'exception')\n .map((event) => {\n const attributes = event.attributes;\n return attributes\n ? truncate(attributes['exception.message'] as string, 4096)\n : '<unknown>';\n })\n .at(0);\n}\n\nexport function extractErrorStack(events: TimedEvent[]): string | undefined {\n return events\n .filter((event) => event.name === 'exception')\n .map((event) => {\n const attributes = event.attributes;\n return attributes\n ? truncate(attributes['exception.stacktrace'] as string, 32_768)\n : '<unknown>';\n })\n .at(0);\n}\n\nexport function createCommonLogAttributes(\n span: ReadableSpan,\n projectId?: string\n) {\n const spanContext = span.spanContext();\n const isSampled = !!(spanContext.traceFlags & TraceFlags.SAMPLED);\n return {\n 'logging.googleapis.com/spanId': spanContext.spanId,\n 'logging.googleapis.com/trace': `projects/${projectId}/traces/${spanContext.traceId}`,\n 'logging.googleapis.com/trace_sampled': isSampled ? '1' : '0',\n };\n}\n\nexport function requestDenied(\n err: Error & {\n code?: number;\n statusDetails?: Record<string, any>[];\n }\n) {\n return err.code === 7;\n}\n\nexport function loggingDenied(\n err: Error & {\n code?: number;\n statusDetails?: Record<string, any>[];\n }\n) {\n return (\n requestDenied(err) &&\n err.statusDetails?.some((details) => {\n return details?.metadata?.permission === 'logging.logEntries.create';\n })\n );\n}\n\nexport function tracingDenied(\n err: Error & {\n code?: number;\n statusDetails?: Record<string, any>[];\n }\n) {\n // Looks like we don't get status details like we do with logging\n return requestDenied(err);\n}\n\nexport function metricsDenied(\n err: Error & {\n code?: number;\n statusDetails?: Record<string, any>[];\n }\n) {\n // Looks like we don't get status details like we do with logging\n return requestDenied(err);\n}\n\nexport async function permissionDeniedHelpText(role: string) {\n const principal = await resolveCurrentPrincipal();\n return `Add the role '${role}' to your Service Account in the IAM & Admin page on the Google Cloud console, or use the following command:\\n\\ngcloud projects add-iam-policy-binding ${principal.projectId ?? '${PROJECT_ID}'} \\\\\\n --member=serviceAccount:${principal.serviceAccountEmail || '${SERVICE_ACCT}'} \\\\\\n --role=${role}`;\n}\n\nexport async function loggingDeniedHelpText() {\n return permissionDeniedHelpText('roles/logging.logWriter');\n}\n\nexport async function tracingDeniedHelpText() {\n return permissionDeniedHelpText('roles/cloudtrace.agent');\n}\n\nexport async function metricsDeniedHelpText() {\n return permissionDeniedHelpText('roles/monitoring.metricWriter');\n}\n"],"mappings":"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","names":[]}
|
package/lib/utils.mjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { TraceFlags } from "@opentelemetry/api";
|
|
2
|
+
import { resolveCurrentPrincipal } from "./auth.js";
|
|
3
|
+
const MAX_LOG_CONTENT_CHARS = 128e3;
|
|
4
|
+
const MAX_PATH_CHARS = 4096;
|
|
5
|
+
function extractOuterFlowNameFromPath(path) {
|
|
6
|
+
if (!path || path === "<unknown>") {
|
|
7
|
+
return "<unknown>";
|
|
8
|
+
}
|
|
9
|
+
const flowName = path.match("/{(.+),t:flow}+");
|
|
10
|
+
return flowName ? flowName[1] : "<unknown>";
|
|
11
|
+
}
|
|
12
|
+
function truncate(text, limit = MAX_LOG_CONTENT_CHARS) {
|
|
13
|
+
return text ? text.substring(0, limit) : text;
|
|
14
|
+
}
|
|
15
|
+
function truncatePath(path) {
|
|
16
|
+
return truncate(path, MAX_PATH_CHARS);
|
|
17
|
+
}
|
|
18
|
+
function extractOuterFeatureNameFromPath(path) {
|
|
19
|
+
if (!path || path === "<unknown>") {
|
|
20
|
+
return "<unknown>";
|
|
21
|
+
}
|
|
22
|
+
const first = path.split("/")[1];
|
|
23
|
+
const featureName = first?.match(
|
|
24
|
+
"{(.+),t:(flow|action|prompt|dotprompt|helper)"
|
|
25
|
+
);
|
|
26
|
+
return featureName ? featureName[1] : "<unknown>";
|
|
27
|
+
}
|
|
28
|
+
function extractErrorName(events) {
|
|
29
|
+
return events.filter((event) => event.name === "exception").map((event) => {
|
|
30
|
+
const attributes = event.attributes;
|
|
31
|
+
return attributes ? truncate(attributes["exception.type"], 1024) : "<unknown>";
|
|
32
|
+
}).at(0);
|
|
33
|
+
}
|
|
34
|
+
function extractErrorMessage(events) {
|
|
35
|
+
return events.filter((event) => event.name === "exception").map((event) => {
|
|
36
|
+
const attributes = event.attributes;
|
|
37
|
+
return attributes ? truncate(attributes["exception.message"], 4096) : "<unknown>";
|
|
38
|
+
}).at(0);
|
|
39
|
+
}
|
|
40
|
+
function extractErrorStack(events) {
|
|
41
|
+
return events.filter((event) => event.name === "exception").map((event) => {
|
|
42
|
+
const attributes = event.attributes;
|
|
43
|
+
return attributes ? truncate(attributes["exception.stacktrace"], 32768) : "<unknown>";
|
|
44
|
+
}).at(0);
|
|
45
|
+
}
|
|
46
|
+
function createCommonLogAttributes(span, projectId) {
|
|
47
|
+
const spanContext = span.spanContext();
|
|
48
|
+
const isSampled = !!(spanContext.traceFlags & TraceFlags.SAMPLED);
|
|
49
|
+
return {
|
|
50
|
+
"logging.googleapis.com/spanId": spanContext.spanId,
|
|
51
|
+
"logging.googleapis.com/trace": `projects/${projectId}/traces/${spanContext.traceId}`,
|
|
52
|
+
"logging.googleapis.com/trace_sampled": isSampled ? "1" : "0"
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function requestDenied(err) {
|
|
56
|
+
return err.code === 7;
|
|
57
|
+
}
|
|
58
|
+
function loggingDenied(err) {
|
|
59
|
+
return requestDenied(err) && err.statusDetails?.some((details) => {
|
|
60
|
+
return details?.metadata?.permission === "logging.logEntries.create";
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function tracingDenied(err) {
|
|
64
|
+
return requestDenied(err);
|
|
65
|
+
}
|
|
66
|
+
function metricsDenied(err) {
|
|
67
|
+
return requestDenied(err);
|
|
68
|
+
}
|
|
69
|
+
async function permissionDeniedHelpText(role) {
|
|
70
|
+
const principal = await resolveCurrentPrincipal();
|
|
71
|
+
return `Add the role '${role}' to your Service Account in the IAM & Admin page on the Google Cloud console, or use the following command:
|
|
72
|
+
|
|
73
|
+
gcloud projects add-iam-policy-binding ${principal.projectId ?? "${PROJECT_ID}"} \\
|
|
74
|
+
--member=serviceAccount:${principal.serviceAccountEmail || "${SERVICE_ACCT}"} \\
|
|
75
|
+
--role=${role}`;
|
|
76
|
+
}
|
|
77
|
+
async function loggingDeniedHelpText() {
|
|
78
|
+
return permissionDeniedHelpText("roles/logging.logWriter");
|
|
79
|
+
}
|
|
80
|
+
async function tracingDeniedHelpText() {
|
|
81
|
+
return permissionDeniedHelpText("roles/cloudtrace.agent");
|
|
82
|
+
}
|
|
83
|
+
async function metricsDeniedHelpText() {
|
|
84
|
+
return permissionDeniedHelpText("roles/monitoring.metricWriter");
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
createCommonLogAttributes,
|
|
88
|
+
extractErrorMessage,
|
|
89
|
+
extractErrorName,
|
|
90
|
+
extractErrorStack,
|
|
91
|
+
extractOuterFeatureNameFromPath,
|
|
92
|
+
extractOuterFlowNameFromPath,
|
|
93
|
+
loggingDenied,
|
|
94
|
+
loggingDeniedHelpText,
|
|
95
|
+
metricsDenied,
|
|
96
|
+
metricsDeniedHelpText,
|
|
97
|
+
permissionDeniedHelpText,
|
|
98
|
+
requestDenied,
|
|
99
|
+
tracingDenied,
|
|
100
|
+
tracingDeniedHelpText,
|
|
101
|
+
truncate,
|
|
102
|
+
truncatePath
|
|
103
|
+
};
|
|
104
|
+
//# sourceMappingURL=utils.mjs.map
|