18
18
19
19
import java .time .Instant ;
20
20
import java .util .concurrent .CompletableFuture ;
21
+ import java .util .concurrent .locks .ReentrantReadWriteLock ;
21
22
import lombok .extern .slf4j .Slf4j ;
22
23
import software .amazon .cloudwatchlogs .emf .environment .Environment ;
23
24
import software .amazon .cloudwatchlogs .emf .environment .EnvironmentProvider ;
@@ -35,6 +36,7 @@ public class MetricsLogger {
35
36
private MetricsContext context ;
36
37
private CompletableFuture <Environment > environmentFuture ;
37
38
private EnvironmentProvider environmentProvider ;
39
+ private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock ();
38
40
39
41
public MetricsLogger () {
40
42
this (new EnvironmentProvider ());
@@ -67,10 +69,16 @@ public void flush() {
67
69
log .info ("Failed to resolve environment. Fallback to default environment: " , ex );
68
70
environment = environmentProvider .getDefaultEnvironment ();
69
71
}
70
- ISink sink = environment .getSink ();
71
- configureContextForEnvironment (context , environment );
72
- sink .accept (context );
73
- context = context .createCopyWithContext ();
72
+
73
+ rwl .writeLock ().lock ();
74
+ try {
75
+ ISink sink = environment .getSink ();
76
+ configureContextForEnvironment (context , environment );
77
+ sink .accept (context );
78
+ context = context .createCopyWithContext ();
79
+ } finally {
80
+ rwl .writeLock ().unlock ();
81
+ }
74
82
}
75
83
76
84
/**
@@ -83,8 +91,13 @@ public void flush() {
83
91
* @return the current logger
84
92
*/
85
93
public MetricsLogger putProperty (String key , Object value ) {
86
- this .context .putProperty (key , value );
87
- return this ;
94
+ rwl .readLock ().lock ();
95
+ try {
96
+ this .context .putProperty (key , value );
97
+ return this ;
98
+ } finally {
99
+ rwl .readLock ().unlock ();
100
+ }
88
101
}
89
102
90
103
/**
@@ -99,8 +112,13 @@ public MetricsLogger putProperty(String key, Object value) {
99
112
* @return the current logger
100
113
*/
101
114
public MetricsLogger putDimensions (DimensionSet dimensions ) {
102
- context .putDimension (dimensions );
103
- return this ;
115
+ rwl .readLock ().lock ();
116
+ try {
117
+ context .putDimension (dimensions );
118
+ return this ;
119
+ } finally {
120
+ rwl .readLock ().unlock ();
121
+ }
104
122
}
105
123
106
124
/**
@@ -113,8 +131,13 @@ public MetricsLogger putDimensions(DimensionSet dimensions) {
113
131
* @return the current logger
114
132
*/
115
133
public MetricsLogger setDimensions (DimensionSet ... dimensionSets ) {
116
- context .setDimensions (dimensionSets );
117
- return this ;
134
+ rwl .readLock ().lock ();
135
+ try {
136
+ context .setDimensions (dimensionSets );
137
+ return this ;
138
+ } finally {
139
+ rwl .readLock ().unlock ();
140
+ }
118
141
}
119
142
120
143
/**
@@ -128,8 +151,13 @@ public MetricsLogger setDimensions(DimensionSet... dimensionSets) {
128
151
* @return the current logger
129
152
*/
130
153
public MetricsLogger putMetric (String key , double value , Unit unit ) {
131
- this .context .putMetric (key , value , unit );
132
- return this ;
154
+ rwl .readLock ().lock ();
155
+ try {
156
+ this .context .putMetric (key , value , unit );
157
+ return this ;
158
+ } finally {
159
+ rwl .readLock ().unlock ();
160
+ }
133
161
}
134
162
135
163
/**
@@ -142,7 +170,7 @@ public MetricsLogger putMetric(String key, double value, Unit unit) {
142
170
* @return the current logger
143
171
*/
144
172
public MetricsLogger putMetric (String key , double value ) {
145
- this .context . putMetric (key , value , Unit .NONE );
173
+ this .putMetric (key , value , Unit .NONE );
146
174
return this ;
147
175
}
148
176
@@ -157,8 +185,13 @@ public MetricsLogger putMetric(String key, double value) {
157
185
* @return the current logger
158
186
*/
159
187
public MetricsLogger putMetadata (String key , Object value ) {
160
- this .context .putMetadata (key , value );
161
- return this ;
188
+ rwl .readLock ().lock ();
189
+ try {
190
+ this .context .putMetadata (key , value );
191
+ return this ;
192
+ } finally {
193
+ rwl .readLock ().unlock ();
194
+ }
162
195
}
163
196
164
197
/**
0 commit comments