Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: enable opensearch slow logging (#1423)

gbirman7dbee6411785
diff --git a/infra/stacks/opensearch/helpers/scripts/configure_slow_logs.ts b/infra/stacks/opensearch/helpers/scripts/configure_slow_logs.ts
new file mode 100644
index 0000000000..9bd19d46c2
--- /dev/null
+++ b/infra/stacks/opensearch/helpers/scripts/configure_slow_logs.ts
@@ -0,0 +1,65 @@
+import { client } from '../client';
+
+async function configureSlowLogs() {
+ const opensearchClient = client();
+ console.log('Configuring slow query logging thresholds...');
+
+ try {
+ // Configure slow query thresholds for all indices
+ const response = await opensearchClient.indices.putSettings({
+ index: '_all',
+ body: {
+ // Search query slow logs
+ 'index.search.slowlog.threshold.query.warn': '5s',
+ 'index.search.slowlog.threshold.query.info': '2s',
+
+ // Search fetch slow logs
+ 'index.search.slowlog.threshold.fetch.warn': '5s',
+ 'index.search.slowlog.threshold.fetch.info': '2s',
+
+ // Indexing slow logs
+ 'index.indexing.slowlog.threshold.index.warn': '5s',
+ 'index.indexing.slowlog.threshold.index.info': '2s',
+
+ // Slow log level - only log INFO and above (no DEBUG/TRACE)
+ 'index.search.slowlog.level': 'info',
+ 'index.indexing.slowlog.level': 'info',
+ },
+ });
+
+ console.log('✅ Slow log thresholds configured successfully');
+ console.log('Response:', response.body);
+
+ // Verify settings were applied
+ const settings = await opensearchClient.indices.getSettings({
+ index: '_all',
+ });
+
+ console.log('\n📊 Current slow log settings (sample from first index):');
+ const firstIndex = Object.keys(settings.body)[0];
+ if (firstIndex) {
+ const indexSettings = settings.body[firstIndex].settings.index;
+ console.log(
+ 'Search query warn threshold:',
+ indexSettings?.search?.slowlog?.threshold?.query?.warn
+ );
+ console.log(
+ 'Search fetch warn threshold:',
+ indexSettings?.search?.slowlog?.threshold?.fetch?.warn
+ );
+ console.log(
+ 'Indexing warn threshold:',
+ indexSettings?.indexing?.slowlog?.threshold?.index?.warn
+ );
+ }
+
+ console.log('\n📝 Slow queries will now be logged to CloudWatch:');
+ console.log(' - Queries > 5s: WARN level');
+ console.log(' - Queries > 2s: INFO level');
+ } catch (error) {
+ console.error('❌ Error configuring slow logs:', error);
+ throw error;
+ }
+}
+
+configureSlowLogs();
diff --git a/infra/stacks/opensearch/index.ts b/infra/stacks/opensearch/index.ts
index e2eb0eea50..34b40ccb0d 100644
--- a/infra/stacks/opensearch/index.ts
+++ b/infra/stacks/opensearch/index.ts
@@ -40,6 +40,59 @@ new aws.vpc.SecurityGroupEgressRule(`opensearch-all-out`, {
tags,
});
+// Create CloudWatch Log Groups for OpenSearch logs
+const indexSlowLogGroup = new aws.cloudwatch.LogGroup(
+ `opensearch-index-slow-logs-${stack}`,
+ {
+ name: `/aws/opensearch/domains/macro-opensearch-${stack}/index-slow-logs`,
+ retentionInDays: stack === 'prod' ? 30 : 7,
+ tags,
+ }
+);
+
+const searchSlowLogGroup = new aws.cloudwatch.LogGroup(
+ `opensearch-search-slow-logs-${stack}`,
+ {
+ name: `/aws/opensearch/domains/macro-opensearch-${stack}/search-slow-logs`,
+ retentionInDays: stack === 'prod' ? 30 : 7,
+ tags,
+ }
+);
+
+const applicationLogGroup = new aws.cloudwatch.LogGroup(
+ `opensearch-application-logs-${stack}`,
+ {
+ name: `/aws/opensearch/domains/macro-opensearch-${stack}/application-logs`,
+ retentionInDays: stack === 'prod' ? 7 : 3,
+ tags,
+ }
+);
+
+// Create CloudWatch Logs Resource Policy to allow OpenSearch to write logs
+const opensearchLogsPolicy = new aws.cloudwatch.LogResourcePolicy(
+ `opensearch-logs-policy-${stack}`,
+ {
+ policyName: `opensearch-logs-policy-${stack}`,
+ policyDocument: JSON.stringify({
+ Version: '2012-10-17',
+ Statement: [
+ {
+ Effect: 'Allow',
+ Principal: {
+ Service: 'es.amazonaws.com',
+ },
+ Action: ['logs:CreateLogStream', 'logs:PutLogEvents'],
+ Resource: [
+ indexSlowLogGroup.arn.apply((arn) => `${arn}:*`),
+ searchSlowLogGroup.arn.apply((arn) => `${arn}:*`),
+ applicationLogGroup.arn.apply((arn) => `${arn}:*`),
+ ],
+ },
+ ],
+ }),
+ }
+);
+
// Create an OpenSearch domain
const opensearchDomain = new aws.opensearch.Domain(
`macro-opensearch-${stack}`,
@@ -116,9 +169,26 @@ const opensearchDomain = new aws.opensearch.Domain(
}
: undefined,
ipAddressType: stack === 'prod' ? 'ipv4' : 'dualstack', // vpc doesn't have ipv6 for prod
- // TODO: figure out logging configuration
+ logPublishingOptions: [
+ {
+ logType: 'INDEX_SLOW_LOGS',
+ cloudwatchLogGroupArn: indexSlowLogGroup.arn,
+ enabled: true,
+ },
+ {
+ logType: 'SEARCH_SLOW_LOGS',
+ cloudwatchLogGroupArn: searchSlowLogGroup.arn,
+ enabled: true,
+ },
+ {
+ logType: 'ES_APPLICATION_LOGS',
+ cloudwatchLogGroupArn: applicationLogGroup.arn,
+ enabled: true,
+ },
+ ],
tags,
- }
+ },
+ { dependsOn: [opensearchLogsPolicy] }
);
// export the domain endpoint