Skip to content

Commit

Permalink
Clean up 107d9e9 a bit. We can use
Browse files Browse the repository at this point in the history
the StatsCollector class for the static settings and make it a
a bit more flexible in case we need to add more stats in the future.
Also remove the CLI options as we want folks to use configs instead
of clogging up the command line.

Signed-off-by: Chris Larsen <[email protected]>
  • Loading branch information
manolama committed Apr 23, 2016
1 parent 7b3db06 commit d4fe65b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 160 deletions.
4 changes: 1 addition & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ tsdb_SRC := \
src/tools/MetaPurge.java \
src/tools/MetaSync.java \
src/tools/Search.java \
src/tools/StartupPlugin.java \
src/tools/StartupPlugin.java \
src/tools/TSDMain.java \
src/tools/TSDPort.java \
src/tools/TextImporter.java \
src/tools/TreeSync.java \
src/tools/UidManager.java \
Expand Down Expand Up @@ -347,7 +346,6 @@ test_SRC := \
test/tsd/TestSuggestRpc.java \
test/tsd/TestTreeRpc.java \
test/tsd/TestUniqueIdRpc.java \
test/tsd/TestStatsWithPort.java \
test/uid/TestNoSuchUniqueId.java \
test/uid/TestRandomUniqueId.java \
test/uid/TestUniqueId.java \
Expand Down
3 changes: 3 additions & 0 deletions src/core/TSDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ public TSDB(final HBaseClient client, final Config config) {
// load up the functions that require the TSDB object
ExpressionFactory.addTSDBFunctions(this);

// set any extra tags from the config for stats
StatsCollector.setGlobalTags(config);

LOG.debug(config.dumpConfiguration());
}

