Skip to content

Commit

Permalink
Add tabletype tag to improve metrics granularity.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtfulCoder committed Jun 24, 2021
1 parent 5b0de4d commit 076bc13
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ atlassian-ide-plugin.xml

# Data created by elastic search tests
metacat-main/data/
metacat-main/nodes/

# Functional tests
metacat-functional-tests/metacat-test-cluster/build/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
*/
package com.netflix.metacat.common;

import java.util.HashMap;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import lombok.NonNull;
import lombok.Setter;

/**
* The context of the request to metacat.
Expand Down Expand Up @@ -60,14 +65,22 @@ public class MetacatRequestContext implements Serializable {
// TODO: Move to Java 8 and use java.time.Instant
private final long timestamp = new Date().getTime();

private String userName;
// the following fields are immutable.
private final String clientAppName;
private final String clientId;
private final String jobId;
private final String dataTypeContext;
private final String apiUri;
private final String scheme;

// The following fields are set during request processing and are mutable.
// The general expectation is that these would be set zero or one times.
@Setter
private String userName;

@Getter(AccessLevel.NONE)
private Map<QualifiedName, String> tableTypeMap;

/**
* Constructor.
*/
Expand All @@ -79,6 +92,7 @@ public MetacatRequestContext() {
this.dataTypeContext = null;
this.apiUri = UNKNOWN;
this.scheme = UNKNOWN;
this.tableTypeMap = new HashMap<>();
}

/**
Expand Down Expand Up @@ -108,6 +122,7 @@ protected MetacatRequestContext(
this.dataTypeContext = dataTypeContext;
this.apiUri = apiUri;
this.scheme = scheme;
this.tableTypeMap = new HashMap<>();
}

@Override
Expand All @@ -122,16 +137,28 @@ public String toString() {
sb.append(", dataTypeContext='").append(dataTypeContext).append('\'');
sb.append(", apiUri='").append(apiUri).append('\'');
sb.append(", scheme='").append(scheme).append('\'');
sb.append(", tableTypeMap='").append(tableTypeMap).append('\'');
sb.append('}');
return sb.toString();
}


/**
* Store the tableType associated with table specified by qualifiedName param.
* @param qualifiedName fully qualified name of table
* @param tableType table type of table
*/
public void updateTableTypeMap(@NonNull final QualifiedName qualifiedName, final String tableType) {
this.tableTypeMap.put(qualifiedName, tableType);
}

/**
* Returns the username.
* @param userName user name
* Gets tableType.
* @param qualifiedName fully qualified name of the table
* @return the tableType associated with table specified by qualifiedName param.
*/
public void setUserName(final String userName) {
this.userName = userName;
public String getTableType(@NonNull final QualifiedName qualifiedName) {
return this.tableTypeMap.get(qualifiedName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.netflix.metacat.common.MetacatRequestContext;
import com.netflix.metacat.common.QualifiedName;
import com.netflix.metacat.common.server.connectors.model.TableInfo;
import com.netflix.metacat.common.server.util.MetacatContextManager;
import com.netflix.metacat.connector.hive.sql.DirectSqlTable;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.common.JavaUtils;
import org.apache.hadoop.hive.metastore.MetaStoreUtils;
Expand Down Expand Up @@ -110,10 +113,26 @@ private static Deserializer createDeserializer(final Class<? extends Deserialize
* @return true for iceberg table
*/
public static boolean isIcebergTable(final TableInfo tableInfo) {
return tableInfo.getMetadata() != null
&& tableInfo.getMetadata().containsKey(DirectSqlTable.PARAM_TABLE_TYPE)
&& DirectSqlTable.ICEBERG_TABLE_TYPE
.equalsIgnoreCase(tableInfo.getMetadata().get(DirectSqlTable.PARAM_TABLE_TYPE));
final String tableTypeVal = getTableType(tableInfo);
return DirectSqlTable.ICEBERG_TABLE_TYPE.equalsIgnoreCase(tableTypeVal);
}

private static String getTableType(final TableInfo tableInfo) {
final QualifiedName tableName = tableInfo.getName();
final String fallbackTableType = "unknown";
final MetacatRequestContext context = MetacatContextManager.getContext();
final Map<String, String> metadata = tableInfo.getMetadata();

if (metadata == null) {
context.updateTableTypeMap(tableName, fallbackTableType);
return null;
}
String tableType = metadata.get(DirectSqlTable.PARAM_TABLE_TYPE);
if (StringUtils.isBlank(tableType)) {
tableType = fallbackTableType;
}
context.updateTableTypeMap(tableName, tableType);
return tableType;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.netflix.metacat.common.MetacatRequestContext;
import com.netflix.metacat.common.QualifiedName;
import com.netflix.metacat.common.exception.MetacatAlreadyExistsException;
import com.netflix.metacat.common.exception.MetacatBadRequestException;
Expand Down Expand Up @@ -47,6 +48,7 @@
import com.netflix.spectator.api.Registry;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -152,7 +154,9 @@ public <R> R processRequest(
final Supplier<R> supplier) {
final long start = registry.clock().wallTime();
final Map<String, String> tags = new HashMap<>(name.parts());
tags.putAll(requestTags);
if (requestTags != null) {
tags.putAll(requestTags);
}
tags.put("request", resourceRequestName);
tags.put("scheme", MetacatContextManager.getContext().getScheme());
registry.counter(requestCounterId.withTags(tags)).increment();
Expand Down Expand Up @@ -216,10 +220,19 @@ public <R> R processRequest(
} finally {
final long duration = registry.clock().wallTime() - start;
log.info("### Time taken to complete {} for {} is {} ms", resourceRequestName, name, duration);
tryAddTableTypeTag(tags, name);
this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
}
}

private static void tryAddTableTypeTag(final Map<String, String> tags, final QualifiedName qualifiedName) {
final MetacatRequestContext context = MetacatContextManager.getContext();
final String tableType = context.getTableType(qualifiedName);
if (!StringUtils.isBlank(tableType)) {
tags.put("tableType", tableType.toLowerCase());
}
}

/**
* Simple request wrapper to process request.
*
Expand Down

0 comments on commit 076bc13

Please sign in to comment.