zxcware commented on a change in pull request #2622: Added random key generator for reporters. Added reporters fo… URL: https://github.com/apache/incubator-gobblin/pull/2622#discussion_r280285309
########## File path: gobblin-modules/gobblin-kafka-common/src/main/java/org/apache/gobblin/metrics/kafka/KeyValueEventObjectReporter.java ########## @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.gobblin.metrics.kafka; + +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Random; + +import org.apache.avro.generic.GenericRecord; +import org.apache.commons.lang3.tuple.Pair; + +import com.google.common.base.Optional; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; +import com.typesafe.config.Config; + +import lombok.extern.slf4j.Slf4j; + +import org.apache.gobblin.configuration.ConfigurationKeys; +import org.apache.gobblin.metrics.GobblinTrackingEvent; +import org.apache.gobblin.metrics.MetricContext; +import org.apache.gobblin.metrics.reporter.EventReporter; +import org.apache.gobblin.util.AvroUtils; +import org.apache.gobblin.util.ConfigUtils; + + +@Slf4j +public class KeyValueEventObjectReporter extends EventReporter { + private static final String PUSHER_CONFIG = "pusherConfig"; + + protected Optional<List<String>> keys = Optional.absent(); + protected final String randomKey; + protected KeyValuePusher pusher; + protected Optional<Map<String,String>> namespaceOverride; + protected final String topic; + + public KeyValueEventObjectReporter(Builder<?> builder){ + super(builder); + + this.topic=builder.topic; + this.namespaceOverride=builder.namespaceOverride; + Config config = builder.config.get(); + + if (builder.pusher.isPresent()) { + this.pusher = builder.pusher.get(); + } else { + Config pusherConfig = ConfigUtils.getConfigOrEmpty(config, PUSHER_CONFIG).withFallback(config); + String pusherClassName = ConfigUtils.getString(config, "pusherClass", PusherUtils.DEFAULT_KEY_VALUE_PUSHER_CLASS_NAME); + this.pusher = PusherUtils.getKeyValuePusher(pusherClassName, builder.brokers, builder.topic, Optional.of(pusherConfig)); + } + + this.closer.register(this.pusher); + + randomKey=String.valueOf(new Random().nextInt(100)); + String pusherKeys_Key = "pusherKeys"; + if (config.hasPath(pusherKeys_Key)) { + List<String> keys = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(config.getString(pusherKeys_Key)); + this.keys = Optional.of(keys); + }else{ + log.warn("Key not assigned from config. Please set it with property {}", ConfigurationKeys.METRICS_REPORTING_EVENTS_KAFKAPUSHERKEYS); + log.warn("Using generated number " + randomKey + " as key"); + } + } + + @Override + public void reportEventQueue(Queue<GobblinTrackingEvent> queue) { + log.info("Emitting report using KeyValueEventObjectReporter"); + + List<Pair<String,GenericRecord>> events = Lists.newArrayList(); + GobblinTrackingEvent event; + + while(null != (event = queue.poll())){ + GenericRecord record = AvroUtils.overrideNameAndNamespace(event, this.topic, this.namespaceOverride); + events.add(Pair.of(buildKey(event), record)); + } + + if (!events.isEmpty()) { + this.pusher.pushKeyValueMessages(events); + } + } + + protected String buildKey(GobblinTrackingEvent event) { Review comment: define as `private`. Initially reserve the right to extend, allowing easy refactor if any in the future. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
