summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/metrics/metrics_engine.cpp
blob: 1ccb3956fc21450631884fa2d7d7a65bd4b0d7f4 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "metrics_engine.h"
#include "attribute_metrics.h"
#include "documentdb_tagged_metrics.h"
#include "content_proton_metrics.h"
#include <vespa/metrics/jsonwriter.h>
#include <vespa/metrics/metricmanager.h>

#include <vespa/log/log.h>
LOG_SETUP(".proton.server.metricsengine");

namespace proton {

MetricsEngine::MetricsEngine()
    : _root(std::make_unique<ContentProtonMetrics>()),
      _manager(std::make_unique<metrics::MetricManager>()),
      _metrics_producer(*_manager)
{ }

MetricsEngine::~MetricsEngine() = default;

void
MetricsEngine::start(const config::ConfigUri &)
{
    {
        metrics::MetricLockGuard guard(_manager->getMetricLock());
        _manager->registerMetric(guard, *_root);
    }

    // Storage doesnt snapshot unset metrics to save memory. Currently
    // feature seems a bit bugged. Disabling this optimalization for search.
    // Can enable it later when it is confirmed to be working well.
    _manager->snapshotUnsetMetrics(true);

    // Currently, when injecting a metric manager into the content layer,
    // the content layer require to be the one initializing and starting it.
    // Thus not calling init here, but further out in the application when
    // one knows whether we are running in row/column mode or not
}

void
MetricsEngine::addMetricsHook(metrics::UpdateHook &hook)
{
    _manager->addMetricUpdateHook(hook, 5);
}

void
MetricsEngine::removeMetricsHook(metrics::UpdateHook &hook)
{
    _manager->removeMetricUpdateHook(hook);
}

void
MetricsEngine::addExternalMetrics(metrics::Metric &child)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    _root->registerMetric(child);
}

void
MetricsEngine::removeExternalMetrics(metrics::Metric &child)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    _root->unregisterMetric(child);
}

void
MetricsEngine::addDocumentDBMetrics(DocumentDBTaggedMetrics &child)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    _root->registerMetric(child);
}

void
MetricsEngine::removeDocumentDBMetrics(DocumentDBTaggedMetrics &child)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    _root->unregisterMetric(child);
}

namespace {

void
doAddAttribute(AttributeMetrics &attributes, const std::string &attrName)
{
    auto entry = attributes.add(attrName);
    if (entry) {
        attributes.parent()->registerMetric(*entry);
    } else {
        LOG(warning, "Could not add metrics for attribute '%s', already existing", attrName.c_str());
    }
}

void
doRemoveAttribute(AttributeMetrics &attributes, const std::string &attrName)
{
    auto entry = attributes.remove(attrName);
    if (entry) {
        attributes.parent()->unregisterMetric(*entry);
    } else {
        LOG(warning, "Could not remove metrics for attribute '%s', not found", attrName.c_str());
    }
}

void
doCleanAttributes(AttributeMetrics &attributes)
{
    auto entries = attributes.release();
    for (const auto &entry : entries) {
        attributes.parent()->unregisterMetric(*entry);
    }
}

}

void
MetricsEngine::addAttribute(AttributeMetrics &subAttributes,
                            const std::string &name)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    doAddAttribute(subAttributes, name);
}

void
MetricsEngine::removeAttribute(AttributeMetrics &subAttributes,
                               const std::string &name)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    doRemoveAttribute(subAttributes, name);
}

void
MetricsEngine::cleanAttributes(AttributeMetrics &subAttributes)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    doCleanAttributes(subAttributes);
}

namespace {

template <typename MatchingMetricsType>
void
addRankProfileTo(MatchingMetricsType &matchingMetrics, const vespalib::string &name, size_t numDocIdPartitions)
{
    auto &entry = matchingMetrics.rank_profiles[name];
    if (entry.get()) {
        LOG(warning, "Two rank profiles have the same name: %s", name.c_str());
    } else {
        matchingMetrics.rank_profiles[name].reset(
                new typename MatchingMetricsType::RankProfileMetrics(name, numDocIdPartitions, &matchingMetrics));
    }
}

template <typename MatchingMetricsType>
void
cleanRankProfilesIn(MatchingMetricsType &matchingMetrics)
{
    typename MatchingMetricsType::RankProfileMap rankMetrics;
    matchingMetrics.rank_profiles.swap(rankMetrics);
    for (const auto &metric : rankMetrics) {
        matchingMetrics.unregisterMetric(*metric.second);
    }
}

}

void
MetricsEngine::addRankProfile(DocumentDBTaggedMetrics &owner, const std::string &name, size_t numDocIdPartitions)
{
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    size_t adjustedNumDocIdPartitions = std::min(numDocIdPartitions, owner.maxNumThreads);
    addRankProfileTo(owner.matching, name, adjustedNumDocIdPartitions);
}

void
MetricsEngine::cleanRankProfiles(DocumentDBTaggedMetrics &owner) {
    metrics::MetricLockGuard guard(_manager->getMetricLock());
    cleanRankProfilesIn(owner.matching);
}

void
MetricsEngine::stop()
{
    _manager->stop();
}

}