Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public enum MetricType {
*/
GAUGE_RUBYTIMESTAMP("gauge/rubytimestamp"),

/**
* A gauge backed by a {@link java.util.List} type.
*/
GAUGE_LIST("gauge/list"),

/**
* A flow-rate {@link FlowMetric}, instantiated with one or more backing {@link Metric}{@code <Number>}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.logstash.instrument.metrics.AbstractMetric;
import org.logstash.instrument.metrics.MetricType;

import java.util.List;
import java.util.Optional;

/**
Expand Down Expand Up @@ -117,6 +118,8 @@ private synchronized void wakeMetric(Object value) {
lazyMetric = new RubyHashGauge(key, (RubyHash) value);
} else if (value instanceof RubyTimestamp) {
lazyMetric = new RubyTimeStampGauge(key, (RubyTimestamp) value);
} else if (value instanceof List<?>) {
lazyMetric = new ListGauge(key, (List<?>) value);
} else {
LOGGER.warn("A gauge metric of an unknown type ({}) has been created for key: {}. This may result in invalid serialization. It is recommended to " +
"log an issue to the responsible developer/development team.", value.getClass().getCanonicalName(), key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 org.logstash.instrument.metrics.gauge;

import org.logstash.instrument.metrics.MetricType;

import java.util.List;

public class ListGauge extends AbstractGaugeMetric<List<?>> {

protected ListGauge(String name) {
super(name);
}

protected ListGauge(String name, List<?> initialValue) {
super(name, initialValue);
}

public MetricType getType() {
return MetricType.GAUGE_LIST;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void ensurePassivity(){
nameMap.put(MetricType.GAUGE_TEXT, "gauge/text");
nameMap.put(MetricType.GAUGE_BOOLEAN, "gauge/boolean");
nameMap.put(MetricType.GAUGE_NUMBER, "gauge/number");
nameMap.put(MetricType.GAUGE_LIST, "gauge/list");
nameMap.put(MetricType.GAUGE_UNKNOWN, "gauge/unknown");
nameMap.put(MetricType.GAUGE_RUBYHASH, "gauge/rubyhash");
nameMap.put(MetricType.GAUGE_RUBYTIMESTAMP, "gauge/rubytimestamp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@

package org.logstash.instrument.metrics.gauge;

import java.net.URI;
import java.util.Collections;
import org.jruby.RubyHash;
import org.junit.Test;
import org.logstash.RubyUtil;
import org.logstash.Timestamp;
import org.logstash.ext.JrubyTimestampExtLibrary;
import org.logstash.instrument.metrics.MetricType;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -91,6 +93,11 @@ public void getValue() {
assertThat(gauge.get()).isNull();
assertThat(gauge.getType()).isNull();

//List
gauge = new LazyDelegatingGauge("bar", List.of("one", "two"));
assertThat(gauge.getValue().toString()).isEqualTo("[one, two]");
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);

assertThat(gauge.getName()).isNotEmpty();
}

Expand Down Expand Up @@ -169,6 +176,13 @@ public void set() {
gauge.set(null);
assertThat(gauge.getValue()).isNull();
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_TEXT);

// List
gauge = new LazyDelegatingGauge("bar");
gauge.set(List.of(1,2));
assertThat(gauge.getValue().toString()).isEqualTo("[1, 2]");
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 org.logstash.instrument.metrics.gauge;

import org.jruby.RubyHash;
import org.jruby.runtime.builtin.IRubyObject;
import org.junit.Test;
import org.logstash.RubyUtil;
import org.logstash.instrument.metrics.MetricType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for {@link TextGauge}
*/
public class ListGaugeTest {
@Test
public void getSetStrings() {
ListGauge gauge = new ListGauge("bar");
gauge.set(List.of("pipeline_name1", "pipeline_name2"));
assertThat(gauge.getValue().toString()).isEqualTo("[pipeline_name1, pipeline_name2]");
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);
}

@Test
public void getSetNull() {
ListGauge gauge = new ListGauge("bar");
gauge.set(null);
assertThat(gauge.getValue()).isNull();
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);
}

@Test
public void getSetListOfNull() {
List<Object> nullList = new ArrayList<>(Collections.nCopies(2, null));
ListGauge gauge = new ListGauge("bar");
gauge.set(nullList);
assertThat(gauge.getValue().toString()).isEqualTo("[null, null]");
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);
}

@Test
public void getSetHash() {
IRubyObject[] args = {RubyUtil.RUBY.newString("k"), RubyUtil.RUBY.newString("v")};
RubyHash rubyHash = RubyHash.newHash(RubyUtil.RUBY, args[0], args[1]);

ListGauge gauge = new ListGauge("bar");
gauge.set(List.of(rubyHash));
assertThat(gauge.getValue().toString()).isEqualTo("[{\"k\"=>\"v\"}]");
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);
}

@Test
public void getSetNumber() {
ListGauge gauge = new ListGauge("bar");
gauge.set(List.of(123, 456.7));
assertThat(gauge.getValue().toString()).isEqualTo("[123, 456.7]");
assertThat(gauge.getType()).isEqualTo(MetricType.GAUGE_LIST);
}
}