summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/MetricsAdminI.cpp
blob: a4b502bebbb841c1673b3f785cc9bdcefc351960 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <Ice/MetricsAdminI.h>

#include <Ice/ObserverI.h>
#include <Ice/Properties.h>
#include <Ice/Communicator.h>
#include <Ice/Instance.h>

#include <IceUtil/StringUtil.h>

using namespace std;
using namespace Ice;
using namespace IceMX;

namespace 
{

NameValueDict
parseRule(const ::Ice::PropertiesPtr& properties, const string& name)
{
    NameValueDict dict;
    PropertyDict rules = properties->getPropertiesForPrefix(name + '.');
    for(PropertyDict::const_iterator p = rules.begin(); p != rules.end(); ++p)
    {
        dict.insert(make_pair(p->first.substr(name.size() + 1), p->second));
    }
    return dict;
}

bool
match(const string& value, const string& expr)
{
    return true; // TODO
}

}

MetricsMapI::MetricsMapI(const string& groupBy, int retain, const NameValueDict& accept, const NameValueDict& reject) : 
    _retain(retain), _accept(accept), _reject(reject)
{
    if(!groupBy.empty())
    {
        string v;
        bool attribute = IceUtilInternal::isAlpha(groupBy[0]) || IceUtilInternal::isDigit(groupBy[0]);
        if(!attribute)
        {
            _groupByAttributes.push_back("");
        }
        
        for(string::const_iterator p = groupBy.begin(); p != groupBy.end(); ++p)
        {
            bool isAlphaNum = IceUtilInternal::isAlpha(*p) || IceUtilInternal::isDigit(*p);
            if(attribute && !isAlphaNum)
            {
                _groupByAttributes.push_back(v);
                v = *p;
                attribute = false;
            }
            else if(!attribute && isAlphaNum)
            {
                _groupBySeparators.push_back(v);
                v = *p;
                attribute = true;
            }
            else
            {
                v += *p;
            }
        }

        if(attribute)
        {
            _groupByAttributes.push_back(v);
        }
        else
        {
            _groupBySeparators.push_back(v);
        }
    }
}

MetricsMap
MetricsMapI::getMetrics()
{
    MetricsMap objects;

    Lock sync(*this);
    for(map<string, EntryPtr>::const_iterator p = _objects.begin(); p != _objects.end(); ++p)
    {
        objects.push_back(p->second->clone());
    }
    return objects;
}

MetricsFailuresSeq
MetricsMapI::getFailures()
{
    MetricsFailuresSeq failures;

    Lock sync(*this);
    for(map<string, EntryPtr>::const_iterator p = _objects.begin(); p != _objects.end(); ++p)
    {
        MetricsFailures f = p->second->getFailures();
        if(!f.failures.empty())
        {
            failures.push_back(f);
        }
    }
    return failures;
}

MetricsMapI::EntryPtr
MetricsMapI::getMatching(const MetricsHelper& helper)
{
    for(map<string, string>::const_iterator p = _accept.begin(); p != _accept.end(); ++p)
    {
        if(!match(helper(p->first), p->second))
        {
            return 0;
        }
    }
    
    for(map<string, string>::const_iterator p = _reject.begin(); p != _reject.end(); ++p)
    {
        if(match(helper(p->first), p->second))
        {
            return 0;
        }
    }
    
    ostringstream os;
    vector<string>::const_iterator q = _groupBySeparators.begin();
    for(vector<string>::const_iterator p = _groupByAttributes.begin(); p != _groupByAttributes.end(); ++p)
    {
        os << helper(*p);
        if(q != _groupBySeparators.end())
        {
            os << *q++;
        }
    }

    string key = os.str();

    Lock sync(*this);
    map<string, EntryPtr>::const_iterator p = _objects.find(key);
    if(p == _objects.end())
    {
        p = _objects.insert(make_pair(key, newEntry(this, helper.newMetrics(key)))).first;
    }
    return p->second;
}

