【发布时间】:2022-01-25 03:50:07
【问题描述】:
我有一个 Azure (AZ) 函数做两件事:
- 验证提交的涉及第 3 方包的信息。
- 正常时在 AZ 调用 postgreSQL 函数以获取一小组数据
使用 Postman 进行测试,此 AF 本地主机响应时间 30 秒 得到Status: 500 Internal Server Error。
进行了搜索,认为this SO 可能是这种情况,我需要将我的订阅提高到昂贵的订阅以避免冷启动。
但更多的调查分别运行第 1 部分和第 2 部分并结合,发现:
- 仅验证部分在 AZ 运行完美,响应时间
- pg 函数调用总是很长,
status: 500无论单独运行还是在第 1 部分之后运行,都没有返回数据。
Application Insight 已启用并添加了 Diagnostic settings 并带有:
- FunctionAppLogs 和 AllMetrics 已选择
- 发送到 LogAnalytics 工作区并流式传输到事件中心已选择
以下查询未发现错误/异常:
requests | order by timestamp desc |limit 100 // success is "true", time taken 30 seconds, status = 500
traces | order by timestamp desc | limit 30 // success is "true", time taken 30 seconds, status = 500
exceptions | limit 30 // no data returned
我的 pg 调用有多复杂?标准连接,简单短:
require('dotenv').config({ path: './environment/PostgreSql.env'});
const fs = require("fs");
const pgp = require('pg-promise')(); // () = taking default initOptions
require('dotenv').config({ path: './environment/PostgreSql.env'});
const fs = require("fs");
const pgp = require('pg-promise')(); // () = taking default initOptions
db = pgp(
{
user: process.env.PGuser,
host: process.env.PGhost,
database: process.env.PGdatabase,
password: process.env.PGpassword,
port: process.env.PGport,
ssl:
{
rejectUnauthorized: true,
ca: fs.readFileSync("./environment/DigiCertGlobalRootCA.crt.pem").toString(),
},
}
);
const pgTest = (nothing) =>
{
return new Promise((resolve, reject) =>
{
var sql = 'select * from schema.test()'; // test() does a select from a 2-row narrrow table.
db.any(sql)
.then
(
good => resolve(good),
bad => reject({status: 555, body: bad})
)
}
);
}
module.exports = { pgTest }
AF test1 是标准的httpTrigger 匿名访问:
const x1 = require("package1");
...
const xx = require("packagex");
const pgdb = require("db");
module.exports = function(context)
{
try
{
pgdb.pgTest(1)
.then
(
good => {context.res={body: good}; context.done();},
bad => {context.res={body: bad}; context.done();}
)
.catch(err => {console.log(err)})
}
catch(e)
{ context.res={body: bad}; context.done(); }
}
注意:
- AZ = Azure。
- AZ pg 不需要 SSL。
- pg 连接方式:
public access (allowed IP addresses) - 本地 F5 上的 Postman 测试针对相同的 AZ pg 数据库、所有相同的区域运行。
-
pgAdmin和psql都跑得很快。 - AF-deploy 是 zip 文件部署,我的理解是使用相同的配置。
- 我是 Azure 的新手,但根据我的经验,如果是关于凭据,那么应该马上回来。
更新 1,FunctionAppLogs | where TimeGenerated between ( datetime(2022-01-21 16:33:20) .. datetime(2022-01-21 16:35:46) )
是不是因为我的pg网络访问设置为Public access?
【问题讨论】:
-
您能否澄清
AZ的意思?这是 Azure 的缩写吗?不清楚。另外:您是否在与 Postgres 实例相同的区域中运行您的代码?你有索引设置吗?你提到总是出错,所以 30 秒似乎是某种类型的凭据超时,而不是数据检索超时,对吧?您是否已验证您可以与您的数据库实例建立任何连接?你能从任何表中检索任何东西吗?请编辑以提供更多详细信息。 -
您需要查看实际查询以了解为什么它很慢。
-
@jjanes sql
test()从 2 行窄表中进行选择。 -
@DavidMakogon 正确,
AZAzure 的缩写。同一个地区?是的,AZ pg 位置与 Postman 和本地相同。 Sqltest()从 2 行窄表中进行选择,不需要索引。返回的总是status: 500,Internal Server Error相信是Postman的解释,同时AZ的Kustov结果是success=True,resultCode=500,duration=24,438.526 ...和performanceBucket=15sec-30sec。 -
@DavidMakogon 请参阅上面的补充说明。如果凭据问题应立即返回,则不应超时。我是 Azure 的新手,试图深入了解它。 :)
标签: node.js postgresql azure azure-functions