ApplicationInsights-Java icon indicating copy to clipboard operation
ApplicationInsights-Java copied to clipboard

Customize Autocollected dependencies

Open anumalasri opened this issue 2 years ago • 2 comments

We are trying to customize auto collected data (requests, depenedencies). We are able to customize Request data using available SDK methods.

Auto-collected dependencies for HTTP calls are getting dependency name as HTTP: <HTTPMETHOD> <BASEHOSTNAME>/<URI> and we would like to customize

We looked into opentelemetry span processor, but looks complex for our usecase. SDK might be easier option.

Is there any way to access current Dependency using SDK and customize existing attributes?

  • SDK Version: SDK v2.6.4, Java Agent v3.2.4
  • OS type and version: AppService Linux
  • Application Server type and version (if applicable):
  • Using spring-boot? Yes. Spring Boot v2.5.8
  • Additional relevant libraries (with version, if applicable):

anumalasri avatar Jan 20 '22 01:01 anumalasri

hi @anumalasri! can you give a concrete example of a current dependency name, and what you would like to change it to? thx!

trask avatar Jan 20 '22 04:01 trask

Hi @trask ..

As per Application Insights data, we could see Dependency Name coming as HTTP: <HTTPMETHOD> <BASEHOSTNAME>/<URI>

We are using REST Services both in & out of our application i.e.most of URIs have data elements.

With this, even though we are calling same REST service, each individual URI listed as separate dependency and our metrics are not aligned correctly. Hence, We would like to update dependency name with 'ServiceName', so that all URIs related to that service will be identified as same dependency.

For example:

We have service name called GetUserDetails and URI is: /users/{username}

Current Dependency Details:

Dependency Name Duration Count
GET serviceHostName/users/user1 1.5ms 20
GET serviceHostName/users/user2 1.2ms 23

We would like to see metrics as below, by consolidating all URIs into single dependency.

Dependency Name Duration Count
GetUserDetails 1.3ms 43

What we tried so far:

  • We tried to update attributes using Span.current().setAttribute('attrName', 'attrValue'). However, this attribute is attached to request, but not dependency. This might be due to depedency is identified once we make outgoing call.

  • Tried to use Span Processor in applicationinsights.json. However, it does not as we have very little documentation on how to setup using different attributes. Also it would be tedious to handle multiple URIs using this method.

"processors": [
      {
        "type": "span",
        "include": {
          "matchType": "regexp",
          "spanNames": [
            "^(.*hostname*?)/(.*?)$"
          ]
        },
        
        "name": {
          "fromAttributes": 
             [
              "attrName"
            ]
          
        }
      }
    ]

Let me know if you need any further details.

anumalasri avatar Jan 20 '22 14:01 anumalasri

hi @anumalasri, sorry for the super late reply, this is a tricky question, and there isn't the best support for it available at this time. there is some work going on in the upstream OpenTelemetry project which should make this easier in the future.

in the meantime, I thought of a way you should be able to accomplish this today using the inherited attributes feature: https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#inherited-attribute-preview

e.g.

{
  "inheritedAttributes": [
    {
      "key": "dependency_name_override",
      "type": "string"
    }
  ]
}

(the dependency_name_override name is arbitrary)

and then in your code, stamp the dependency_name_override onto the parent (request) span before making the outgoing http call, e.g.

Span span = Span.current();
span.setAttribute("dependency_name_override", "GetUserDetails");
try {
    ... make outgoing http call ...
} finally {
    // reset it so that future dependency names do not get overwritten
    // there's no reliable way to remove an attribute, see
    // https://javadoc.io/doc/io.opentelemetry/opentelemetry-api/latest/io/opentelemetry/api/trace/Span.html
    span.setAttribute("dependency_name_override", "none_marker");
}

and then add a span processor, e.g.

{
  "preview": {
    "processors": [
      {
        "type": "span",
        "include": {
          "matchType": "strict",
          "attributes": [
            {
              "key": "dependency_name_override"
            }
          ]
        },
        "exclude": {
          "matchType": "strict",
          "attributes": [
            {
              "key": "dependency_name_override",
              "value": "none_marker"
            }
          ]
        },
        "name": {
          "fromAttributes": [
            "dependency_name_override"
          ]
        }
      }
    ]
  }
}

trask avatar Nov 05 '22 23:11 trask

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 days of this comment.

ghost avatar Nov 13 '22 02:11 ghost