2 * Copyright © 2016-2018 European Support Limited
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 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.api;
20 * Builder to populate <i>metrics</i> data. This includes only data known to an application, and not otherwise available
21 * to the logging framework.
26 public class MetricsData {
28 // don't inherit from AuditData because it has a very different meaning
30 private final long startTime;
31 private final long endTime;
32 private final StatusCode statusCode;
33 private final String responseCode;
34 private final String responseDescription;
35 private final String clientIpAddress;
36 private final String targetVirtualEntity;
37 private final String targetEntity;
39 private MetricsData(final MetricsDataBuilder builder) {
40 this.startTime = builder.startTime;
41 this.endTime = builder.endTime;
42 this.statusCode = builder.statusCode;
43 this.responseCode = builder.responseCode;
44 this.responseDescription = builder.responseDescription;
45 this.clientIpAddress = builder.clientIpAddress;
46 this.targetEntity = builder.targetEntity;
47 this.targetVirtualEntity = builder.targetVirtualEntity;
51 * Begin timestamp of an API invocation.
55 public long getStartTime() {
60 * End timestamp of an API invocation.
64 public long getEndTime() {
69 * Result status of an API invocation.
71 * @return protocol and application agnostic status code
73 public StatusCode getStatusCode() {
78 * Application/protocol specific response status of an API invocation.
80 * @return response code
82 public String getResponseCode() {
87 * Application/protocol specific response in a human-friendly way.
89 * @return human-friendly response description
91 public String getResponseDescription() {
92 return responseDescription;
96 * IP address of the invoking client when available.
100 public String getClientIpAddress() {
101 return clientIpAddress;
105 * External entity invoked by the local system.
107 * @return identifier of an external entity (system, component, sub-component)
109 public String getTargetEntity() {
114 * External API invoked by the local system.
116 * @return name of an external API
118 public String getTargetVirtualEntity() {
119 return targetVirtualEntity;
123 public String toString() {
124 return "AuditData{startTime=" + startTime + ", endTime=" + endTime + ", statusCode=" + statusCode
125 + ", responseCode=" + responseCode + ", responseDescription=" + responseDescription
126 + ", clientIpAddress=" + clientIpAddress + '}';
129 public static MetricsDataBuilder builder() {
130 return new MetricsDataBuilder();
134 * Fluent API for building metrics data.
136 public static class MetricsDataBuilder {
138 private long startTime;
139 private long endTime;
140 private StatusCode statusCode;
141 private String responseCode;
142 private String responseDescription;
143 private String clientIpAddress;
144 private String targetEntity;
145 private String targetVirtualEntity;
147 MetricsDataBuilder() { /* package-private default constructor to hide the public one */ }
150 * Begin timestamp of an activity being audited.
152 * @param startTime local timestamp, usually received from {@link System#currentTimeMillis()}
153 * @return this builder for fluent API
155 public MetricsDataBuilder startTime(final long startTime) {
156 this.startTime = startTime;
161 * End timestamp of an activity being audited.
163 * @param endTime local timestamp, usually received from {@link System#currentTimeMillis()}
164 * @return this builder for fluent API
166 public MetricsDataBuilder endTime(final long endTime) {
167 this.endTime = endTime;
172 * Indicate whether an invocation was successful. It is up the the application to decide if a particular result
173 * must be treated as a success or a failure.
175 * @param statusCode invocation status success/failure
176 * @return this builder for fluent API
178 public MetricsDataBuilder statusCode(final StatusCode statusCode) {
179 this.statusCode = statusCode;
184 * Application/protocol specific response code. For a Web API, it is likely a standard HTTP response code.
186 * @param responseCode response code that depends on application and invocation protocol
187 * @return this builder for fluent API
189 public MetricsDataBuilder responseCode(final String responseCode) {
190 this.responseCode = responseCode;
195 * Response description that explains {@link #responseCode(String)} in a human-friendly way. For a Web API, it
196 * is likely to be a standard HTTP response phrase.
198 * @param responseDescription human-friendly response description
199 * @return this builder for fluent API
201 public MetricsDataBuilder responseDescription(final String responseDescription) {
202 this.responseDescription = responseDescription;
207 * IP address of an invoking client.
209 * @param clientIpAddress IP address
210 * @return this builder for fluent API
212 public MetricsDataBuilder clientIpAddress(final String clientIpAddress) {
213 this.clientIpAddress = clientIpAddress;
218 * External entity at which the operation is invoked.
220 * @param targetEntity external entity identifier
221 * @return this builder for fluent API
223 public MetricsDataBuilder targetEntity(String targetEntity) {
224 this.targetEntity = targetEntity;
229 * Name of the API or operation activities invoked at the external entity.
231 * @param targetVirtualEntity invoked external API
232 * @return this builder for fluent API
234 public MetricsDataBuilder targetVirtualEntity(String targetVirtualEntity) {
235 this.targetVirtualEntity = targetVirtualEntity;
240 * Create an instance of {@link MetricsData}.
242 * @return a populated instance of audit data
244 public MetricsData build() {
245 return new MetricsData(this);