Skip to content

Commit 076bc13

Browse files
committed
Add tabletype tag to improve metrics granularity.
1 parent 5b0de4d commit 076bc13

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ atlassian-ide-plugin.xml
6767

6868
# Data created by elastic search tests
6969
metacat-main/data/
70+
metacat-main/nodes/
7071

7172
# Functional tests
7273
metacat-functional-tests/metacat-test-cluster/build/

metacat-common/src/main/java/com/netflix/metacat/common/MetacatRequestContext.java

+32-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
*/
1818
package com.netflix.metacat.common;
1919

20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import lombok.AccessLevel;
2023
import lombok.Getter;
2124

2225
import javax.annotation.Nullable;
2326
import java.io.Serializable;
2427
import java.util.Date;
2528
import java.util.UUID;
29+
import lombok.NonNull;
30+
import lombok.Setter;
2631

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

63-
private String userName;
68+
// the following fields are immutable.
6469
private final String clientAppName;
6570
private final String clientId;
6671
private final String jobId;
6772
private final String dataTypeContext;
6873
private final String apiUri;
6974
private final String scheme;
7075

76+
// The following fields are set during request processing and are mutable.
77+
// The general expectation is that these would be set zero or one times.
78+
@Setter
79+
private String userName;
80+
81+
@Getter(AccessLevel.NONE)
82+
private Map<QualifiedName, String> tableTypeMap;
83+
7184
/**
7285
* Constructor.
7386
*/
@@ -79,6 +92,7 @@ public MetacatRequestContext() {
7992
this.dataTypeContext = null;
8093
this.apiUri = UNKNOWN;
8194
this.scheme = UNKNOWN;
95+
this.tableTypeMap = new HashMap<>();
8296
}
8397