void
MetricsMapI::detached(Entry* entry)
{
    Lock sync(*this);
    if(_retain == 0)
    {
        return;
    }

    assert(_detachedQueue.size() <= _retain);

    deque<Entry*>::iterator p = _detachedQueue.begin();
    while(p != _detachedQueue.end())
    {
        if(*p == entry)
        {
            _detachedQueue.erase(p);
            break;
        }
        else if(!(*p)->isDetached())
        {
            p = _detachedQueue.erase(p);
        }
        else
        {
            ++p;
        }
    }

    if(_detachedQueue.size() == _retain)
    {
        // Remove oldest entry if there's still no room
        _objects.erase(_detachedQueue.front()->id());
        _detachedQueue.pop_front();
    }

    _detachedQueue.push_back(entry);
}
    
MetricsViewI::MetricsViewI(bool enabled) : _enabled(enabled)
{
}

void
MetricsViewI::add(const string& name, const MetricsMapIPtr& map)
{
    _maps.insert(make_pair(name, map));
}

void
MetricsViewI::remove(const string& cl)
{
    _maps.erase(cl);
}

MetricsView
MetricsViewI::getMetrics()
{
    MetricsView metrics;
    if(_enabled)
    {
        for(map<string, MetricsMapIPtr>::const_iterator p = _maps.begin(); p != _maps.end(); ++p)
        {
            metrics.insert(make_pair(p->first, p->second->getMetrics()));
        }
    }
    return metrics;
}

MetricsFailuresSeq
MetricsViewI::getFailures(const string& mapName)
{
    map<string, MetricsMapIPtr>::const_iterator p = _maps.find(mapName);
    if(p != _maps.end())
    {
        return p->second->getFailures();
    }
    return MetricsFailuresSeq();
}

MetricsMapI::EntryPtr
MetricsViewI::getMatching(const MetricsHelper& helper) const
{
    if(_enabled)
    {
        map<string, MetricsMapIPtr>::const_iterator p = _maps.find(helper.getMapName());
        if(p != _maps.end())
        {
            return p->second->getMatching(helper);
        }
    }
    return 0;
}

vector<string>
MetricsViewI::getMaps() const
{
    vector<string> maps;
    for(map<string, MetricsMapIPtr>::const_iterator p = _maps.begin(); p != _maps.end(); ++p)
    {
        maps.push_back(p->first);
    }
    return maps;
}

MetricsAdminI::MetricsAdminI(const Ice::PropertiesPtr& properties) : _properties(properties)
{
}

void
MetricsAdminI::addUpdater(const string& mapName, const UpdaterPtr& updater)
{
    _updaters.insert(make_pair(mapName, updater));
}

void
MetricsAdminI::addFactory(const string& mapName, const MetricsMapFactoryPtr& factory)
{
    _factories[mapName] = factory;

    //
    // Add maps to views configured with the given map name.
    //
    const string viewsPrefix = "IceMX.MetricsView.";
    PropertyDict views = _properties->getPropertiesForPrefix(viewsPrefix);
    for(PropertyDict::const_iterator p = views.begin(); p != views.end(); ++p)
    {
        string viewName = p->first.substr(viewsPrefix.size());
        string::size_type dotPos = viewName.find('.');
        if(dotPos != string::npos)
        {
            viewName = viewName.substr(0, dotPos);
        }
        
        map<string, MetricsViewIPtr>::const_iterator q = _views.find(viewName);
        if(q == _views.end())
        {
            bool enabled = _properties->getPropertyAsIntWithDefault(viewsPrefix + viewName, 1) > 0;
            q = _views.insert(make_pair(viewName, new MetricsViewI(enabled))).first;
        }
        MetricsViewIPtr view = q->second;
        
        const string mapsPrefix = viewsPrefix + viewName + ".Map.";
        string mapPrefix = mapsPrefix + mapName;
        if(_properties->getPropertiesForPrefix(mapPrefix).empty() && 
           _properties->getPropertiesForPrefix(mapsPrefix).empty())
        {
            mapPrefix = viewsPrefix + viewName;
        }
        else
        {
            continue; // This map isn't configured for this view.
        }

        string groupBy = _properties->getProperty(mapPrefix + ".GroupBy");
        int retain = _properties->getPropertyAsIntWithDefault(mapPrefix + ".RetainDetached", 10);
        NameValueDict accept = parseRule(_properties, mapPrefix + ".Accept");
        NameValueDict reject = parseRule(_properties, mapPrefix + ".Reject");

        view->add(mapName, factory->create(groupBy, retain, accept, reject));
    }
}

