summaryrefslogtreecommitdiffstats
path: root/logforwarder/src/apps/vespa-otelcol-start/wrapper.cpp
blob: 7228a3ea921d2ad134bfd98226b79193e84530d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "wrapper.h"
#include "child-handler.h"

#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <vespa/defaults.h>
#include <vespa/log/log.h>
LOG_SETUP(".wrapper");

namespace {

vespalib::string fixDir(const vespalib::string &parent, const vespalib::string &subdir) {
    auto dirname = parent + "/" + subdir;
    DIR *dp = opendir(dirname.c_str());
    if (dp == NULL) {
        if (errno != ENOENT || mkdir(dirname.c_str(), 0755) != 0) {
            LOG(warning, "Could not create directory '%s'", dirname.c_str());
            perror(dirname.c_str());
        }
    } else {
        closedir(dp);
    }
    return dirname;
}

vespalib::string cfFilePath() {
    vespalib::string path = vespa::Defaults::underVespaHome("var/db/vespa");
    path = fixDir(path, "otelcol");
    return path + "/" + "config.yaml";
}

void  writeConfig(const vespalib::string &config, const vespalib::string &path) {
    LOG(info, "got config, writing %s", path.c_str());
    vespalib::string tmpPath = path + ".new";
    FILE *fp = fopen(tmpPath.c_str(), "w");
    if (fp == NULL) {
        LOG(warning, "could not open '%s' for write", tmpPath.c_str());
        return;
    }
    fprintf(fp, "%s\n", config.c_str());
    fclose(fp);
    rename(tmpPath.c_str(), path.c_str());
}

} // namespace <unnamed>

Wrapper::Wrapper(const std::string &configId)
  : CfHandler(configId),
    _childHandler()
{}

Wrapper::~Wrapper() = default;

void Wrapper::stop() {
    _childHandler.stopChild();
}

void Wrapper::check() {
    checkConfig();
    if (_childHandler.checkChild()) {
        LOG(error, "Fatal: child process died unexpectedly");
        std::_Exit(EXIT_FAILURE);
    }
}

void Wrapper::gotConfig(const OpenTelemetryConfig& config) {
    _childHandler.stopChild();
    std::string progPath = vespa::Defaults::underVespaHome("sbin/otelcol-contrib");
    std::string cfPath = cfFilePath();
    writeConfig(config.yaml, cfPath);
    _childHandler.startChild(progPath, cfPath);
}