Skip to content

Commit 28722d9

Browse files
author
Mattia Moretti
authored
Multi region cdscrs 35591 (#264)
* multi region and improvs * working * tags are out * tags disabled for now * cleanup * cache assertions fixes * label improvements * babel 7.23.2 * minimal clean * ready * uid * docs * sidenote
1 parent fceccb5 commit 28722d9

File tree

10 files changed

+317
-149
lines changed

10 files changed

+317
-149
lines changed
21.8 KB
Loading

docs/using.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ If you are using a Data Source configured in **multitenancy** mode then choose t
5959

6060
![Metrics Query Editor - multitenancy mode](images/novar-multi.png)
6161

62+
If your tenancy is subscribed to more than one region and you want to query the same metric in all subscribed regions, you can use the **all-subscribed-region** option as in the following example:
63+
64+
![All Subscribed Regions](images/all-subscribed-region.png)
65+
66+
Please note that querying using **all-subscribed-region** option can take significantly more time than querying only one region, depending on the number of the subscribed regions.
6267

6368
Click the save icon to save your graph.
6469

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
},
2323
"engineStrict": true,
2424
"devDependencies": {
25-
"@babel/core": "^7.16.7",
2625
"@grafana/e2e": "9.3.8",
2726
"@grafana/e2e-selectors": "9.3.8",
2827
"@grafana/eslint-config": "^5.1.0",
@@ -39,6 +38,7 @@
3938
"@types/jest": "^29.2.2",
4039
"@types/node": "^18.11.9",
4140
"babel": "^6.23.0",
41+
"@babel/core": "^7.23.2",
4242
"babel-preset-react": "^6.24.1",
4343
"copy-webpack-plugin": "^11.0.0",
4444
"css-loader": "^6.7.1",

pkg/plugin/metrics_functions.go

Lines changed: 43 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,13 @@ func (o *OCIDatasource) GetNamespaceWithMetricNames(
346346
// fetching from cache, if present
347347
cacheKey := strings.Join([]string{tenancyOCID, compartmentOCID, region, "nss"}, "-")
348348
if cachedMetricNamesWithNamespaces, found := o.cache.Get(cacheKey); found {
349-
backend.Logger.Warn("client", "GetNamespaceWithMetricNames", "getting the data from cache")
350-
return cachedMetricNamesWithNamespaces.([]models.OCIMetricNamesWithNamespace)
349+
// This check avoids the type assertion and potential panic
350+
if _, ok := cachedMetricNamesWithNamespaces.([]models.OCIMetricNamesWithNamespace); ok {
351+
backend.Logger.Warn("client", "GetNamespaceWithMetricNames", "getting the data from cache")
352+
return cachedMetricNamesWithNamespaces.([]models.OCIMetricNamesWithNamespace)
353+
} else {
354+
backend.Logger.Warn("client.utils", "GetNamespaceWithMetricNames", "cannot use cached data -> "+cacheKey)
355+
}
351356
}
352357

353358
// calling the api if not present in cache
@@ -499,17 +504,18 @@ func (o *OCIDatasource) GetMetricDataPoints(ctx context.Context, requestParams m
499504
// fetching the resource labels
500505
var rl map[string]map[string]string
501506

502-
cachedResourceLabels := o.fetchFromCache(
503-
ctx,
504-
requestParams.TenancyOCID,
505-
requestParams.CompartmentOCID,
506-
requestParams.CompartmentName,
507-
sRegion,
508-
requestParams.Namespace,
509-
"resource_labels",
510-
)
507+
// Tags will be used in future releases
508+
// cachedResourceLabels := o.fetchFromCache(
509+
// ctx,
510+
// requestParams.TenancyOCID,
511+
// requestParams.CompartmentOCID,
512+
// requestParams.CompartmentName,
513+
// sRegion,
514+
// requestParams.Namespace,
515+
// "resource_labels",
516+
// )
511517

512-
rl = cachedResourceLabels.(map[string]map[string]string)
518+
// rl = cachedResourceLabels.(map[string]map[string]string)
513519

514520
// storing the data to calculate later
515521
allRegionsMetricsDataPoint.Store(sRegion, metricDataBank{
@@ -536,27 +542,28 @@ func (o *OCIDatasource) GetMetricDataPoints(ctx context.Context, requestParams m
536542

537543
backend.Logger.Info("client", "GetMetricDataPoints", "Metric datapoints got for region-"+regionInUse)
538544

545+
// Tags will be used in future releases
539546
// get the selected tags
540-
if len(selectedTags) != 0 {
541-
cachedResourceNamesPerTag := o.fetchFromCache(
542-
ctx,
543-
requestParams.TenancyOCID,
544-
requestParams.CompartmentOCID,
545-
requestParams.CompartmentName,
546-
regionInUse,
547-
requestParams.Namespace,
548-
constants.CACHE_KEY_RESOURCE_IDS_PER_TAG,
549-
)
550-
551-
resourceIDsPerTag = cachedResourceNamesPerTag.(map[string]map[string]struct{})
552-
}
547+
// if len(selectedTags) != 0 {
548+
// cachedResourceNamesPerTag := o.fetchFromCache(
549+
// ctx,
550+
// requestParams.TenancyOCID,
551+
// requestParams.CompartmentOCID,
552+
// requestParams.CompartmentName,
553+
// regionInUse,
554+
// requestParams.Namespace,
555+
// constants.CACHE_KEY_RESOURCE_IDS_PER_TAG,
556+
// )
557+
558+
// resourceIDsPerTag = cachedResourceNamesPerTag.(map[string]map[string]struct{})
559+
// }
553560

554561
metricData := value.(metricDataBank)
555562

556563
for _, metricDataItem := range metricData.dataPoints {
557564
found := false
558565

559-
uniqueDataID, resourceDisplayName, extraUniqueID, rIDPresent := getUniqueIdsForLabels(requestParams.Namespace, metricDataItem.Dimensions, requestParams.QueryText)
566+
uniqueDataID, dimensionKey, resourceDisplayName, extraUniqueID, rIDPresent := getUniqueIdsForLabels(requestParams.Namespace, metricDataItem.Dimensions, requestParams.QueryText)
560567

561568
if rIDPresent {
562569
for _, selectedTag := range selectedTags {
@@ -610,10 +617,6 @@ func (o *OCIDatasource) GetMetricDataPoints(ctx context.Context, requestParams m
610617
// for base tenancy
611618
splits := strings.Split(tenancyOCID, "/")
612619
tenancyName := splits[0]
613-
// tenancyName := oc.tenanciesMap[requestParams.TenancyOCID]
614-
// if tenancyName == constants.DEFAULT_PROFILE {
615-
// tenancyName = oc.baseTenancyName
616-
// }
617620

618621
// to get the resource labels
619622
labelKey := uniqueDataID + extraUniqueID
@@ -640,6 +643,7 @@ func (o *OCIDatasource) GetMetricDataPoints(ctx context.Context, requestParams m
640643
MetricName: *metricDataItem.Name,
641644
ResourceName: resourceDisplayName,
642645
UniqueDataID: uniqueDataID,
646+
DimensionKey: dimensionKey,
643647
Labels: labelsToAdd,
644648
}
645649
}
@@ -657,10 +661,6 @@ func (o *OCIDatasource) GetMetricDataPoints(ctx context.Context, requestParams m
657661
continue
658662
}
659663

660-
// for i := 0; i < resourcesFetched-len(dvs); i++ {
661-
// dataValuesWithTime[t] = append(dataValuesWithTime[t], 0.0)
662-
// }
663-
664664
lastValue := dataValuesWithTime[t][len(dataValuesWithTime[t])-1]
665665
for i := 0; i < resourcesFetched-len(dvs); i++ {
666666
dataValuesWithTime[t] = append(dataValuesWithTime[t], lastValue)
@@ -702,7 +702,6 @@ func (o *OCIDatasource) fetchFromCache(ctx context.Context, tenancyOCID string,
702702
backend.Logger.Error("client", "fetchFromCache", "fetching from cache")
703703

704704
labelCacheKey := strings.Join([]string{tenancyOCID, compartmentOCID, region, namespace, suffix}, "-")
705-
706705
if _, found := o.cache.Get(labelCacheKey); !found {
707706
o.GetTags(ctx, tenancyOCID, compartmentOCID, compartmentName, region, namespace)
708707
}
@@ -1010,27 +1009,9 @@ func (o *OCIDatasource) GetResourceGroups(
10101009
)
10111010
}
10121011

1013-
reqDetails := monitoring.ListMetricsDetails{}
1014-
reqDetails.Namespace = common.String(namespace)
1015-
reqDetails.GroupBy = []string{"resourceGroup", "name"}
1016-
1017-
// retrieve metric list for template var
1018-
items, err := o.getListMetrics(ctx, region, compartmentOCID, reqDetails, takey)
1019-
if err != nil {
1020-
backend.Logger.Error("client", "GetResourceGroups", "Error retrieving metric list under compartment '"+compartmentOCID+"'")
1021-
return nil
1022-
}
1023-
10241012
if len(metricResourceGroups) == 0 {
1025-
var arca []string
1026-
for _, item := range items {
1027-
alfa := *(item.Name)
1028-
arca = append(arca, alfa)
1029-
}
1030-
metricResourceGroupsList = append(metricResourceGroupsList, models.OCIMetricNamesWithResourceGroup{
1031-
ResourceGroup: constants.DEFAULT_RESOURCE_GROUP,
1032-
MetricNames: arca,
1033-
})
1013+
backend.Logger.Error("client", "GetResourceGroups", "resource groups under compartment '"+compartmentOCID+"' for namespace '"+namespace+"' is empty")
1014+
return nil
10341015
} else {
10351016
for k, v := range metricResourceGroups {
10361017
metricResourceGroupsList = append(metricResourceGroupsList, models.OCIMetricNamesWithResourceGroup{
@@ -1078,8 +1059,13 @@ func (o *OCIDatasource) GetDimensions(
10781059
// fetching from cache, if present
10791060
cacheKey := strings.Join([]string{tenancyOCID, compartmentOCID, region, namespace, metricName, cacheSubKey}, "-")
10801061
if cachedDimensions, found := o.cache.Get(cacheKey); found {
1081-
backend.Logger.Warn("client", "GetDimensions", "getting the data from cache")
1082-
return cachedDimensions.([]models.OCIMetricDimensions)
1062+
// This check avoids the type assertion and potential panic
1063+
if _, ok := cachedDimensions.([]models.OCIMetricDimensions); ok {
1064+
backend.Logger.Warn("client", "GetDimensions", "getting the data from cache")
1065+
return cachedDimensions.([]models.OCIMetricDimensions)
1066+
} else {
1067+
backend.Logger.Warn("client.utils", "GetDimensions", "cannot use cached data -> "+cacheKey)
1068+
}
10831069
}
10841070

10851071
var metricDimensions map[string][]string
@@ -1143,32 +1129,3 @@ func (o *OCIDatasource) GetDimensions(
11431129

11441130
return metricDimensionsList
11451131
}
1146-
1147-
func (o *OCIDatasource) getListMetrics(ctx context.Context, region, compartment string, metricDetails monitoring.ListMetricsDetails, takey string) ([]monitoring.Metric, error) {
1148-
var items []monitoring.Metric
1149-
var page *string
1150-
1151-
pageNumber := 0
1152-
for {
1153-
reg := common.StringToRegion(region)
1154-
o.tenancyAccess[takey].monitoringClient.SetRegion(string(reg))
1155-
res, err := o.tenancyAccess[takey].monitoringClient.ListMetrics(ctx, monitoring.ListMetricsRequest{
1156-
CompartmentId: common.String(compartment),
1157-
ListMetricsDetails: metricDetails,
1158-
Page: page,
1159-
})
1160-
1161-
if err != nil {
1162-
return nil, errors.Wrap(err, "list metrics failed")
1163-
}
1164-
items = append(items, res.Items...)
1165-
// Only 0 - n-1 pages are to be fetched, as indexing starts from 0 (for page number
1166-
if res.OpcNextPage == nil || pageNumber >= MaxPagesToFetch {
1167-
break
1168-
}
1169-
1170-
page = res.OpcNextPage
1171-
pageNumber++
1172-
}
1173-
return items, nil
1174-
}

pkg/plugin/models/oci.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type OCIMetricDataPoints struct {
3737
MetricName string
3838
ResourceName string
3939
UniqueDataID string
40+
DimensionKey string
4041
DataPoints []float64
4142
Labels map[string]string
4243
}

pkg/plugin/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (ocidx *OCIDatasource) query(ctx context.Context, pCtx backend.PluginContex
105105
// Assign the values slice to the map key
106106
OriginalDimensionMap[key] = values
107107
}
108-
name = ocidx.generateCustomMetricLabel(metricsDataRequest.LegendFormat, metricDataValue.MetricName, OriginalDimensionMap, metricDataValue.UniqueDataID)
108+
name = ocidx.generateCustomMetricLabel(metricsDataRequest.LegendFormat, metricDataValue.MetricName, OriginalDimensionMap, metricDataValue.UniqueDataID, metricDataValue.DimensionKey)
109109
if name == "" {
110110
ocidx.logger.Error("No valid resourceID found in dimensions", "metricDataValue.name", name)
111111
name = metricDataValue.UniqueDataID

0 commit comments

Comments
 (0)