Skip to content

Commit

Permalink
convert PlaceholderFactory to interface (#327)
Browse files Browse the repository at this point in the history
Changes the PlaceholderFactory class to be an interface
instead. This should give us more flexibility later on if
needed.

Also does some cleanup:

* Remove unused NoopPlaceholderId class.
* Specify registry when constructing a PlaceholderId rather
    then when invoking resolveToId.
* Remove tags method from PlaceholderId interface, which
    was a legacy from when the interface extended Id.
  • Loading branch information
pstout authored and brharrington committed Oct 11, 2016
1 parent 5761175 commit f301832
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Measurement;
import com.netflix.spectator.api.Meter;
import com.netflix.spectator.api.Registry;

import java.util.function.Function;

Expand All @@ -28,31 +27,28 @@
*/
abstract class AbstractDefaultPlaceholderMeter<T extends Meter> implements Meter {
private final PlaceholderId id;
private final Registry registry;
private final Function<Id, T> meterResolver;

/**
* Creates a new dynamic meter.
*
* @param id the dynamic id for the meter
* @param id the dynamic id for the meter
* @param meterResolver the function to map a resolved id to concrete metric
*/
AbstractDefaultPlaceholderMeter(PlaceholderId id, Registry registry, Function<Id, T> meterResolver) {
AbstractDefaultPlaceholderMeter(PlaceholderId id, Function<Id, T> meterResolver) {
this.id = id;
this.registry = registry;
this.meterResolver = meterResolver;
}

/**
* Resolve the dynamic id to the current metric instance.
*/
protected final T resolveToCurrentMeter() {
return meterResolver.apply(id.resolveToId(registry));
return meterResolver.apply(id());
}

@Override
public final Id id() {
return resolveToCurrentMeter().id();
return id.resolveToId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DefaultPlaceholderCounter extends AbstractDefaultPlaceholderMeter<Counter>
* @param registry the registry to use to instantiate the individual counters
*/
DefaultPlaceholderCounter(PlaceholderId id, Registry registry) {
super(id, registry, registry::counter);
super(id, registry::counter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DefaultPlaceholderDistributionSummary extends AbstractDefaultPlaceholderMe
* @param registry the registry to use to instantiate the individual distribution summaries
*/
DefaultPlaceholderDistributionSummary(PlaceholderId id, Registry registry) {
super(id, registry, registry::distributionSummary);
super(id, registry::distributionSummary);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright 2016 Netflix, Inc.
*
* 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 com.netflix.spectator.placeholders;

import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.DistributionSummary;
import com.netflix.spectator.api.Registry;
import com.netflix.spectator.api.Timer;

/**
* Factory for creating instances of activity based meters with placeholders in the
* identifiers so that the final id can be resolved when the activity takes place.
*/
final class DefaultPlaceholderFactory implements PlaceholderFactory {

private final Registry registry;

/**
* Create a new instance of the factory.
*
* @param registry
* the registry to use when creating ids
*/
DefaultPlaceholderFactory(Registry registry) {
this.registry = registry;
}

@Override
public PlaceholderId createId(String name) {
return new DefaultPlaceholderId(name, registry);
}

@Override
public PlaceholderId createId(String name, Iterable<TagFactory> tagFactories) {
return DefaultPlaceholderId.createWithFactories(name, tagFactories, registry);
}

@Override
public Counter counter(PlaceholderId id) {
return new DefaultPlaceholderCounter(id, registry);
}

@Override
public DistributionSummary distributionSummary(PlaceholderId id) {
return new DefaultPlaceholderDistributionSummary(id, registry);
}

@Override
public Timer timer(PlaceholderId id) {
return new DefaultPlaceholderTimer(id, registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Collection<TagFactory> asCollection() {

private final String name;
private final Collection<TagFactory> tagFactories;
private final Registry registry;

/**
* Creates a new id with the specified name and collection of factories.
Expand All @@ -87,52 +88,48 @@ Collection<TagFactory> asCollection() {
* the name of the new id
* @param tagFactories
* the possibly empty collection of factories to be attached to the new id
* @param registry
* the registry to use when resolving to an {@link Id}
* @return
* the newly created id
*/
static DefaultPlaceholderId createWithFactories(String name, Iterable<TagFactory> tagFactories) {
static DefaultPlaceholderId createWithFactories(String name, Iterable<TagFactory> tagFactories, Registry registry) {
if (tagFactories == null) {
return new DefaultPlaceholderId(name);
return new DefaultPlaceholderId(name, registry);
} else {
FactorySorterAndDeduplicator sorter = new FactorySorterAndDeduplicator(tagFactories);

return new DefaultPlaceholderId(name, sorter.asCollection());
return new DefaultPlaceholderId(name, sorter.asCollection(), registry);
}
}

/**
* Constructs a new id with the specified name and no associated tag factories.
*/
DefaultPlaceholderId(String name) {
this(name, Collections.emptyList());
DefaultPlaceholderId(String name, Registry registry) {
this(name, Collections.emptyList(), registry);
}

/**
* Constructs a new id with the specified name and tag factories.
*
* @param name
* @param name
* the name of the new id
* @param tagFactories
* the possibly empty, immutable collection of tag factories to use
* the possibly empty collection of factories to be attached to the new id
* @param registry
* the registry to use when resolving to an {@link Id}
*/
private DefaultPlaceholderId(String name, Collection<TagFactory> tagFactories) {
private DefaultPlaceholderId(String name, Collection<TagFactory> tagFactories, Registry registry) {
this.name = Preconditions.checkNotNull(name, "name");
this.tagFactories = tagFactories;
this.registry = registry;
}

@Override
public String name() {
return name;
}

@Override
public Iterable<Tag> tags() {
return tagFactories.stream()
.map(TagFactory::createTag)
.filter(tag -> tag != null)
.collect(Collectors.toList());
}

@Override
public DefaultPlaceholderId withTag(String k, String v) {
return withTagFactory(new ConstantTagFactory(new BasicTag(k, v)));
Expand All @@ -157,7 +154,7 @@ public DefaultPlaceholderId withTags(Map<String, String> tags) {
@Override
public DefaultPlaceholderId withTagFactory(TagFactory factory) {
if (tagFactories.isEmpty()) {
return new DefaultPlaceholderId(name, Collections.singleton(factory));
return new DefaultPlaceholderId(name, Collections.singleton(factory), registry);
} else {
return createNewId(sorter -> sorter.addFactory(factory));
}
Expand All @@ -169,8 +166,13 @@ public DefaultPlaceholderId withTagFactories(Iterable<TagFactory> factories) {
}

@Override
public Id resolveToId(Registry registry) {
return registry.createId(name, tags());
public Id resolveToId() {
Iterable<Tag> tags = tagFactories.stream()
.map(TagFactory::createTag)
.filter(tag -> tag != null)
.collect(Collectors.toList());

return registry.createId(name, tags);
}

@Override
Expand All @@ -193,14 +195,7 @@ public int hashCode() {

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(name);
if (!tagFactories.isEmpty()) {
for (Tag cur: tags()) {
buf.append(":").append(cur.key()).append("=").append(cur.value());
}
}
return buf.toString();
return resolveToId().toString();
}

/**
Expand All @@ -216,6 +211,6 @@ private DefaultPlaceholderId createNewId(Consumer<FactorySorterAndDeduplicator>
FactorySorterAndDeduplicator sorter = new FactorySorterAndDeduplicator(tagFactories);

consumer.accept(sorter);
return new DefaultPlaceholderId(name, sorter.asCollection());
return new DefaultPlaceholderId(name, sorter.asCollection(), registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DefaultPlaceholderTimer extends AbstractDefaultPlaceholderMeter<Timer> imp
* @param registry the registry to use to instantiate the individual timers
*/
DefaultPlaceholderTimer(PlaceholderId id, Registry registry) {
super(id, registry, registry::timer);
super(id, registry::timer);
}

@Override
Expand Down

This file was deleted.

Loading

0 comments on commit f301832

Please sign in to comment.