1package demo;
2
3import com.amberflo.metering.ingest.MeteringContext;
4import com.amberflo.metering.ingest.extensions.ServiceMetering;
5import com.amberflo.metering.ingest.meter_message.Domain;
6import com.amberflo.metering.ingest.meter_message.MeterMessage;
7import com.amberflo.metering.ingest.meter_message.MeterMessageBuilder;
8
9import java.time.LocalDateTime;
10import java.time.ZoneId;
11import java.util.HashMap;
12import java.util.Map;
13
14import com.amberflo.metering.ingest.meter_message.Region;
15
16import static com.amberflo.metering.ingest.MeteringContext.metering;
17import static com.amberflo.metering.ingest.extensions.ServiceMetering.serviceMetering;
18import static com.amberflo.metering.ingest.extensions.CustomerMetering.customerMetering;
19
20
30public class MeteringExamples {
31 private final static String METER_NAME = "TrancsactionCount";
32 private final static int METER_VALUE = 3;
33 private final static String CUSTOMER_ID = "YWJjNDU2";
34 private final static String SERVICE_CALL = "process-request";
35 private final static String SERVICE_NAME = "magic-service";
36 private final static LocalDateTime EVENT_TIME = LocalDateTime.now(ZoneOffset.UTC);
37 private final static Double MB_USED = 15.5;
38 private final static double PROCESSING_TIME_MILLIS = 100;
39
40 private final static boolean ERROR_FLAG = true;
41
42 private final static Map<String, String> EXTRA_DIMENSIONS_MAP = null;
43
44
45 public static void main(String[] args) throws Exception {
46
47
48 System.setProperty(MeteringContext.METERING_DOMAIN, Domain.Prod.name());
49
50
51 sendSomeMeters();
52
53
54
55
56
57 MeteringContext.flushAndClose();
58
59
60 try (final MeteringContext context = MeteringContext.getContext()) {
61
62 customerMetering().signUp(CUSTOMER_ID, EVENT_TIME);
63 }
64 }
65
66
72 private static void sendSomeMeters() {
73
74
75
76
77 final MeterMessage meter = MeterMessageBuilder
78 .createInstance(METER_NAME, EVENT_TIME, CUSTOMER_ID)
79 .setMeterValue(METER_VALUE)
80 .build();
81 metering().meter(meter);
82
83
84 customBuilderAdvanced();
85
86
87
88
89
90
91
92
93
94
95
96
97
98 metering().meter(CUSTOMER_ID, METER_NAME, METER_VALUE, EVENT_TIME, EXTRA_DIMENSIONS_MAP);
99 metering().meter(CUSTOMER_ID, METER_NAME, METER_VALUE, EVENT_TIME, null);
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 serviceMetering().call(CUSTOMER_ID, SERVICE_CALL, EVENT_TIME);
115 serviceMetering().processingTime(CUSTOMER_ID, SERVICE_CALL, PROCESSING_TIME_MILLIS, EVENT_TIME);
116 serviceMetering().dataUsage(CUSTOMER_ID, SERVICE_CALL, MB_USED, EVENT_TIME);
117
118 serviceMeteringAdvanced();
119
120
121
122 customerMetering().signUp(CUSTOMER_ID, EVENT_TIME);
123 customerMetering().onboarded(CUSTOMER_ID, EVENT_TIME);
124 customerMetering().offboarded(CUSTOMER_ID, EVENT_TIME);
125 customerMetering().login(CUSTOMER_ID, EVENT_TIME);
126
127
128 customerMetering().onboardingRejected(CUSTOMER_ID, EVENT_TIME);
129 }
130
131 private static void customBuilderAdvanced() {
132 final Map<String, String> sessionInfo = new HashMap<>();
133 sessionInfo.put("session", "789");
134
135 final Map<String, String> countryAndStateInfo = new HashMap<>();
136 sessionInfo.put("country", "US");
137 sessionInfo.put("state", "WA");
138
139 final MeterMessage meter = MeterMessageBuilder
140 .createInstance(METER_NAME, EVENT_TIME, CUSTOMER_ID)
141
142 .setMeterValue(METER_VALUE)
143
144
145
146 .captureEndTimeAndDuration()
147
148
149 .setMeterType("millis")
150
151
152 .setServiceName(SERVICE_NAME)
153 .setServiceCall(SERVICE_CALL)
154
155 .asError() .asError(IllegalArgumentException.class)
156
157 .setRegion(Region.US_West)
158
159
160
161
162 .setDimensionsMap(sessionInfo)
163
164
165
166 .setDimensionsMap(countryAndStateInfo)
167
168
169 .build();
170 metering().meter(meter);
171 }
172
173
191 private static void serviceMeteringAdvanced() {
192
193 serviceMetering().call(CUSTOMER_ID, SERVICE_CALL, EVENT_TIME);
194 serviceMetering().call(CUSTOMER_ID, SERVICE_CALL, ERROR_FLAG, EVENT_TIME);
195 serviceMetering().call(CUSTOMER_ID, SERVICE_CALL, ERROR_FLAG, IllegalAccessError.class, EVENT_TIME);
196
197
198
199 serviceMetering().callCompleted(CUSTOMER_ID, SERVICE_CALL, EVENT_TIME);
200 serviceMetering().callError(CUSTOMER_ID, SERVICE_CALL, EVENT_TIME);
201
202
203
204
205
206 serviceMetering().callStarted(CUSTOMER_ID, SERVICE_CALL, EVENT_TIME);
207
208
209 serviceMetering().processingTime(CUSTOMER_ID, SERVICE_CALL, PROCESSING_TIME_MILLIS, EVENT_TIME);
210
211
212
213 serviceMetering().dataUsage(CUSTOMER_ID, SERVICE_CALL, MB_USED, EVENT_TIME);
214
215
216
217 final Runnable methodToRun = () -> {
218 try {
219 Thread.sleep(300);
220 } catch (InterruptedException e) {
221 e.printStackTrace();
222 }
223 };
224 serviceMeteringMultiCalls(methodToRun);
225 }
226
227
230 private static void serviceMeteringMultiCalls(final Runnable runnable) {
231 final LocalDateTime startTime = LocalDateTime.now(ZoneOffset.UTC);
232 LocalDateTime endTime = startTime;
233 try {
234 serviceMetering().callStarted(CUSTOMER_ID, SERVICE_CALL, startTime);
235
236 runnable.run();
237
238 endTime = LocalDateTime.now(ZoneOffset.UTC);
239 serviceMetering().callCompleted(CUSTOMER_ID, SERVICE_CALL, endTime);
240 } catch (final Exception e) {
241 endTime = LocalDateTime.now(ZoneOffset.UTC);
242 serviceMetering().callError(CUSTOMER_ID, SERVICE_CALL, e.getClass(), endTime);
243 } finally {
244 final long durationInMillis =
245 endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() -
246 startTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
247
248 serviceMetering().processingTime(CUSTOMER_ID, SERVICE_CALL, durationInMillis, endTime);
249 }
250 }
251}