Skip to content
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

chore(web-api): separate the telemetry-configuration into aspnet utils library #1341

Merged
merged 2 commits into from
Oct 24, 2024

Conversation

arealmaas
Copy link
Collaborator

@arealmaas arealmaas commented Oct 24, 2024

Description

As a step to configure telemetry in service and graphql, separating the configuration into the aspnet-project

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

Summary by CodeRabbit

  • New Features

    • Introduced a new method for configuring telemetry in the application, enhancing observability through OpenTelemetry for tracing and metrics.
    • Added support for Azure Monitor and Application Insights based on environment settings.
  • Bug Fixes

    • Removed outdated telemetry configuration that could lead to inconsistencies in monitoring.
  • Chores

    • Updated project dependencies to include necessary telemetry packages for improved performance and monitoring capabilities.

@arealmaas arealmaas requested a review from a team as a code owner October 24, 2024 10:31
Copy link
Contributor

coderabbitai bot commented Oct 24, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request involves the removal of the ConfigureTelemetry method from the WebApplicationBuilderExtensions class and the associated telemetry package references from the Digdir.Domain.Dialogporten.WebApi.csproj file. Simultaneously, a new ConfigureTelemetry method is added to the AspNetUtilitiesExtensions class, along with new package references in the Digdir.Library.Utils.AspNet.csproj file. This restructuring aims to centralize telemetry configuration within the utility library while removing redundant dependencies from the main web API project.

Changes

File Path Change Summary
src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs Method removed: ConfigureTelemetry for WebApplicationBuilder.
src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj Package references removed: Azure.Monitor.OpenTelemetry.AspNetCore, Microsoft.ApplicationInsights.AspNetCore, Npgsql.OpenTelemetry, OpenTelemetry.Instrumentation.Runtime, OpenTelemetry.
src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs Method added: ConfigureTelemetry for WebApplicationBuilder.
src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj Package references added: Azure.Monitor.OpenTelemetry.AspNetCore, Microsoft.ApplicationInsights.AspNetCore, Npgsql.OpenTelemetry, OpenTelemetry, OpenTelemetry.Instrumentation.Runtime.

Possibly related PRs

Suggested reviewers

  • knuhau
  • oskogstad

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs (1)

57-61: Specify a sampling strategy for the production environment

Currently, AlwaysOnSampler() is set for the development environment (lines 57~ to 60~), but no sampler is specified for the production environment. This means the default sampler will be used in production, which may not align with your desired telemetry data collection strategy. To control the volume of telemetry data and manage costs, consider specifying an appropriate sampler for production.

Here is a suggested addition:

 if (builder.Environment.IsDevelopment())
 {
     tracing.SetSampler(new AlwaysOnSampler());
 }
+else
+{
+    // Set a sampler for production, e.g.,
+    tracing.SetSampler(new TraceIdRatioBasedSampler(0.1)); // Adjust the sampling rate as needed
+}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d8ca865 and 2cb69a1.

📒 Files selected for processing (4)
  • src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs (0 hunks)
  • src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj (0 hunks)
  • src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs (2 hunks)
  • src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj (1 hunks)
💤 Files with no reviewable changes (2)
  • src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs
  • src/Digdir.Domain.Dialogporten.WebApi/Digdir.Domain.Dialogporten.WebApi.csproj
🔇 Additional comments (3)
src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj (2)

10-11: LGTM! Package selection aligns with the PR objectives.

The addition of these telemetry packages aligns well with the PR's objective of separating telemetry configuration into the ASP.NET utils library. The combination provides comprehensive monitoring capabilities:

  • Azure Monitor and Application Insights for cloud monitoring
  • OpenTelemetry for standardized telemetry
  • PostgreSQL instrumentation for database monitoring
  • Runtime instrumentation for system metrics

Also applies to: 15-17


10-11: Verify package versions and compatibility.

Let's ensure the selected package versions are appropriate and compatible:

Also applies to: 15-17

✅ Verification successful

Let me try a different approach to verify the package versions and their usage in the codebase.


Let's check the actual usage of these packages to ensure they are properly integrated and not conflicting.


Let's check the content of the dependent projects to ensure there's no duplication or conflicts in telemetry configuration.


Based on the comprehensive analysis of the codebase, I can now provide a final response regarding the telemetry package versions and their integration.

The telemetry package versions are appropriate and well-integrated.

The implementation shows a well-structured approach where:

  • The WebApi and Service projects properly utilize the centralized telemetry configuration from Utils.AspNet
  • The GraphQL project has a custom ApplicationInsights integration for GraphQL-specific telemetry needs
  • There's no duplication of telemetry configuration across projects
  • The package versions are used consistently:
    • Azure.Monitor.OpenTelemetry.AspNetCore (1.2.0)
    • Microsoft.ApplicationInsights.AspNetCore (2.22.0)
    • OpenTelemetry packages (1.9.0)

