1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical
21 
22 import cats.implicits._
23 import grizzled.slf4j.Logging
24 import kamon.instrumentation.http.{HttpMessage, HttpOperationNameGenerator}
25 import kamon.Kamon
26 import kamon.trace.Span
27 import org.make.api.technical.tracing.Tracing
28 import com.typesafe.config.ConfigFactory
29 
30 class MakeClientOperationNameGenerator extends HttpOperationNameGenerator with Logging {
31 
32   logger.info("Creating the Make name generator for akka-http")
33 
34   override def name(request: HttpMessage.Request): Option[String] = {
35     val spanName = Kamon.currentSpan().operationName()
36     val operationName =
37       if (spanName === Span.Empty.operationName() || Tracing.entrypoint.contains(spanName))
38         createOperationNameFromRequest(request)
39       else
40         spanName
41     Some(operationName)
42   }
43 
44   private val IgnoredPorts = Set(80, 443, 0)
45   private def createOperationNameFromRequest(request: HttpMessage.Request): String =
46     ElasticsearchSpanGeneration.generate(request).getOrElse {
47       val portString =
48         if (IgnoredPorts.contains(request.port)) ""
49         else s":${request.port}"
50       s"${request.host}$portString${request.path}"
51     }
52 }
53 
54 object ElasticsearchSpanGeneration {
55 
56   private val esConfig =
57     ConfigFactory
58       .load("default-application.conf")
59       .getConfig("make-api.elasticSearch")
60 
61   private val connectionString = esConfig.getString("connection-string")
62 
63   private val ideaAliasName = esConfig.getString("idea-alias-name")
64   private val proposalAliasName = esConfig.getString("proposal-alias-name")
65   private val organisationAliasName = esConfig.getString("organisation-alias-name")
66   private val operationOfQuestionAliasName = esConfig.getString("operation-of-question-alias-name")
67   private val postAliasName = esConfig.getString("post-alias-name")
68 
69   private val ESAliasesNames =
70     List(ideaAliasName, proposalAliasName, organisationAliasName, operationOfQuestionAliasName, postAliasName)
71 
72   def generate(request: HttpMessage.Request): Option[String] =
73     Option.when(connectionString.contains(request.host)) {
74       // ES query parameters are part of its path. We need to clean it up by only keeping the relevant
75       // part so as not to blow up the granularity of operation names.
76       "elasticsearch" ++ ESAliasesNames.find(request.path.contains).fold("")('-' +: _)
77     }
78 }
Line Stmt Id Pos Tree Symbol Tests Code
32 35056 1130 - 1191 Apply grizzled.slf4j.Logger.info org.make.api.technical.makeclientoperationnamegeneratortest MakeClientOperationNameGenerator.this.logger.info("Creating the Make name generator for akka-http")
35 48688 1282 - 1317 Apply kamon.trace.Span.operationName org.make.api.technical.makeclientoperationnamegeneratortest kamon.Kamon.currentSpan().operationName()
37 49706 1395 - 1432 Apply scala.Option.contains org.make.api.technical.makeclientoperationnamegeneratortest org.make.api.technical.tracing.Tracing.entrypoint.contains[String](spanName)
37 44265 1352 - 1352 Select cats.kernel.instances.StringInstances.catsKernelStdOrderForString org.make.api.technical.makeclientoperationnamegeneratortest cats.implicits.catsKernelStdOrderForString
37 41594 1352 - 1432 Apply scala.Boolean.|| org.make.api.technical.makeclientoperationnamegeneratortest cats.implicits.catsSyntaxEq[String](spanName)(cats.implicits.catsKernelStdOrderForString).===(kamon.trace.Span.Empty.operationName()).||(org.make.api.technical.tracing.Tracing.entrypoint.contains[String](spanName))
37 36429 1365 - 1391 Apply kamon.trace.Span.Empty.operationName org.make.api.technical.makeclientoperationnamegeneratortest kamon.trace.Span.Empty.operationName()
38 50803 1442 - 1481 Block org.make.api.technical.MakeClientOperationNameGenerator.createOperationNameFromRequest org.make.api.technical.makeclientoperationnamegeneratortest MakeClientOperationNameGenerator.this.createOperationNameFromRequest(request)
38 34335 1442 - 1481 Apply org.make.api.technical.MakeClientOperationNameGenerator.createOperationNameFromRequest org.make.api.technical.makeclientoperationnamegeneratortest MakeClientOperationNameGenerator.this.createOperationNameFromRequest(request)
40 43221 1501 - 1509 Ident org.make.api.technical.MakeClientOperationNameGenerator.spanName org.make.api.technical.makeclientoperationnamegeneratortest spanName
41 34814 1514 - 1533 Apply scala.Some.apply org.make.api.technical.makeclientoperationnamegeneratortest scala.Some.apply[String](operationName)
44 48124 1568 - 1583 Apply scala.collection.IterableFactory.apply org.make.api.technical.makeclientoperationnamegeneratortest scala.Predef.Set.apply[Int](80, 443, 0)
46 50840 1673 - 1895 Apply scala.Option.getOrElse org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.generate(request).getOrElse[String]({ val portString: String = if (MakeClientOperationNameGenerator.this.IgnoredPorts.contains(request.port)) "" else (":".+(request.port): String); ("".+(request.host).+(portString).+(request.path): String) })
48 41353 1803 - 1805 Block <nosymbol> org.make.api.technical.makeclientoperationnamegeneratortest ""
48 49200 1803 - 1805 Literal <nosymbol> org.make.api.technical.makeclientoperationnamegeneratortest ""
48 36465 1766 - 1801 Apply scala.collection.SetOps.contains org.make.api.technical.makeclientoperationnamegeneratortest MakeClientOperationNameGenerator.this.IgnoredPorts.contains(request.port)
48 44025 1788 - 1800 Select kamon.instrumentation.http.HttpMessage.Request.port org.make.api.technical.makeclientoperationnamegeneratortest request.port
49 33215 1819 - 1838 Typed <nosymbol> (":".+(request.port): String)
59 42981 1966 - 2062 Apply com.typesafe.config.Config.getConfig org.make.api.technical.makeclientoperationnamegeneratortest com.typesafe.config.ConfigFactory.load("default-application.conf").getConfig("make-api.elasticSearch")
61 34851 2097 - 2136 Apply com.typesafe.config.Config.getString org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.esConfig.getString("connection-string")
63 47883 2168 - 2205 Apply com.typesafe.config.Config.getString org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.esConfig.getString("idea-alias-name")
64 44754 2240 - 2281 Apply com.typesafe.config.Config.getString org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.esConfig.getString("proposal-alias-name")
65 36506 2320 - 2365 Apply com.typesafe.config.Config.getString org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.esConfig.getString("organisation-alias-name")
66 48972 2411 - 2465 Apply com.typesafe.config.Config.getString org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.esConfig.getString("operation-of-question-alias-name")
67 41391 2496 - 2533 Apply com.typesafe.config.Config.getString org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.esConfig.getString("post-alias-name")
70 34616 2632 - 2660 Select org.make.api.technical.ElasticsearchSpanGeneration.operationOfQuestionAliasName org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.operationOfQuestionAliasName
70 42472 2609 - 2630 Select org.make.api.technical.ElasticsearchSpanGeneration.organisationAliasName org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.organisationAliasName
70 51294 2590 - 2607 Select org.make.api.technical.ElasticsearchSpanGeneration.proposalAliasName org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.proposalAliasName
70 34289 2575 - 2588 Select org.make.api.technical.ElasticsearchSpanGeneration.ideaAliasName org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.ideaAliasName
70 39785 2570 - 2676 Apply scala.collection.IterableFactory.apply org.make.api.technical.makeclientoperationnamegeneratortest scala.`package`.List.apply[String](ElasticsearchSpanGeneration.this.ideaAliasName, ElasticsearchSpanGeneration.this.proposalAliasName, ElasticsearchSpanGeneration.this.organisationAliasName, ElasticsearchSpanGeneration.this.operationOfQuestionAliasName, ElasticsearchSpanGeneration.this.postAliasName)
70 48678 2662 - 2675 Select org.make.api.technical.ElasticsearchSpanGeneration.postAliasName org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.postAliasName
73 36948 2783 - 2795 Select kamon.instrumentation.http.HttpMessage.Request.host org.make.api.technical.makeclientoperationnamegeneratortest request.host
73 40862 2745 - 3066 Apply scala.Option.when org.make.api.technical.makeclientoperationnamegeneratortest scala.Option.when[String](ElasticsearchSpanGeneration.this.connectionString.contains(request.host))(scala.Predef.augmentString("elasticsearch").++(ElasticsearchSpanGeneration.this.ESAliasesNames.find({ <synthetic> val eta$0$1: String = request.path; ((x$1: CharSequence) => eta$0$1.contains(x$1)) }).fold[String]("")(((x$1: String) => { scala.Predef.augmentString(x$1).+:('-') }))))
73 49004 2757 - 2796 Apply java.lang.String.contains org.make.api.technical.makeclientoperationnamegeneratortest ElasticsearchSpanGeneration.this.connectionString.contains(request.host)
76 51336 3047 - 3049 Literal <nosymbol> ""
76 42932 3055 - 3059 Apply scala.collection.StringOps.+: scala.Predef.augmentString(x$1).+:('-')
76 34324 3019 - 3040 Apply java.lang.String.contains eta$0$1.contains(x$1)
76 48440 2980 - 3060 Apply scala.collection.StringOps.++ scala.Predef.augmentString("elasticsearch").++(ElasticsearchSpanGeneration.this.ESAliasesNames.find({ <synthetic> val eta$0$1: String = request.path; ((x$1: CharSequence) => eta$0$1.contains(x$1)) }).fold[String]("")(((x$1: String) => { scala.Predef.augmentString(x$1).+:('-') })))
76 41154 2980 - 2995 Literal <nosymbol> "elasticsearch"
76 34649 2999 - 3060 Apply scala.Option.fold ElasticsearchSpanGeneration.this.ESAliasesNames.find({ <synthetic> val eta$0$1: String = request.path; ((x$1: CharSequence) => eta$0$1.contains(x$1)) }).fold[String]("")(((x$1: String) => { scala.Predef.augmentString(x$1).+:('-') }))