8498
/**
@@ -108,6 +122,7 @@ protected MetacatRequestContext(
108122
this.dataTypeContext = dataTypeContext;
109123
this.apiUri = apiUri;
110124
this.scheme = scheme;
125+
this.tableTypeMap = new HashMap<>();
111126
}
112127

113128
@Override
@@ -122,16 +137,28 @@ public String toString() {
122137
sb.append(", dataTypeContext='").append(dataTypeContext).append('\'');
123138
sb.append(", apiUri='").append(apiUri).append('\'');
124139
sb.append(", scheme='").append(scheme).append('\'');
140+
sb.append(", tableTypeMap='").append(tableTypeMap).append('\'');
125141
sb.append('}');
126142
return sb.toString();
127143
}
128144

145+
146+
/**
147+
* Store the tableType associated with table specified by qualifiedName param.
148+
* @param qualifiedName fully qualified name of table
149+
* @param tableType table type of table
150+
*/
151+
public void updateTableTypeMap(@NonNull final QualifiedName qualifiedName, final String tableType) {
152+
this.tableTypeMap.put(qualifiedName, tableType);
153+
}
154+
129155
/**
130-
* Returns the username.
131-
* @param userName user name
156+
* Gets tableType.
157+
* @param qualifiedName fully qualified name of the table
158+
* @return the tableType associated with table specified by qualifiedName param.
132159
*/
133-
public void setUserName(final String userName) {
134-
this.userName = userName;
160+
public String getTableType(@NonNull final QualifiedName qualifiedName) {
161+
return this.tableTypeMap.get(qualifiedName);
135162
}
136163

137164
/**

metacat-connector-hive/src/main/java/com/netflix/metacat/connector/hive/util/HiveTableUtil.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
import com.google.common.base.Preconditions;
1919
import com.google.common.base.Throwables;
20+
import com.netflix.metacat.common.MetacatRequestContext;
2021
import com.netflix.metacat.common.QualifiedName;
2122
import com.netflix.metacat.common.server.connectors.model.TableInfo;
23+
import com.netflix.metacat.common.server.util.MetacatContextManager;
2224
import com.netflix.metacat.connector.hive.sql.DirectSqlTable;
25+
import org.apache.commons.lang.StringUtils;
2326
import org.apache.hadoop.conf.Configuration;
2427
import org.apache.hadoop.hive.common.JavaUtils;
2528
import org.apache.hadoop.hive.metastore.MetaStoreUtils;
@@ -110,10 +113,26 @@ private static Deserializer createDeserializer(final Class<? extends Deserialize
110113
* @return true for iceberg table
111114
*/
112115
public static boolean isIcebergTable(final TableInfo tableInfo) {
113-
return tableInfo.getMetadata() != null
114-
&& tableInfo.getMetadata().containsKey(DirectSqlTable.PARAM_TABLE_TYPE)
115-
&& DirectSqlTable.ICEBERG_TABLE_TYPE
116-
.equalsIgnoreCase(tableInfo.getMetadata().get(DirectSqlTable.PARAM_TABLE_TYPE));
116+
final String tableTypeVal = getTableType(tableInfo);
117+
return DirectSqlTable.ICEBERG_TABLE_TYPE.equalsIgnoreCase(tableTypeVal);
118+
}
119+
120+
private static String getTableType(final TableInfo tableInfo) {
121+
final QualifiedName tableName = tableInfo.getName();
122+
final String fallbackTableType = "unknown";
123+
final MetacatRequestContext context = MetacatContextManager.getContext();
124+
final Map<String, String> metadata = tableInfo.getMetadata();
125+
126+
if (metadata == null) {
127+
context.updateTableTypeMap(tableName, fallbackTableType);
128+
return null;
129+
}
130+
String tableType = metadata.get(DirectSqlTable.PARAM_TABLE_TYPE);
131+
if (StringUtils.isBlank(tableType)) {
132+
tableType = fallbackTableType;
133+
}
134+
context.updateTableTypeMap(tableName, tableType);
135+
return tableType;
117136
}
118137

119138
/**

metacat-main/src/main/java/com/netflix/metacat/main/api/RequestWrapper.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.base.Throwables;
2121
import com.google.common.collect.Maps;
22+
import com.netflix.metacat.common.MetacatRequestContext;
2223
import com.netflix.metacat.common.QualifiedName;
2324
import com.netflix.metacat.common.exception.MetacatAlreadyExistsException;
2425
import com.netflix.metacat.common.exception.MetacatBadRequestException;
@@ -47,6 +48,7 @@
4748
import com.netflix.spectator.api.Registry;
4849
import lombok.NonNull;
4950
import lombok.extern.slf4j.Slf4j;
51+
import org.apache.commons.lang3.StringUtils;
5052
import org.springframework.beans.factory.annotation.Autowired;
5153
import org.springframework.stereotype.Component;
5254

@@ -152,7 +154,9 @@ public <R> R processRequest(
152154
final Supplier<R> supplier) {
153155
final long start = registry.clock().wallTime();
154156
final Map<String, String> tags = new HashMap<>(name.parts());
155-
tags.putAll(requestTags);
157+
if (requestTags != null) {
158+
tags.putAll(requestTags);
159+
}
156160
tags.put("request", resourceRequestName);
157161
tags.put("scheme", MetacatContextManager.getContext().getScheme());
158162
registry.counter(requestCounterId.withTags(tags)).increment();
@@ -216,10 +220,19 @@ public <R> R processRequest(
216220
} finally {
217221
final long duration = registry.clock().wallTime() - start;
218222
log.info("### Time taken to complete {} for {} is {} ms", resourceRequestName, name, duration);
223+
tryAddTableTypeTag(tags, name);
219224
this.registry.timer(requestTimerId.withTags(tags)).record(duration, TimeUnit.MILLISECONDS);
220225
}
221226
}
222227

228+
private static void tryAddTableTypeTag(final Map<String, String> tags, final QualifiedName qualifiedName) {
229+
final MetacatRequestContext context = MetacatContextManager.getContext();
230+
final String tableType = context.getTableType(qualifiedName);
231+
if (!StringUtils.isBlank(tableType)) {
232+
tags.put("tableType", tableType.toLowerCase());
233+
}
234+
}
235+
223236
/**
224237
* Simple request wrapper to process request.
225238
*

0 commit comments

Comments
 (0)