The configuration supports both local development (Application Insights SDK) and production (Azure Monitor) scenarios with proper fallback mechanisms.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for latest stable versions and known vulnerabilities

# Test 1: Check if these are the latest stable versions
echo "Checking latest versions from NuGet..."
for package in \
    "Azure.Monitor.OpenTelemetry.AspNetCore" \
    "Microsoft.ApplicationInsights.AspNetCore" \
    "Npgsql.OpenTelemetry" \
    "OpenTelemetry" \
    "OpenTelemetry.Instrumentation.Runtime"; do
    curl -s "https://api.nuget.org/v3-flatcontainer/$package/index.json" | jq -r '.versions[]' | sort -V | tail -n 1
done

# Test 2: Look for version constraints in other project files
echo "Checking for version constraints in other projects..."
fd -e csproj -x grep -l "OpenTelemetry"

Length of output: 2170


Script:

#!/bin/bash
# Let's examine the content of the csproj file to understand the full context
echo "=== Content of Digdir.Library.Utils.AspNet.csproj ==="
cat ./src/Digdir.Library.Utils.AspNet/Digdir.Library.Utils.AspNet.csproj

# Let's also check if there are any other projects that might be affected
echo -e "\n=== Projects referencing Digdir.Library.Utils.AspNet ==="
fd -e csproj -x grep -l "Digdir.Library.Utils.AspNet"

# Let's also check for any OpenTelemetry usage in the code
echo -e "\n=== Files using OpenTelemetry ===="
rg "using.*OpenTelemetry" -l

Length of output: 1839


Script:

#!/bin/bash
# Let's examine the telemetry configuration in the AspNetUtilitiesExtensions
echo "=== Telemetry configuration in AspNetUtilitiesExtensions.cs ==="
cat src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs

# Let's also check if there are any other telemetry configurations in the dependent projects
echo -e "\n=== Telemetry configurations in dependent projects ==="
rg -l "ApplicationInsights|OpenTelemetry" ./src/Digdir.Domain.Dialogporten.WebApi/ ./src/Digdir.Domain.Dialogporten.GraphQL/ ./src/Digdir.Domain.Dialogporten.Service/

Length of output: 4467


Script:

#!/bin/bash
# Check the Program.cs files in dependent projects
echo "=== WebApi Program.cs ==="
cat ./src/Digdir.Domain.Dialogporten.WebApi/Program.cs

echo -e "\n=== GraphQL Program.cs ==="
cat ./src/Digdir.Domain.Dialogporten.GraphQL/Program.cs

echo -e "\n=== Service Program.cs ==="
cat ./src/Digdir.Domain.Dialogporten.Service/Program.cs

# Check if there are any other telemetry-related configurations
echo -e "\n=== ApplicationInsightEventListener.cs ==="
cat ./src/Digdir.Domain.Dialogporten.GraphQL/ApplicationInsightEventListener.cs

Length of output: 20911

src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs (1)

76-79: ⚠️ Potential issue

Avoid multiple calls to AddOpenTelemetry() to prevent configuration conflicts

In line 52~, builder.Services.AddOpenTelemetry() is already called to configure OpenTelemetry services. Calling AddOpenTelemetry() again in line 78~ may lead to configuration conflicts or duplicated telemetry data. To ensure a consistent telemetry setup, consider integrating UseAzureMonitor() into the existing OpenTelemetry configuration without calling AddOpenTelemetry() again.

⛔ Skipped due to learnings
Learnt from: knuhau
PR: digdir/dialogporten#1202
File: src/Digdir.Domain.Dialogporten.WebApi/Program.cs:159-162
Timestamp: 2024-10-18T09:54:43.808Z
Learning: Calling `AddOpenTelemetry()` multiple times is safe and does not cause duplicate service registrations.

Copy link

@arealmaas arealmaas merged commit fa3157a into main Oct 24, 2024
23 checks passed
@arealmaas arealmaas deleted the chore/separate-telemetry-configuration branch October 24, 2024 13:13
arealmaas added a commit that referenced this pull request Oct 29, 2024
<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

Configures opentelemetry for Service. Copy pasta from WebAPI.

Dependent on #1341

## Related Issue(s)

- #1262

## Verification

- [ ] **Your** code builds clean without any errors or warnings
- [ ] Manual testing done (required)
- [ ] Relevant automated test added (if you find this hard, leave it and
we'll help out)

## Documentation

- [ ] Documentation is updated (either in `docs`-directory, Altinnpedia
or a separate linked PR in
[altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if
applicable)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced logging and telemetry configuration during application
startup.
	- Introduced a modular approach to configuring telemetry settings.

- **Bug Fixes**
- Improved error handling during application startup to ensure
exceptions are logged appropriately.

- **Refactor**
- Updated method signature for improved telemetry configuration
handling.
- Removed direct call to Application Insights, streamlining telemetry
integration.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants