joewitt commented on a change in pull request #3674: Nifi 6583
URL: https://github.com/apache/nifi/pull/3674#discussion_r318266135
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-metrics-reporting-bundle/nifi-metrics-reporting-task/src/main/java/org/apache/nifi/metrics/reporting/reporter/service/AzureLogAnalysticsReporter.java
 ##########
 @@ -0,0 +1,260 @@
+/*
+ * 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.nifi.metrics.reporting.reporter.service;
+
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedMap;
+import java.util.TimeZone;
+import java.util.concurrent.TimeUnit;
+
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.MetricFilter;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.ScheduledReporter;
+import com.codahale.metrics.Timer;
+import com.codahale.metrics.json.MetricsModule;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+//import org.apache.nifi.reporting.util.metrics.api.MetricsBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import javax.net.ssl.HttpsURLConnection;
+import javax.xml.bind.DatatypeConverter;
+
+import java.io.DataOutputStream;
+import java.net.InetAddress;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+
+/**
+ * AzureLogAnalysticsReporter is a ScheduleReporter that sends metrics to 
Azure Log Analystics Workspace.
+ * For reference, look at <a 
href="https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api";>Azure
 documentation</a>. 
+ * @author Seokwon J. Yang
+ */
+public class AzureLogAnalysticsReporter extends ScheduledReporter {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(AzureLogAnalysticsReporter.class);
+    private final ObjectMapper mapper;
+    private final String workspaceId;
+    private final String workspaceKey;
+    private final String logType;
+
+    /**
+     * AzureLogAnalysticsReporter Constructor used by {@link 
AzureLogAnalysticsMetricReporterService}
+     * 
+     * @param workspaceId  Azure log analystics workspace id, retreived from 
the adavancned setting
+     * @param workspaceKey Azure log analystics workspace key (either primary 
or secondary), retreived from the adavancned setting
+     * @param logType Record type name (<a 
hef="https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api#record-type-and-properties";>Ref</>)
+     * @param registry registry with the metrics to report
+     * @param filter metric filter. By default, all
+     * @param rateUnit rate unite. By default, TimeUnit.MILLISECONDS
+     * @param durationUnit duration unit. By default, TimeUnit.MILLISECONDS
+     */
+    public AzureLogAnalysticsReporter(
+        String workspaceId,
+        String workspaceKey,
+        String logType,
+        MetricRegistry registry, MetricFilter filter, TimeUnit rateUnit, 
TimeUnit durationUnit)
+    {
+        
+        super(registry, "AzureLogAnalysticsReporter", filter, rateUnit, 
durationUnit);
+        MetricsModule metricsModule = new MetricsModule(rateUnit, 
durationUnit, false, filter);
+
+        mapper = new ObjectMapper().registerModule(metricsModule);
+        mapper.enable(SerializationFeature.INDENT_OUTPUT);
+        this.workspaceId = workspaceId;
+        this.workspaceKey = workspaceKey;
+        this.logType = logType;
+    }
+    /**
+     * InjectComputer column and value to json in string format and return the 
result
+     * @param json
+     * @return json in string with computer column and value
+     */
+    private String injectComputerName(String json) {
+        String result ="";
+        try {
+            String hostname= InetAddress.getLocalHost().getHostName();
+            ObjectMapper _mapper = new ObjectMapper();
+            Map<String, Object> map = _mapper.readValue(json, new 
TypeReference<HashMap<String, Object>>(){});
+            if(map.size() > 0) {
+                map.put("Computer", hostname);
+                result = _mapper.writeValueAsString(map);
+            }
+
+        }catch(Exception e) {
+            e.printStackTrace();
 
 Review comment:
   we generally want to avoid catching Exception.  Is somethig more specific 
workable?
   
   We definitely do not want e.printStackTrace().  Be sure to use the logger.

----------------------------------------------------------------
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

Reply via email to