mirror of https://github.com/grafana/grafana.git
				
				
				
			Loki Datasource: Don't escape single quotes (#98767)
* fix: remove single quote escape
This commit is contained in:
		
							parent
							
								
									f3d2313f09
								
							
						
					
					
						commit
						6568f3669d
					
				|  | @ -269,7 +269,7 @@ describe('LokiDatasource', () => { | ||||||
|         }, |         }, | ||||||
|       ]; |       ]; | ||||||
|       expect(ds.applyTemplateVariables(query, {}, adhocFilters).expr).toBe( |       expect(ds.applyTemplateVariables(query, {}, adhocFilters).expr).toBe( | ||||||
|         'rate({bar="baz", job="foo", k1=~"v.*", k2=~"v\\\\\'.*"} |= "bar" [5m])' |         `rate({bar="baz", job="foo", k1=~"v.*", k2=~"v'.*"} |= "bar" [5m])` | ||||||
|       ); |       ); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|  | @ -325,8 +325,15 @@ describe('LokiDatasource', () => { | ||||||
|       variable = {} as unknown as CustomVariableModel; |       variable = {} as unknown as CustomVariableModel; | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it('should only escape single quotes', () => { |     it('should not escape', () => { | ||||||
|       expect(ds.interpolateQueryExpr("abc'$^*{}[]+?.()|", variable)).toEqual("abc\\\\'$^*{}[]+?.()|"); |       expect(ds.interpolateQueryExpr("abc'$^*{}[]+?.()|", variable)).toEqual("abc'$^*{}[]+?.()|"); | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     it('should not escape single quotes in line filters', () => { | ||||||
|  |       expect(ds.interpolateQueryExpr("|= `abc'$^*{}[]+?.()|`", variable)).toEqual("|= `abc'$^*{}[]+?.()|`"); | ||||||
|  |       expect(ds.interpolateQueryExpr("|~ `abc'$^*{}[]+?.()|`", variable)).toEqual("|~ `abc'$^*{}[]+?.()|`"); | ||||||
|  |       expect(ds.interpolateQueryExpr("!= `abc'$^*{}[]+?.()|`", variable)).toEqual("!= `abc'$^*{}[]+?.()|`"); | ||||||
|  |       expect(ds.interpolateQueryExpr("!~ `abc'$^*{}[]+?.()|`", variable)).toEqual("!~ `abc'$^*{}[]+?.()|`"); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     it('should return a number', () => { |     it('should return a number', () => { | ||||||
|  |  | ||||||
|  | @ -814,7 +814,7 @@ export class LokiDatasource | ||||||
|   interpolateQueryExpr(value: any, variable: QueryVariableModel | CustomVariableModel) { |   interpolateQueryExpr(value: any, variable: QueryVariableModel | CustomVariableModel) { | ||||||
|     // if no multi or include all do not regexEscape
 |     // if no multi or include all do not regexEscape
 | ||||||
|     if (!variable.multi && !variable.includeAll) { |     if (!variable.multi && !variable.includeAll) { | ||||||
|       return lokiRegularEscape(value); |       return value; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (typeof value === 'string') { |     if (typeof value === 'string') { | ||||||
|  | @ -1097,13 +1097,8 @@ export class LokiDatasource | ||||||
|     expr = adhocFilters.reduce((acc: string, filter: { key: string; operator: string; value: string }) => { |     expr = adhocFilters.reduce((acc: string, filter: { key: string; operator: string; value: string }) => { | ||||||
|       const { key, operator } = filter; |       const { key, operator } = filter; | ||||||
|       let { value } = filter; |       let { value } = filter; | ||||||
|       if (isRegexSelector(operator)) { |       if (!isRegexSelector(operator)) { | ||||||
|         // Adhoc filters don't support multiselect, therefore if user selects regex operator
 |         // We want to escape special characters in value for non-regex selectors to match the same char in the log line as the user types in the input
 | ||||||
|         // we are going to consider value to be regex filter and use lokiRegularEscape
 |  | ||||||
|         // that does not escape regex special characters (e.g. .*test.* => .*test.*)
 |  | ||||||
|         value = lokiRegularEscape(value); |  | ||||||
|       } else { |  | ||||||
|         // Otherwise, we want to escape special characters in value
 |  | ||||||
|         value = escapeLabelValueInSelector(value, operator); |         value = escapeLabelValueInSelector(value, operator); | ||||||
|       } |       } | ||||||
|       return addLabelToQuery(acc, key, operator, value); |       return addLabelToQuery(acc, key, operator, value); | ||||||
|  | @ -1207,20 +1202,9 @@ export class LokiDatasource | ||||||
|     }; |     }; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // NOTE: these two functions are very similar to the escapeLabelValueIn* functions
 |  | ||||||
| // in language_utils.ts, but they are not exactly the same algorithm, and we found
 |  | ||||||
| // no way to reuse one in the another or vice versa.
 |  | ||||||
| export function lokiRegularEscape<T>(value: T) { |  | ||||||
|   if (typeof value === 'string') { |  | ||||||
|     return value.replace(/'/g, "\\\\'"); |  | ||||||
|   } |  | ||||||
|   return value; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export function lokiSpecialRegexEscape<T>(value: T) { | export function lokiSpecialRegexEscape<T>(value: T) { | ||||||
|   if (typeof value === 'string') { |   if (typeof value === 'string') { | ||||||
|     return lokiRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()|]/g, '\\\\$&')); |     return value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()|]/g, '\\\\$&'); | ||||||
|   } |   } | ||||||
|   return value; |   return value; | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue