|
| 1 | +/* |
| 2 | + * Copyright 2019 Dropbox.com |
| 3 | + * |
| 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 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 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 com.arpnetworking.metrics.mad.sources; |
| 17 | + |
| 18 | +import com.arpnetworking.commons.builder.ThreadLocalBuilder; |
| 19 | +import com.arpnetworking.commons.observer.Observable; |
| 20 | +import com.arpnetworking.commons.observer.Observer; |
| 21 | +import com.arpnetworking.logback.annotations.LogValue; |
| 22 | +import com.arpnetworking.metrics.common.sources.BaseSource; |
| 23 | +import com.arpnetworking.metrics.common.sources.Source; |
| 24 | +import com.arpnetworking.metrics.mad.model.DefaultRecord; |
| 25 | +import com.arpnetworking.metrics.mad.model.Record; |
| 26 | +import com.arpnetworking.steno.LogValueMapFactory; |
| 27 | +import com.arpnetworking.steno.Logger; |
| 28 | +import com.arpnetworking.steno.LoggerFactory; |
| 29 | +import net.sf.oval.constraint.NotNull; |
| 30 | + |
| 31 | +import java.time.Instant; |
| 32 | +import java.time.ZoneId; |
| 33 | +import java.time.ZonedDateTime; |
| 34 | + |
| 35 | +/** |
| 36 | + * Implementation of {@link Source} which wraps another {@link Source} and sets |
| 37 | + * the time stamp of the {@link Record}s received from the wrapped {@link Source} |
| 38 | + * to the current time. |
| 39 | + * |
| 40 | + * @author Joey Jackson (jjackson at dropbox dot com) |
| 41 | + */ |
| 42 | +public final class TimeStampingSource extends BaseSource { |
| 43 | + |
| 44 | + private static final Logger LOGGER = LoggerFactory.getLogger(MappingSource.class); |
| 45 | + |
| 46 | + private final Source _source; |
| 47 | + |
| 48 | + @Override |
| 49 | + public void start() { |
| 50 | + _source.start(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void stop() { |
| 55 | + _source.stop(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Generate a Steno log compatible representation. |
| 60 | + * |
| 61 | + * @return Steno log compatible representation. |
| 62 | + */ |
| 63 | + @LogValue |
| 64 | + public Object toLogValue() { |
| 65 | + return LogValueMapFactory.builder(this) |
| 66 | + .put("source", _source) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String toString() { |
| 72 | + return toLogValue().toString(); |
| 73 | + } |
| 74 | + |
| 75 | + private TimeStampingSource(final Builder builder) { |
| 76 | + super(builder); |
| 77 | + _source = builder._source; |
| 78 | + _source.attach(new TimeStampingObserver(this)); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private static final class TimeStampingObserver implements Observer { |
| 83 | + private final TimeStampingSource _source; |
| 84 | + |
| 85 | + TimeStampingObserver(final TimeStampingSource source) { |
| 86 | + _source = source; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void notify(final Observable observable, final Object event) { |
| 91 | + if (!(event instanceof Record)) { |
| 92 | + LOGGER.error() |
| 93 | + .setMessage("Observed unsupported event") |
| 94 | + .addData("event", event) |
| 95 | + .log(); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + final Record record = (Record) event; |
| 100 | + _source.notify( |
| 101 | + ThreadLocalBuilder.build( |
| 102 | + DefaultRecord.Builder.class, |
| 103 | + b1 -> b1.setMetrics(record.getMetrics()) |
| 104 | + .setId(record.getId()) |
| 105 | + .setTime(ZonedDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))) |
| 106 | + .setAnnotations(record.getAnnotations()) |
| 107 | + .setDimensions(record.getDimensions()))); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Builder pattern for {@link TimeStampingSource}. |
| 113 | + * |
| 114 | + * @author Joey Jackson (jjackson at dropbox dot com) |
| 115 | + */ |
| 116 | + public static final class Builder extends BaseSource.Builder<Builder, TimeStampingSource> { |
| 117 | + |
| 118 | + /** |
| 119 | + * Public constructor. |
| 120 | + */ |
| 121 | + public Builder() { |
| 122 | + super(TimeStampingSource::new); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Sets the wrapped {@link Source}. Cannot be null. |
| 127 | + * |
| 128 | + * @param value The wrapped source |
| 129 | + * @return This instance of {@link TimeStampingSource.Builder} |
| 130 | + */ |
| 131 | + public Builder setSource(final Source value) { |
| 132 | + _source = value; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected Builder self() { |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + @NotNull |
| 142 | + private Source _source; |
| 143 | + } |
| 144 | +} |
0 commit comments