Skip to content

Commit

Permalink
šŸ› Fix for allowing custom log source with all custom table configuratā€¦
Browse files Browse the repository at this point in the history
ā€¦ions
  • Loading branch information
shaeqahmed committed Apr 17, 2023
1 parent 682376e commit 8de1367
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions infra/lib/log-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ export class MatanoLogSource extends Construct {
constructor(scope: Construct, id: string, props: MatanoLogSourceProps) {
super(scope, id);

const logSourceConfig = props.config
? props.config
: (readConfig(props.configPath!, "log_source.yml") as LogSourceConfig);
const configPath = props.configPath!;

const logSourceConfig = props.config ? props.config : (readConfig(configPath, "log_source.yml") as LogSourceConfig);
this.logSourceConfig = logSourceConfig;

if (props.config?.name === "matano_alerts") {
Expand All @@ -224,7 +224,6 @@ export class MatanoLogSource extends Construct {
this.name = logSourceName;

if (logSourceConfig?.managed && logSourceConfig.name !== "matano_alerts") {
const configPath = props.configPath!;
const managedLogSourceType = logSourceConfig?.managed?.type?.toLowerCase();
this.managedLogSourceType = managedLogSourceType;
if (!managedLogSourceType) {
Expand Down Expand Up @@ -371,6 +370,35 @@ export class MatanoLogSource extends Construct {
);

const logSourceConfigToMerge = diff(this.logSourceConfig, logSourceLevelConfig);

// if no table configs have been loaded during above managed log source traversal, load table configs now from user tables/ path
if (Object.keys(this.tablesConfig).length == 0) {
// tableConfig has path like tables/*.yml, where the * part is the table name that should be used as a fallback table name
const tableConfigFilePaths = walkdirSync(configPath)
.map((p) => path.relative(configPath, p))
.filter((p) => p.match(/tables\/.*.yml/));
for (const tableConfigFilePath of tableConfigFilePaths) {
const tableConfig = readConfig(configPath, tableConfigFilePath);
if (tableConfig.name == null) {
tableConfig.name = tableConfigFilePath.match(/tables\/(.*).yml/)![1];
// fail if inferred table name contains any non-alphanumeric characters snake_cased
if (!tableConfig.name.match(/^[a-z0-9_]+$/)) {
fail(
`Table name from path ${tableConfig.name} is invalid. Table names must be alphanumeric and snake cased. (log source: ${logSourceName})`
);
}
}

// fail if table name is already defined
if (tableConfig.name in this.tablesConfig) {
fail(`Table name ${tableConfig.name} is already defined in log source: ${logSourceName}`);
}

this.tablesConfig[tableConfig.name] = tableConfig;
}
}

// if no still no tables/ added, assume a default table (from log_source.yml)
if (Object.keys(this.tablesConfig).length == 0) {
this.tablesConfig["default"] = {};
}
Expand Down

0 comments on commit 8de1367

Please sign in to comment.