Expand Down
33 changes: 28 additions & 5 deletions src/stats/StatsCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.opentsdb.tools.TSDPort;
import net.opentsdb.utils.Config;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
* Receives various stats/metrics from the current process.
Expand All @@ -36,6 +37,9 @@ public abstract class StatsCollector {
private static final Logger LOG =
LoggerFactory.getLogger(StatsCollector.class);

/** Tags to add to every stat emitted by the collector */
private static Map<String, String> global_tags;

/** Prefix to add to every metric name, for example `tsd'. */
protected final String prefix;

Expand All @@ -44,19 +48,21 @@ public abstract class StatsCollector {

/** Buffer used to build lines emitted. */
private final StringBuilder buf = new StringBuilder();

/**
* Constructor.
* @param prefix A prefix to add to every metric name, for example
* `tsd'.
*/
public StatsCollector(final String prefix) {
this.prefix = prefix;
if(TSDPort.isStatsWithPort()) {
addExtraTag("port", "" + TSDPort.getTSDPort());
if (global_tags != null && !global_tags.isEmpty()) {
for (final Entry<String, String> entry : global_tags.entrySet()) {
addExtraTag(entry.getKey(), entry.getValue());
}
}
}

/**
* Method to override to actually emit a data point.
* @param datapoint A data point in a format suitable for a text
Expand Down Expand Up @@ -245,4 +251,21 @@ public final void clearExtraTag(final String name) {
extratags.remove(name);
}

/**
* Parses the configuration to determine if any extra tags should be included
* with every stat emitted.
* @param config The config object to parse
* @throws IllegalArgumentException if the config is null. Other exceptions
* may be thrown if the config values are unparseable.
*/
public static final void setGlobalTags(final Config config) {
if (config == null) {
throw new IllegalArgumentException("Configuration cannot be null.");
}

if (config.getBoolean("tsd.core.stats_with_port")) {
global_tags = new HashMap<String, String>(1);
global_tags.put("port", config.getString("tsd.network.port"));
}
}
}
6 changes: 1 addition & 5 deletions src/tools/CliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ static void overloadConfig(final ArgP argp, final Config config) {
config.overrideConfig("tsd.network.async_io", entry.getValue());
} else if (entry.getKey().toLowerCase().equals("--worker-threads")) {
config.overrideConfig("tsd.network.worker_threads", entry.getValue());
} else if (entry.getKey().toLowerCase().equals("--max-connections")) {
config.overrideConfig("tsd.core.connections.limit", entry.getValue());
} else if (entry.getKey().toLowerCase().equals("--statswport")) {
config.overrideConfig("tsd.core.stats_with_port", "true");
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/tools/TSDMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public static void main(String[] args) throws IOException {
if (startup != null) {
startup.setReady(tsdb);
}
TSDPort.set(config);
log.info("Ready to serve on " + addr);
} catch (Throwable e) {
factory.releaseExternalResources();
Expand Down
56 changes: 0 additions & 56 deletions src/tools/TSDPort.java

This file was deleted.

11 changes: 0 additions & 11 deletions src/utils/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ public class Config {
/** tsd.http.request.enable_chunked */
private boolean enable_chunked_requests = false;

/** tsd.core.stats_with_port */
private boolean stats_with_port = false;


/** tsd.storage.fix_duplicates */
private boolean fix_duplicates = false;

Expand Down Expand Up @@ -237,11 +233,6 @@ public boolean enable_chunked_requests() {
return enable_chunked_requests;
}

/** @return whether or not rpc stats should be broken out by port */
public boolean rpc_stats_withport() {
return stats_with_port;
}

/** @return max incoming chunk size in bytes */
public int max_chunked_requests() {
return max_chunked_requests;
Expand Down Expand Up @@ -662,8 +653,6 @@ protected void loadStaticVariables() {
enable_tree_processing = this.getBoolean("tsd.core.tree.enable_processing");
fix_duplicates = this.getBoolean("tsd.storage.fix_duplicates");
scanner_max_num_rows = this.getInt("tsd.storage.hbase.scanner.maxNumRows");
stats_with_port = this.getBoolean("tsd.core.stats_with_port");

}

/**
Expand Down
29 changes: 29 additions & 0 deletions test/tsd/TestStatsRpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package net.opentsdb.tsd;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
Expand All @@ -21,6 +22,7 @@
import java.nio.charset.Charset;

import net.opentsdb.core.TSDB;
import net.opentsdb.stats.StatsCollector;
import net.opentsdb.utils.Config;

import org.hbase.async.HBaseClient;
Expand All @@ -45,6 +47,33 @@ public void before() throws Exception {
when(tsdb.getClient()).thenReturn(client);
}

@Test
public void statsWithOutPort() throws Exception {
final StatsRpc rpc = new StatsRpc();
HttpQuery query = NettyMocks.getQuery(tsdb, "/api/stats");
rpc.execute(tsdb, query);
assertEquals(HttpResponseStatus.OK, query.response().getStatus());
final String json =
query.response().getContent().toString(Charset.forName("UTF-8"));
assertFalse(json.contains("port=4242"));
}

@Test
public void statsWithPort() throws Exception {
when(tsdb.getConfig().getBoolean("tsd.core.stats_with_port"))
.thenReturn(true);
when(tsdb.getConfig().getString("tsd.network.port"))
.thenReturn("4242");
StatsCollector.setGlobalTags(tsdb.getConfig());
final StatsRpc rpc = new StatsRpc();
HttpQuery query = NettyMocks.getQuery(tsdb, "/api/stats");
rpc.execute(tsdb, query);
assertEquals(HttpResponseStatus.OK, query.response().getStatus());
final String json =
query.response().getContent().toString(Charset.forName("UTF-8"));
assertTrue(json.contains("port=4242"));
}

@Test
public void printThreadStats() throws Exception {
final StatsRpc rpc = new StatsRpc();
Expand Down
79 changes: 0 additions & 79 deletions test/tsd/TestStatsWithPort.java

This file was deleted.

0 comments on commit d4fe65b

Please sign in to comment.