|
| 1 | +/** |
| 2 | + * Copyright 2020 VMware, Inc. |
| 3 | + * <p> |
| 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 |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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. |
| 15 | + */ |
| 16 | +package io.micrometer.samples.hazelcast3; |
| 17 | + |
| 18 | +import com.hazelcast.core.Hazelcast; |
| 19 | +import com.hazelcast.core.HazelcastInstance; |
| 20 | +import com.hazelcast.core.IMap; |
| 21 | +import com.hazelcast.monitor.LocalMapStats; |
| 22 | +import com.hazelcast.monitor.NearCacheStats; |
| 23 | +import io.micrometer.core.instrument.*; |
| 24 | +import io.micrometer.core.instrument.binder.cache.HazelcastCacheMetrics; |
| 25 | +import io.micrometer.core.instrument.search.RequiredSearch; |
| 26 | +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Disabled; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import java.util.concurrent.TimeUnit; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Copied from micrometer-core test HazelcastCacheMetricsTest here to ensure cache metrics work with Hazelcast 3. |
| 38 | + */ |
| 39 | +class Hazelcast3CacheMetricsTest { |
| 40 | + |
| 41 | + HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(); |
| 42 | + IMap<String, String> cache = hazelcastInstance.getMap("mycache"); |
| 43 | + HazelcastCacheMetrics metrics = new HazelcastCacheMetrics(cache, Tags.empty()); |
| 44 | + LocalMapStats localMapStats = cache.getLocalMapStats(); |
| 45 | + MeterRegistry meterRegistry = new SimpleMeterRegistry(); |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setup() { |
| 49 | + metrics.bindTo(meterRegistry); |
| 50 | + cache.put("hello", "world"); |
| 51 | + cache.get("hello"); |
| 52 | + cache.get("hi"); |
| 53 | + cache.get("hello"); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterEach |
| 57 | + void cleanUp() { |
| 58 | + hazelcastInstance.shutdown(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void generalMetrics() { |
| 63 | + Gauge backupEntries = fetch(meterRegistry, "cache.entries", Tags.of("ownership", "backup")).gauge(); |
| 64 | + assertThat(backupEntries.value()).isEqualTo(localMapStats.getBackupEntryCount()); |
| 65 | + |
| 66 | + Gauge ownedEntries = fetch(meterRegistry, "cache.entries", Tags.of("ownership", "owned")).gauge(); |
| 67 | + assertThat(ownedEntries.value()).isEqualTo(localMapStats.getOwnedEntryCount()); |
| 68 | + |
| 69 | + Gauge backupEntryMemory = fetch(meterRegistry, "cache.entry.memory", Tags.of("ownership", "backup")).gauge(); |
| 70 | + assertThat(backupEntryMemory.value()).isEqualTo(localMapStats.getBackupEntryMemoryCost()); |
| 71 | + |
| 72 | + Gauge ownedEntryMemory = fetch(meterRegistry, "cache.entry.memory", Tags.of("ownership", "owned")).gauge(); |
| 73 | + assertThat(ownedEntryMemory.value()).isEqualTo(localMapStats.getOwnedEntryMemoryCost()); |
| 74 | + |
| 75 | + FunctionCounter partitionGets = fetch(meterRegistry, "cache.partition.gets").functionCounter(); |
| 76 | + assertThat(partitionGets.count()).isEqualTo(localMapStats.getGetOperationCount()); |
| 77 | + } |
| 78 | + |
| 79 | + @Disabled("TODO figure out how to setup near cache properly") |
| 80 | + @Test |
| 81 | + void nearCacheMetrics() { |
| 82 | + NearCacheStats nearCacheStats = localMapStats.getNearCacheStats(); |
| 83 | + Gauge hitCacheRequests = fetch(meterRegistry, "cache.near.requests", Tags.of("result", "hit")).gauge(); |
| 84 | + assertThat(hitCacheRequests.value()).isEqualTo(nearCacheStats.getHits()); |
| 85 | + |
| 86 | + Gauge missCacheRequests = fetch(meterRegistry, "cache.near.requests", Tags.of("result", "miss")).gauge(); |
| 87 | + assertThat(missCacheRequests.value()).isEqualTo(nearCacheStats.getMisses()); |
| 88 | + |
| 89 | + Gauge nearPersistance = fetch(meterRegistry, "cache.near.persistences").gauge(); |
| 90 | + assertThat(nearPersistance.value()).isEqualTo(nearCacheStats.getPersistenceCount()); |
| 91 | + |
| 92 | + Gauge nearEvictions = fetch(meterRegistry, "cache.near.evictions").gauge(); |
| 93 | + assertThat(nearEvictions.value()).isEqualTo(nearCacheStats.getEvictions()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void timingsMetrics() { |
| 98 | + TimeUnit timeUnit = TimeUnit.MILLISECONDS; |
| 99 | + FunctionTimer getsLatency = fetch(meterRegistry, "cache.gets.latency").functionTimer(); |
| 100 | + assertThat(getsLatency.count()).isEqualTo(localMapStats.getGetOperationCount()); |
| 101 | + assertThat(getsLatency.totalTime(timeUnit)).isEqualTo(localMapStats.getTotalGetLatency()); |
| 102 | + |
| 103 | + FunctionTimer putsLatency = fetch(meterRegistry, "cache.puts.latency").functionTimer(); |
| 104 | + assertThat(putsLatency.count()).isEqualTo(localMapStats.getPutOperationCount()); |
| 105 | + assertThat(putsLatency.totalTime(timeUnit)).isEqualTo(localMapStats.getTotalPutLatency()); |
| 106 | + |
| 107 | + FunctionTimer removeLatency = fetch(meterRegistry, "cache.removals.latency").functionTimer(); |
| 108 | + assertThat(removeLatency.count()).isEqualTo(localMapStats.getRemoveOperationCount()); |
| 109 | + assertThat(removeLatency.totalTime(timeUnit)).isEqualTo(localMapStats.getTotalRemoveLatency()); |
| 110 | + } |
| 111 | + |
| 112 | + private RequiredSearch fetch(MeterRegistry meterRegistry, String name) { |
| 113 | + return fetch(meterRegistry, name, Tags.empty()); |
| 114 | + } |
| 115 | + |
| 116 | + private RequiredSearch fetch(MeterRegistry meterRegistry, String name, Iterable<Tag> tags) { |
| 117 | + return meterRegistry.get(name).tags(tags); |
| 118 | + } |
| 119 | +} |
0 commit comments