-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bogdan Cristian Drutu <[email protected]>
- Loading branch information
1 parent
2dd7612
commit f912b33
Showing
11 changed files
with
207 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
sdk/src/main/java/io/opentelemetry/sdk/metrics/BoundRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics; | ||
|
||
import io.opentelemetry.metrics.LabelSet; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
abstract class BoundRegistry<B extends AbstractBoundInstrument> { | ||
private final ConcurrentHashMap<LabelSet, B> bounds; | ||
private final ReentrantLock collectLock; | ||
|
||
BoundRegistry() { | ||
bounds = new ConcurrentHashMap<>(); | ||
collectLock = new ReentrantLock(); | ||
} | ||
|
||
B acquire(LabelSet labelSet) { | ||
B bound = bounds.get(labelSet); | ||
if (bound != null && bound.ref()) { | ||
// At this moment it is guaranteed that the Bound is in the map and will not be removed. | ||
return bound; | ||
} | ||
|
||
// Missing entry or no longer mapped, try to add a new entry. | ||
bound = newBound(labelSet); | ||
while (true) { | ||
B oldBound = bounds.putIfAbsent(labelSet, bound); | ||
if (oldBound != null) { | ||
if (oldBound.ref()) { | ||
// At this moment it is guaranteed that the Bound is in the map and will not be removed. | ||
return oldBound; | ||
} | ||
// Try to remove the oldBound. This will race with the collect method, but only one will | ||
// succeed. | ||
bounds.remove(labelSet, oldBound); | ||
continue; | ||
} | ||
return bound; | ||
} | ||
} | ||
|
||
void release(B bound) { | ||
bound.unref(); | ||
} | ||
|
||
/** | ||
* Collects records from all the entries (labelSet, Bound) that changed since the last collect() | ||
* call. | ||
* | ||
* <p>It is possible that some entries | ||
*/ | ||
void collect(RecordProcessor recordProcessor) { | ||
collectLock.lock(); | ||
try { | ||
for (Map.Entry<LabelSet, B> entry : bounds.entrySet()) { | ||
if (entry.getValue().tryUnmap()) { | ||
// If able to unmap then remove the record from the current Map. This can race with the | ||
// acquire but because we requested a specific value only one will succeed. | ||
bounds.remove(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
entry.getValue().checkpoint(); | ||
} | ||
} finally { | ||
collectLock.unlock(); | ||
} | ||
} | ||
|
||
abstract B newBound(LabelSet labelSet); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
sdk/src/main/java/io/opentelemetry/sdk/metrics/RecordProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics; | ||
|
||
import io.opentelemetry.metrics.LabelSet; | ||
|
||
public interface RecordProcessor { | ||
void process(LabelSet labelSet, Aggregator<?> aggregator); | ||
} |