1/*
2 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16#include <aws/core/utils/memory/AWSMemory.h>
17#include <aws/core/monitoring/MonitoringInterface.h>
18#include <aws/core/monitoring/MonitoringFactory.h>
19#include <aws/core/monitoring/MonitoringManager.h>
20#include <aws/core/monitoring/DefaultMonitoring.h>
21#include <aws/core/Core_EXPORTS.h>
22
23#ifdef _MSC_VER
24#pragma warning(disable : 4592)
25#endif
26
27namespace Aws
28{
29 namespace Monitoring
30 {
31 typedef Aws::Vector<Aws::UniquePtr<MonitoringInterface>> Monitors;
32
33 const char MonitoringTag[] = "MonitoringAllocTag";
34
35 /**
36 * Global factory to create global metrics instance.
37 */
38 static Aws::UniquePtr<Monitors> s_monitors;
39
40 Aws::Vector<void*> OnRequestStarted(const Aws::String& serviceName, const Aws::String& requestName, const std::shared_ptr<const Aws::Http::HttpRequest>& request)
41 {
42 assert(s_monitors);
43 Aws::Vector<void*> contexts;
44 contexts.reserve(s_monitors->size());
45 for (const auto& interface: *s_monitors)
46 {
47 contexts.emplace_back(interface->OnRequestStarted(serviceName, requestName, request));
48 }
49 return contexts;
50 }
51
52 void OnRequestSucceeded(const Aws::String& serviceName, const Aws::String& requestName, const std::shared_ptr<const Aws::Http::HttpRequest>& request,
53 const Aws::Client::HttpResponseOutcome& outcome, const CoreMetricsCollection& metricsFromCore, const Aws::Vector<void*>& contexts)
54 {
55 assert(s_monitors);
56 assert(contexts.size() == s_monitors->size());
57 size_t index = 0;
58 for (const auto& interface: *s_monitors)
59 {
60 interface->OnRequestSucceeded(serviceName, requestName, request, outcome, metricsFromCore, contexts[index++]);
61 }
62 }
63
64 void OnRequestFailed(const Aws::String& serviceName, const Aws::String& requestName, const std::shared_ptr<const Aws::Http::HttpRequest>& request,
65 const Aws::Client::HttpResponseOutcome& outcome, const CoreMetricsCollection& metricsFromCore, const Aws::Vector<void*>& contexts)
66 {
67 assert(s_monitors);
68 assert(contexts.size() == s_monitors->size());
69 size_t index = 0;
70 for (const auto& interface: *s_monitors)
71 {
72 interface->OnRequestFailed(serviceName, requestName, request, outcome, metricsFromCore, contexts[index++]);
73 }
74 }
75
76 void OnRequestRetry(const Aws::String& serviceName, const Aws::String& requestName,
77 const std::shared_ptr<const Aws::Http::HttpRequest>& request, const Aws::Vector<void*>& contexts)
78 {
79 assert(s_monitors);
80 assert(contexts.size() == s_monitors->size());
81 size_t index = 0;
82 for (const auto& interface: *s_monitors)
83 {
84 interface->OnRequestRetry(serviceName, requestName, request, contexts[index++]);
85 }
86 }
87
88 void OnFinish(const Aws::String& serviceName, const Aws::String& requestName,
89 const std::shared_ptr<const Aws::Http::HttpRequest>& request, const Aws::Vector<void*>& contexts)
90 {
91 assert(s_monitors);
92 assert(contexts.size() == s_monitors->size());
93 size_t index = 0;
94 for (const auto& interface: *s_monitors)
95 {
96 interface->OnFinish(serviceName, requestName, request, contexts[index++]);
97 }
98 }
99
100 void InitMonitoring(const std::vector<MonitoringFactoryCreateFunction>& monitoringFactoryCreateFunctions)
101 {
102 if (s_monitors)
103 {
104 return;
105 }
106 s_monitors = Aws::MakeUnique<Monitors>(MonitoringTag);
107 for (const auto& function: monitoringFactoryCreateFunctions)
108 {
109 auto factory = function();
110 if (factory)
111 {
112 auto instance = factory->CreateMonitoringInstance();
113 if (instance)
114 {
115 s_monitors->emplace_back(std::move(instance));
116 }
117 }
118 }
119
120 auto defaultMonitoringFactory = Aws::MakeShared<DefaultMonitoringFactory>(MonitoringTag);
121 auto instance = defaultMonitoringFactory->CreateMonitoringInstance();
122 if (instance)
123 {
124 s_monitors->emplace_back(std::move(instance));
125 }
126 }
127
128 void CleanupMonitoring()
129 {
130 if (!s_monitors)
131 {
132 return;
133 }
134
135 s_monitors = nullptr;
136 }
137 } // namepsace Monitoring
138
139} // namespace Aws
140