@studion/infra-code-blocks
Version:
Studion common infra components
157 lines (156 loc) • 6.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtelCollectorBuilder = void 0;
const pulumi = require("@pulumi/pulumi");
const batchProcessor = require("./batch-processor");
const memoryLimiterProcessor = require("./memory-limiter-processor");
const _1 = require(".");
const config_1 = require("./config");
class OtelCollectorBuilder {
_serviceName;
_env;
_configBuilder;
_taskRoleInlinePolicies = [];
constructor(serviceName, env) {
this._serviceName = pulumi.output(serviceName);
this._env = pulumi.output(env);
this._configBuilder = new config_1.OtelCollectorConfigBuilder();
}
withOTLPReceiver(protocols = ['http']) {
this._configBuilder.withOTLPReceiver(protocols);
return this;
}
withBatchProcessor(name = batchProcessor.defaults.name, size = batchProcessor.defaults.size, maxSize = batchProcessor.defaults.maxSize, timeout = batchProcessor.defaults.timeout) {
this._configBuilder.withBatchProcessor(name, size, maxSize, timeout);
return this;
}
withMemoryLimiterProcessor(checkInterval = memoryLimiterProcessor.defaults.checkInterval, limitPercentage = memoryLimiterProcessor.defaults.limitPercentage, spikeLimitPercentage = memoryLimiterProcessor.defaults.spikeLimitPercentage) {
this._configBuilder.withMemoryLimiterProcessor(checkInterval, limitPercentage, spikeLimitPercentage);
return this;
}
withAWSXRayExporter(region) {
this._configBuilder.withAWSXRayExporter(region);
this.createAWSXRayPolicy();
return this;
}
withCloudWatchLogsExporter(region, logGroup, logStreamName) {
this._configBuilder.withCloudWatchLogsExporter(region, logGroup.name, logStreamName, logGroup.retentionInDays);
this.createCloudWatchLogsPolicy(logGroup);
return this;
}
withHealthCheckExtension(endpoint = '0.0.0.0:13133') {
this._configBuilder.withHealthCheckExtension(endpoint);
return this;
}
withPprofExtension(endpoint = '0.0.0.0:1777') {
this._configBuilder.withPprofExtension(endpoint);
return this;
}
withAPS(namespace, workspace, region) {
this._configBuilder.withAPS(pulumi.output(namespace), pulumi.interpolate `${workspace.prometheusEndpoint}api/v1/remote_write`, region);
this.createAPSInlinePolicy(workspace);
return this;
}
withDebug(verbosity = 'detailed') {
this._configBuilder.withDebug(verbosity);
return this;
}
withTelemetry(logLevel = 'error', metricsVerbosity = 'basic') {
this._configBuilder.withTelemetry(logLevel, metricsVerbosity);
return this;
}
withMetricsPipeline(receivers, processors, exporters) {
this._configBuilder.withMetricsPipeline(receivers, processors, exporters);
return this;
}
withTracesPipeline(receivers, processors, exporters) {
this._configBuilder.withTracesPipeline(receivers, processors, exporters);
return this;
}
withLogsPipeline(receivers, processors, exporters) {
this._configBuilder.withLogsPipeline(receivers, processors, exporters);
return this;
}
withDefault({ prometheusNamespace, prometheusWorkspace, region, logGroup, logStreamName, }) {
this._configBuilder.withDefault({
prometheusNamespace,
prometheusEndpoint: pulumi.interpolate `${prometheusWorkspace.prometheusEndpoint}api/v1/remote_write`,
region,
logGroupName: logGroup.name,
logStreamName,
logRetention: logGroup.retentionInDays,
});
this.createAPSInlinePolicy(prometheusWorkspace);
this.createAWSXRayPolicy();
this.createCloudWatchLogsPolicy(logGroup);
return this;
}
build() {
return new _1.OtelCollector(this._serviceName, this._env, this._configBuilder.build(), { taskRoleInlinePolicies: this._taskRoleInlinePolicies });
}
createAPSInlinePolicy(workspace) {
const policy = pulumi
.all([this._serviceName, workspace.arn])
.apply(([serviceName, workspaceArn]) => ({
name: `${serviceName}-task-role-aps-write`,
policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: ['aps:RemoteWrite'],
Resource: workspaceArn,
},
],
}),
}));
this._taskRoleInlinePolicies.push(policy);
}
createAWSXRayPolicy() {
const policy = this._serviceName.apply(serviceName => ({
name: `${serviceName}-task-role-xray`,
policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: [
'xray:PutTraceSegments',
'xray:PutTelemetryRecords',
'xray:GetSamplingRules',
'xray:GetSamplingTargets',
'xray:GetSamplingStatisticSummaries',
],
Resource: '*',
},
],
}),
}));
this._taskRoleInlinePolicies.push(policy);
}
createCloudWatchLogsPolicy(logGroup) {
const policy = pulumi
.all([this._serviceName, logGroup.arn])
.apply(([serviceName, logGroupArn]) => {
return {
name: `${serviceName}-task-role-cloudwatch-logs`,
policy: JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: 'Allow',
Action: [
'logs:CreateLogStream',
'logs:DescribeLogStreams',
'logs:PutLogEvents',
],
Resource: `${logGroupArn}:*`,
},
],
}),
};
});
this._taskRoleInlinePolicies.push(policy);
}
}
exports.OtelCollectorBuilder = OtelCollectorBuilder;