-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CASSSIDECAR-216: Capture Metrics for Schema Reporting #204
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,9 +20,13 @@ | |||||
package org.apache.cassandra.sidecar.datahub; | ||||||
|
||||||
import java.util.List; | ||||||
import java.util.concurrent.TimeUnit; | ||||||
import java.util.stream.Stream; | ||||||
import com.google.common.base.Stopwatch; | ||||||
import com.google.common.collect.ImmutableList; | ||||||
import com.google.common.collect.Streams; | ||||||
import org.slf4j.Logger; | ||||||
import org.slf4j.LoggerFactory; | ||||||
|
||||||
import com.datastax.driver.core.Cluster; | ||||||
import com.datastax.driver.core.KeyspaceMetadata; | ||||||
|
@@ -34,6 +38,9 @@ | |||||
import datahub.client.Emitter; | ||||||
import datahub.event.MetadataChangeProposalWrapper; | ||||||
import org.apache.cassandra.sidecar.common.server.utils.ThrowableUtils; | ||||||
import org.apache.cassandra.sidecar.metrics.DeltaGauge; | ||||||
import org.apache.cassandra.sidecar.metrics.SidecarMetrics; | ||||||
import org.apache.cassandra.sidecar.metrics.server.SchemaReportingMetrics; | ||||||
import org.jetbrains.annotations.NotNull; | ||||||
|
||||||
/** | ||||||
|
@@ -47,6 +54,8 @@ | |||||
@Singleton | ||||||
public class SchemaReporter | ||||||
{ | ||||||
private static final Logger LOGGER = LoggerFactory.getLogger(SchemaReporter.class); | ||||||
|
||||||
@NotNull | ||||||
protected final IdentifiersProvider identifiersProvider; | ||||||
@NotNull | ||||||
|
@@ -57,6 +66,8 @@ public class SchemaReporter | |||||
protected final List<TableToAspectConverter<? extends RecordTemplate>> tableConverters; | ||||||
@NotNull | ||||||
protected final EmitterFactory emitterFactory; | ||||||
@NotNull | ||||||
protected final SchemaReportingMetrics reportingMetrics; | ||||||
|
||||||
/** | ||||||
* The public constructor that instantiates {@link SchemaReporter} with default configuration. | ||||||
|
@@ -66,10 +77,12 @@ public class SchemaReporter | |||||
* | ||||||
* @param identifiersProvider an instance of {@link IdentifiersProvider} to use | ||||||
* @param emitterFactory an instance of {@link EmitterFactory} to use | ||||||
* @param sidecarMetrics an instance of {@link SidecarMetrics} to obtain {@link SchemaReportingMetrics} from | ||||||
*/ | ||||||
@Inject | ||||||
public SchemaReporter(@NotNull IdentifiersProvider identifiersProvider, | ||||||
@NotNull EmitterFactory emitterFactory) | ||||||
@NotNull EmitterFactory emitterFactory, | ||||||
@NotNull SidecarMetrics sidecarMetrics) | ||||||
{ | ||||||
this(identifiersProvider, | ||||||
ImmutableList.of(new ClusterToDataPlatformInfoConverter(identifiersProvider), | ||||||
|
@@ -85,7 +98,8 @@ public SchemaReporter(@NotNull IdentifiersProvider identifiersProvider, | |||||
new TableToDataPlatformInstanceConverter(identifiersProvider), | ||||||
new TableToBrowsePathsV2Converter(identifiersProvider), | ||||||
new TableToBrowsePathsConverter(identifiersProvider)), | ||||||
emitterFactory); | ||||||
emitterFactory, | ||||||
sidecarMetrics.server().schemaReporting()); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -96,45 +110,73 @@ public SchemaReporter(@NotNull IdentifiersProvider identifiersProvider, | |||||
* @param keyspaceConverters a {@link List} of {@link KeyspaceToAspectConverter} instances to use | ||||||
* @param tableConverters a {@link List} of {@link TableToAspectConverter} instances to use | ||||||
* @param emitterFactory an instance of {@link EmitterFactory} to use | ||||||
* @param reportingMetrics an instance of {@link SchemaReportingMetrics} to use | ||||||
*/ | ||||||
protected SchemaReporter(@NotNull IdentifiersProvider identifiersProvider, | ||||||
@NotNull List<ClusterToAspectConverter<? extends RecordTemplate>> clusterConverters, | ||||||
@NotNull List<KeyspaceToAspectConverter<? extends RecordTemplate>> keyspaceConverters, | ||||||
@NotNull List<TableToAspectConverter<? extends RecordTemplate>> tableConverters, | ||||||
@NotNull EmitterFactory emitterFactory) | ||||||
@NotNull EmitterFactory emitterFactory, | ||||||
@NotNull SchemaReportingMetrics reportingMetrics) | ||||||
{ | ||||||
this.identifiersProvider = identifiersProvider; | ||||||
this.clusterConverters = clusterConverters; | ||||||
this.keyspaceConverters = keyspaceConverters; | ||||||
this.tableConverters = tableConverters; | ||||||
this.emitterFactory = emitterFactory; | ||||||
this.reportingMetrics = reportingMetrics; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Public method for converting and reporting the Cassandra schema | ||||||
* Public method for converting and reporting the Cassandra schema when triggered by a scheduled periodic task | ||||||
* | ||||||
* @param cluster the {@link Cluster} to extract Cassandra schema from | ||||||
*/ | ||||||
public void process(@NotNull Cluster cluster) | ||||||
public void processScheduled(@NotNull Cluster cluster) | ||||||
{ | ||||||
process(cluster.getMetadata()); | ||||||
process(cluster.getMetadata(), reportingMetrics.startedSchedule.metric); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Public method for converting and reporting the Cassandra schema | ||||||
* Public method for converting and reporting the Cassandra schema when triggered by a received API request | ||||||
* | ||||||
* @param metadata the {@link Metadata} to extract Cassandra schema from | ||||||
*/ | ||||||
public void process(@NotNull Metadata metadata) | ||||||
public void processRequested(@NotNull Metadata metadata) | ||||||
{ | ||||||
process(metadata, reportingMetrics.startedRequest.metric); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Private method for converting and reporting the Cassandra schema | ||||||
* | ||||||
* @param metadata the {@link Metadata} to extract Cassandra schema from | ||||||
* @param started the {@link DeltaGauge} for the metric counting invocations | ||||||
*/ | ||||||
private void process(@NotNull Metadata metadata, | ||||||
@NotNull DeltaGauge started) | ||||||
{ | ||||||
String action = " reporting schema for cluster with identifiers " + identifiersProvider; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove the heading space. It should make the error message in the RuntimeException look a bit nicer too. |
||||||
LOGGER.info("Started" + action); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the value placeholder of logger to eval lazily. Update the other places too.
Suggested change
|
||||||
started.increment(); | ||||||
|
||||||
try (Emitter emitter = emitterFactory.emitter()) | ||||||
{ | ||||||
stream(metadata) | ||||||
.forEach(ThrowableUtils.consumer(emitter::emit)); | ||||||
Stopwatch stopwatch = Stopwatch.createStarted(); | ||||||
long counter = stream(metadata) | ||||||
.map(ThrowableUtils.function(emitter::emit)) | ||||||
.count(); | ||||||
|
||||||
reportingMetrics.durationMilliseconds.metric.update(stopwatch.elapsed(TimeUnit.MILLISECONDS)); | ||||||
reportingMetrics.sizeAspects.metric.update(counter); | ||||||
reportingMetrics.finishedSuccess.metric.increment(); | ||||||
LOGGER.info("Success" + action); | ||||||
} | ||||||
catch (Exception exception) | ||||||
{ | ||||||
throw new RuntimeException("Cannot extract schema for cluster " + identifiersProvider.cluster(), exception); | ||||||
reportingMetrics.finishedFailure.metric.increment(); | ||||||
LOGGER.error("Failure" + action); | ||||||
throw new RuntimeException(action, exception); | ||||||
} | ||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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.cassandra.sidecar.metrics.server; | ||
|
||
import com.codahale.metrics.Histogram; | ||
import com.codahale.metrics.MetricRegistry; | ||
import org.apache.cassandra.sidecar.metrics.DeltaGauge; | ||
import org.apache.cassandra.sidecar.metrics.NamedMetric; | ||
import org.apache.cassandra.sidecar.metrics.ServerMetrics; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Tracks metrics for the schema reporting done by Sidecar | ||
*/ | ||
public class SchemaReportingMetrics | ||
{ | ||
protected static final String DOMAIN = ServerMetrics.SERVER_PREFIX + ".SchemaReporting"; | ||
protected static final String STARTED = "Started"; | ||
protected static final String FINISHED = "Finished"; | ||
protected static final String DURATION = "Duration"; | ||
protected static final String SIZE = "Size"; | ||
protected static final String TRIGGER = "Trigger"; | ||
protected static final String RESULT = "Result"; | ||
protected static final String UNIT = "Unit"; | ||
protected static final String REQUEST = "Request"; | ||
protected static final String SCHEDULE = "Schedule"; | ||
protected static final String SUCCESS = "Success"; | ||
protected static final String FAILURE = "Failure"; | ||
protected static final String ASPECTS = "Aspects"; | ||
protected static final String MILLISECONDS = "Milliseconds"; | ||
|
||
public final NamedMetric<DeltaGauge> startedRequest; | ||
public final NamedMetric<DeltaGauge> startedSchedule; | ||
public final NamedMetric<DeltaGauge> finishedSuccess; | ||
public final NamedMetric<DeltaGauge> finishedFailure; | ||
public final NamedMetric<Histogram> durationMilliseconds; | ||
public final NamedMetric<Histogram> sizeAspects; | ||
Comment on lines
+33
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the patch to follow the other Metrics class? Let them be consistent. The string constants that are only used once can all be removed. |
||
|
||
public SchemaReportingMetrics(@NotNull MetricRegistry registry) | ||
{ | ||
startedSchedule = NamedMetric.builder(name -> registry.gauge(name, DeltaGauge::new)) | ||
.withDomain(DOMAIN) | ||
.withName(STARTED) | ||
.addTag(TRIGGER, SCHEDULE) | ||
.build(); | ||
startedRequest = NamedMetric.builder(name -> registry.gauge(name, DeltaGauge::new)) | ||
.withDomain(DOMAIN) | ||
.withName(STARTED) | ||
.addTag(TRIGGER, REQUEST) | ||
.build(); | ||
|
||
finishedSuccess = NamedMetric.builder(name -> registry.gauge(name, DeltaGauge::new)) | ||
.withDomain(DOMAIN) | ||
.withName(FINISHED) | ||
.addTag(RESULT, SUCCESS) | ||
.build(); | ||
finishedFailure = NamedMetric.builder(name -> registry.gauge(name, DeltaGauge::new)) | ||
.withDomain(DOMAIN) | ||
.withName(FINISHED) | ||
.addTag(RESULT, FAILURE) | ||
.build(); | ||
|
||
sizeAspects = NamedMetric.builder(registry::histogram) | ||
.withDomain(DOMAIN) | ||
.withName(SIZE) | ||
.addTag(UNIT, ASPECTS) | ||
.build(); | ||
|
||
durationMilliseconds = NamedMetric.builder(registry::histogram) | ||
.withDomain(DOMAIN) | ||
.withName(DURATION) | ||
.addTag(UNIT, MILLISECONDS) | ||
.build(); | ||
Comment on lines
+57
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer self-describing names over tags. For example, "StartedBySchedule" provides better readability than "Trigger=Schedule.Started". Note that all the tags are combined into this |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The toString is relatively complex. Is there a test?