Github user bowenli86 commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3736#discussion_r112340440
  
    --- Diff: 
flink-metrics/flink-metrics-datadog/src/main/java/org/apache/flink/metrics/datadog/DatadogHttpReporter.java
 ---
    @@ -0,0 +1,214 @@
    +/*
    + * 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.flink.metrics.datadog;
    +
    +import org.apache.flink.metrics.CharacterFilter;
    +import org.apache.flink.metrics.Counter;
    +import org.apache.flink.metrics.Gauge;
    +import org.apache.flink.metrics.Meter;
    +import org.apache.flink.metrics.Histogram;
    +import org.apache.flink.metrics.Metric;
    +import org.apache.flink.metrics.MetricConfig;
    +import org.apache.flink.metrics.MetricGroup;
    +import org.apache.flink.metrics.datadog.utils.SerializationUtils;
    +import org.apache.flink.metrics.reporter.MetricReporter;
    +import org.apache.flink.metrics.reporter.Scheduled;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.concurrent.ConcurrentHashMap;
    +
    +
    +/**
    + * Metric Reporter for Datadog
    + *
    + * Variables in metrics scope will be sent to Datadog as tags
    + * */
    +public class DatadogHttpReporter implements MetricReporter, 
CharacterFilter, Scheduled {
    +   private static final Logger LOGGER = 
LoggerFactory.getLogger(DatadogHttpReporter.class);
    +
    +   // Both Flink's Gauge and Meter values are taken as gauge in Datadog
    +   private final Map<Gauge, DGauge> gauges = new ConcurrentHashMap<>();
    +   private final Map<Counter, DCounter> counters = new 
ConcurrentHashMap<>();
    +   private final Map<Meter, DMeter> meters = new ConcurrentHashMap<>();
    +
    +   private DatadogHttpClient client;
    +   private List<String> configTags;
    +
    +   public static final String API_KEY = "apikey";
    +   public static final String TAGS = "tags";
    +
    +   @Override
    +   public void notifyOfAddedMetric(Metric metric, String metricName, 
MetricGroup group) {
    +           final String name = group.getMetricIdentifier(metricName, this);
    +
    +           List<String> tags = new ArrayList<>(configTags);
    +           tags.addAll(getTagsFromMetricGroup(group));
    +
    +           synchronized (this) {
    +                   if (metric instanceof Counter) {
    +                           Counter c = (Counter) metric;
    +                           counters.put(c, new DCounter(c, name, tags));
    +                   } else if (metric instanceof Gauge) {
    +                           Gauge g = (Gauge) metric;
    +                           gauges.put(g, new DGauge(g, name, tags));
    +                   } else if(metric instanceof Meter) {
    +                           Meter m = (Meter) metric;
    +                           // Only consider rate
    +                           meters.put(m, new DMeter(m, name, tags));
    +                   } else if (metric instanceof Histogram) {
    +                           LOGGER.warn("Cannot add {} because Datadog HTTP 
API doesn't support Histogram", metricName);
    +                   } else {
    +                           LOGGER.warn("Cannot add unknown metric type {}. 
This indicates that the reporter " +
    +                                   "does not support this metric type.", 
metric.getClass().getName());
    +                   }
    +           }
    +   }
    +
    +   @Override
    +   public void notifyOfRemovedMetric(Metric metric, String metricName, 
MetricGroup group) {
    +           synchronized (this) {
    +                   if (metric instanceof Counter) {
    +                           counters.remove(metric);
    +                   } else if (metric instanceof Gauge) {
    +                           gauges.remove(metric);
    +                   } else if (metric instanceof Meter) {
    +                           meters.remove(metric);
    +                   } else if (metric instanceof Histogram) {
    +                           // No Histogram and Meter metrics are registered
    +                   } else {
    +                           LOGGER.warn("Cannot remove unknown metric type 
{}. This indicates that the reporter " +
    +                                   "does not support this metric type.", 
metric.getClass().getName());
    +                   }
    +           }
    +   }
    +
    +   @Override
    +   public void open(MetricConfig config) {
    +           client = new DatadogHttpClient(config.getString(API_KEY, null));
    +           LOGGER.info("Configured DatadogHttpReporter");
    +
    +           configTags = getTagsFromConfig(config.getString(TAGS, null));
    +   }
    +
    +   @Override
    +   public void close() {
    +           LOGGER.info("Shut down DatadogHttpReporter");
    +   }
    +
    +   @Override
    +   public void report() {
    +           try {
    +                   DatadogHttpRequest request = new 
DatadogHttpRequest(this);
    +
    +                   for(DGauge g : gauges.values()) {
    +                           try {
    +                                   // Will throw exception if the Gauge is 
not of Number type
    +                                   // Flink uses Gauge to store many types 
other than Number
    +                                   g.getMetricValue();
    +                                   request.addGauge(g);
    +                           } catch (Exception e) {
    +                                   // ignore if the Gauge is not of Number 
type
    +                           }
    +                   }
    +
    +                   for(DCounter c : counters.values()) {
    --- End diff --
    
    I'll fix them. 
    
    But, is it enforced programmatically? I didn't see any checkstyle errors


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to