zxcware commented on a change in pull request #2622: [GOBBLIN-758] Added new reporters to emit MetricReport and GobblinTrackingEvent without serializing them. Also added random key generator for reporters. URL: https://github.com/apache/incubator-gobblin/pull/2622#discussion_r280842949
########## File path: gobblin-modules/gobblin-kafka-common/src/main/java/org/apache/gobblin/metrics/kafka/KeyValueEventObjectReporter.java ########## @@ -0,0 +1,173 @@ +/* + * 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"; + private static final String PUSHER_CLASS = "pusherClass"; + private static final String PUSHER_KEYS = "pusherKeys"; + + 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(); + Config pusherConfig = ConfigUtils.getConfigOrEmpty(config, PUSHER_CONFIG).withFallback(config); + String pusherClassName = + ConfigUtils.getString(config, PUSHER_CLASS, 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)); + if (config.hasPath(PUSHER_KEYS)) { + List<String> keys = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(config.getString(PUSHER_KEYS)); + 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); + } + } + + private String buildKey(GobblinTrackingEvent event) { + + String key = randomKey; + if (this.keys.isPresent()) { + StringBuilder keyBuilder = new StringBuilder(); + for (String keyPart : keys.get()) { + Optional value = AvroUtils.getFieldValue(event, keyPart); + if (value.isPresent()) { + keyBuilder.append(value.get().toString()); Review comment: separator? ---------------------------------------------------------------- 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
