-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add OpenTelemetry support; Improve logging (#100)
- Loading branch information
1 parent
018aaea
commit 71d77d5
Showing
48 changed files
with
1,014 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Keycloak.AuthServices.OpenTelemetry | ||
|
||
`Keycloak.AuthServices` can be instrumented via [OpenTelemetry](https://opentelemetry.io/docs/languages/net/getting-started/). | ||
|
||
You may ask, **"Why do I even need it?"** and you would be right. In most cases, logging is enough. However, since `Keycloak.AuthServices.Authorization` makes multiple outgoing requests to the Authorization Server, it was decided to add OpenTelemetry support to gain better insights into how the authorization process works. | ||
|
||
## Add to your code | ||
|
||
```bash | ||
dotnet add package Keycloak.AuthServices.OpenTelemetry | ||
``` | ||
|
||
Here is how to use it: | ||
|
||
```csharp | ||
var builder = WebApplication.CreateBuilder(args); | ||
var services = builder.Services; | ||
|
||
builder.Logging.AddOpenTelemetry(logging => | ||
{ | ||
logging.IncludeFormattedMessage = true; | ||
logging.IncludeScopes = true; | ||
}); | ||
|
||
services | ||
.AddOpenTelemetry() | ||
.WithMetrics(metrics => | ||
metrics | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddKeycloakAuthServicesInstrumentation() // [!code highlight] | ||
) | ||
.WithTracing(tracing => | ||
tracing | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddKeycloakAuthServicesInstrumentation() // [!code highlight] | ||
) | ||
.UseOtlpExporter(); | ||
``` | ||
|
||
## Metrics example | ||
|
||
```bash | ||
dotnet counters monitor \ | ||
--name ResourceAuthorization \ | ||
--counters Keycloak.AuthServices.Authorization | ||
``` | ||
|
||
Play around with an application and see the results: | ||
|
||
```text | ||
Press p to pause, r to resume, q to quit. | ||
Status: Running | ||
Name Current Value | ||
[Keycloak.AuthServices.Authorization] | ||
keycloak.authservices.requirements.fail (Count) | ||
requirement=ParameterizedProtectedResourceRequirement 3 | ||
keycloak.authservices.requirements.succeed (Count) | ||
requirement=ParameterizedProtectedResourceRequirement 5 | ||
requirement=RealmAccessRequirement 16 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Troubleshooting | ||
|
||
*Common issues:* | ||
|
||
[[toc]] | ||
|
||
## I receive 401 Unauthorized status code | ||
|
||
* [Turn on](/qa/recipes.html#how-to-debug-an-application) Debug or Trace logging level and see the logs output | ||
* Make sure access token is provided in Authorization Header. | ||
* Make sure the audience is mapped to a token via [audience mapper](/configuration/configuration-keycloak#add-audience-mapper). You can try to disable audience validation temporarily. | ||
* Make sure the HTTPs requirement is turned off in Development Mode. `KeycloakAuthenticationOptions.SslRequired="none"` | ||
|
||
## I receive 403 Forbidden | ||
|
||
* [Turn on](/qa/recipes.html#how-to-debug-an-application) Debug or Trace logging level and see the logs output | ||
* In case of RBAC Authorization make sure the `ClaimsPrincipal` has "realm_access" and "resource_access" claims mapped from token issued by Keycloak. | ||
* If you use Keycloak as Authorization Server, make sure it is properly configured and that the Keycloak installation is accessible. | ||
|
||
## Keycloak is slow to respond | ||
|
||
Keycloak is a central part of the system used by many components. Especially, in Authorization Server scenario where authorization requests are sent to centralized place. Essentially, Keycloak becomes a bottleneck of the system. Consider [Cluster Setup](https://www.keycloak.org/2019/05/keycloak-cluster-setup) to tackle this problem. | ||
|
||
Also, you can handle transient HTTP errors by adding resiliency, see [How to setup resiliency to HTTP Clients](/qa/recipes.html#how-to-setup-resiliency-to-http-clients) | ||
|
||
> [!NOTE] | ||
> ☝️`Keycloak.AuthServices` supports OpenTelemetry. See [Keycloak.AuthServices.OpenTelemetry](/opentelemetry). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.