vector<MetricsMapI::EntryPtr>
MetricsAdminI::getMatching(const MetricsHelper& helper) const
{
    Lock sync(*this);
    vector<MetricsMapI::EntryPtr> objects;
    for(map<string, MetricsViewIPtr>::const_iterator p = _views.begin(); p != _views.end(); ++p)
    {
        MetricsMapI::EntryPtr e = p->second->getMatching(helper);
        if(e)
        {
            objects.push_back(e);
        }
    }
    sort(objects.begin(), objects.end());
    return objects;
}

Ice::StringSeq
MetricsAdminI::getMetricsViewNames(const ::Ice::Current&)
{
    Ice::StringSeq names;

    Lock sync(*this);
    for(map<string, MetricsViewIPtr>::const_iterator p = _views.begin(); p != _views.end(); ++p)
    {
        names.push_back(p->first);
    }
    return names;
}

MetricsView
MetricsAdminI::getMetricsView(const string& view, const ::Ice::Current&)
{
    Lock sync(*this);
    std::map<string, MetricsViewIPtr>::const_iterator p = _views.find(view);
    if(p == _views.end())
    {
        throw UnknownMetricsView();
    }
    return p->second->getMetrics();
}

MetricsFailuresSeq
MetricsAdminI::getMetricsFailures(const string& view, const string& map, const ::Ice::Current&)
{
    Lock sync(*this);
    std::map<string, MetricsViewIPtr>::const_iterator p = _views.find(view);
    if(p == _views.end())
    {
        throw UnknownMetricsView();
    }
    return p->second->getFailures(map);
}

void
MetricsAdminI::addMapToView(const string& view, 
                            const string& mapName, 
                            const string& groupBy,
                            int retain,
                            const NameValueDict& accept, 
                            const NameValueDict& reject,
                            const ::Ice::Current&)
{
    UpdaterPtr updater;
    {
        Lock sync(*this);
        map<string, MetricsViewIPtr>::const_iterator p = _views.find(view);
        if(p == _views.end())
        {
            p = _views.insert(make_pair(view, new MetricsViewI(true))).first;
        }
        p->second->add(mapName, _factories[mapName]->create(groupBy, retain, accept, reject));

        map<string, UpdaterPtr>::const_iterator q = _updaters.find(mapName);
        if(q != _updaters.end())
        {
            updater = q->second;
        }
    }
    if(updater)
    {
        updater->update();
    }
}

void
MetricsAdminI::removeMapFromView(const string& view, const string& mapName, const ::Ice::Current&)
{
    UpdaterPtr updater;
    {
        Lock sync(*this);
        map<string, MetricsViewIPtr>::const_iterator p = _views.find(view);
        if(p == _views.end())
        {
            throw UnknownMetricsView();
        }
        p->second->remove(mapName);

        map<string, UpdaterPtr>::const_iterator q = _updaters.find(mapName);
        if(q != _updaters.end())
        {
            updater = q->second;
        }
    }
    if(updater)
    {
        updater->update();
    }
}

void
MetricsAdminI::setViewEnabled(const string& view, bool enabled)
{
    vector<string> maps;
    {
        Lock sync(*this);
        map<string, MetricsViewIPtr>::const_iterator p = _views.find(view);
        if(p == _views.end())
        {
            throw UnknownMetricsView();
        }
        p->second->setEnabled(enabled);
        maps = p->second->getMaps();
    }
    for(vector<string>::const_iterator p = maps.begin(); p != maps.end(); ++p)
    {
        _updaters[*p]->update();
    }
}