@teamkeel/functions-runtime 0.336.0 → 0.337.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/src/database.js CHANGED
@@ -2,6 +2,7 @@ const { Kysely, PostgresDialect } = require("kysely");
2
2
  const { AsyncLocalStorage } = require("async_hooks");
3
3
  const pg = require("pg");
4
4
  const { PROTO_ACTION_TYPES } = require("./consts");
5
+ const { withSpan } = require("./tracing");
5
6
 
6
7
  // withDatabase is responsible for setting the correct database client in our AsyncLocalStorage
7
8
  // so that the the code in a custom function uses the correct client.
@@ -73,12 +74,50 @@ function mustEnv(key) {
73
74
  return v;
74
75
  }
75
76
 
77
+ class InstrumentedPool extends pg.Pool {
78
+ async connect(...args) {
79
+ const _super = super.connect.bind(this);
80
+ return withSpan("Database Connect", function () {
81
+ return _super(...args);
82
+ });
83
+ }
84
+ }
85
+
86
+ const txStatements = {
87
+ begin: "Transaction Begin",
88
+ commit: "Transaction Commit",
89
+ rollback: "Transaction Rollback",
90
+ };
91
+
92
+ class InstrumentedClient extends pg.Client {
93
+ async query(...args) {
94
+ const _super = super.query.bind(this);
95
+ const sql = args[0];
96
+
97
+ let sqlAttribute = false;
98
+
99
+ let spanName = txStatements[sql.toLowerCase()];
100
+ if (!spanName) {
101
+ spanName = "Database Query";
102
+ sqlAttribute = true;
103
+ }
104
+
105
+ return withSpan(spanName, function (span) {
106
+ if (sqlAttribute) {
107
+ span.setAttribute("sql", args[0]);
108
+ }
109
+ return _super(...args);
110
+ });
111
+ }
112
+ }
113
+
76
114
  function getDialect() {
77
115
  const dbConnType = process.env["KEEL_DB_CONN_TYPE"];
78
116
  switch (dbConnType) {
79
117
  case "pg":
80
118
  return new PostgresDialect({
81
- pool: new pg.Pool({
119
+ pool: new InstrumentedPool({
120
+ Client: InstrumentedClient,
82
121
  connectionString: mustEnv("KEEL_DB_CONN"),
83
122
  }),
84
123
  });