@studion/infra-code-blocks
Version:
Studion common infra components
98 lines (97 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtelCollector = void 0;
const node_buffer_1 = require("node:buffer");
const pulumi = require("@pulumi/pulumi");
const yaml = require("yaml");
class OtelCollector {
config;
configVolume;
container;
configContainer;
taskRoleInlinePolicies;
constructor(serviceName, env, config, opts = {}) {
const containerName = opts.containerName || pulumi.interpolate `${serviceName}-otel-collector`;
const configVolumeName = opts.configVolumeName || 'otel-collector-config-volume';
this.configVolume = pulumi.output(configVolumeName);
this.taskRoleInlinePolicies = opts.taskRoleInlinePolicies || [];
this.config = pulumi.output(config);
this.configContainer = this.createConfigContainer(this.config, configVolumeName);
this.container = this.createContainer(containerName, this.config, configVolumeName, serviceName, env);
}
createContainer(containerName, config, configVolumeName, serviceName, env) {
return pulumi
.all([containerName, config, configVolumeName, serviceName, env])
.apply(([containerName, config, configVolumeName, serviceName, env]) => ({
name: containerName,
image: 'otel/opentelemetry-collector-contrib:0.123.0',
portMappings: this.getCollectorPortMappings(config),
mountPoints: [
{
sourceVolume: configVolumeName,
containerPath: '/etc/otelcol-contrib',
readOnly: true,
},
],
dependsOn: [
{
containerName: this.configContainer.name,
condition: 'COMPLETE',
},
],
environment: this.getCollectorEnvironment(serviceName, env),
}));
}
getCollectorEnvironment(serviceName, env) {
return [
{
name: 'OTEL_RESOURCE_ATTRIBUTES',
value: `service.name=${serviceName},env=${env}`,
},
];
}
getCollectorPortMappings(config) {
const hasOTLPGRpcReceiver = !!config.receivers.otlp?.protocols.grpc;
const hasOTLPHttpReceiver = !!config.receivers.otlp?.protocols.http;
const protocol = 'tcp';
return [
...(hasOTLPGRpcReceiver
? [{ containerPort: 4317, hostPort: 4317, protocol }]
: []),
...(hasOTLPHttpReceiver
? [{ containerPort: 4318, hostPort: 4318, protocol }]
: []),
// TODO: Expose 8888 for collector telemetry
{ containerPort: 13133, hostPort: 13133, protocol },
];
}
createConfigWriterCommand(config) {
const configYaml = yaml.stringify(config);
const encodedConfig = node_buffer_1.Buffer.from(configYaml, 'utf8').toString('base64');
return [
'sh',
'-c',
[
'set -eu',
'tmp_config=$(mktemp /etc/otelcol-contrib/config.yaml.XXXXXX)',
`printf '%s' '${encodedConfig}' | base64 -d > "$tmp_config"`,
'mv "$tmp_config" /etc/otelcol-contrib/config.yaml',
].join('\n'),
];
}
createConfigContainer(config, volume) {
return {
name: 'otel-config-writer',
image: 'amazonlinux:latest',
essential: false,
command: config.apply(config => this.createConfigWriterCommand(config)),
mountPoints: [
{
sourceVolume: volume,
containerPath: '/etc/otelcol-contrib',
},
],
};
}
}
exports.OtelCollector = OtelCollector;