-
Notifications
You must be signed in to change notification settings - Fork 2
[FEATURE] Add support for OpenTelemetry #19
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
Open
KostLinux
wants to merge
12
commits into
Matrix278:master
Choose a base branch
from
KostLinux:feature/add-otel-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
15b38ad
feature: replace util package with random
KostLinux 9fcdba7
Merge branch 'Matrix278:master' into master
KostLinux 90d2dd5
chore: add configurable ssl mode
KostLinux 4af9559
Merge branch 'Matrix278:master' into master
KostLinux b2b4425
Merge branch 'chore/add-dynamic-ssl-mode'
KostLinux f7cabaa
fix: conflicts from changes
KostLinux 222b8ee
fix: deprecated linter used
KostLinux ae1faf9
Merge branch 'Matrix278:master' into master
KostLinux e92fd0a
feature: add platform agnostic OTEL support
KostLinux 3298f78
fix: some idiom fixes for collector
KostLinux 448001b
feature: add possibility to disable otel
KostLinux ca963fb
feature: introduce otel in readme
KostLinux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| import http from 'k6/http'; | ||
| import { check, sleep } from 'k6'; | ||
| import { randomString } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js'; | ||
|
|
||
| export const options = { | ||
| stages: [ | ||
| { duration: '30s', target: 5 }, // Ramp up to 5 users | ||
| { duration: '1m', target: 5 }, // Stay at 5 users | ||
| { duration: '20s', target: 10 }, // Ramp up to 10 users | ||
| { duration: '1m', target: 10 }, // Stay at 10 users | ||
| { duration: '20s', target: 0 }, // Scale down to 0 users | ||
| ], | ||
| thresholds: { | ||
| http_req_duration: ['p(95)<2000'], // 95% of requests must complete below 2s | ||
| http_req_failed: ['rate<0.1'], // Less than 10% can fail | ||
| }, | ||
| }; | ||
|
|
||
| const BASE_URL = 'http://localhost:8080/api/v1'; | ||
|
|
||
| // Generates a random UUID v4 | ||
| function uuidv4() { | ||
| return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
| const r = Math.random() * 16 | 0; | ||
| const v = c == 'x' ? r : (r & 0x3 | 0x8); | ||
| return v.toString(16); | ||
| }); | ||
| } | ||
|
|
||
| export default function () { | ||
| const userId = uuidv4(); | ||
| const authToken = `Bearer ${randomString(32)}`; | ||
|
|
||
| // Add trace context headers | ||
| const headers = { | ||
| 'Authorization': authToken, | ||
| 'traceparent': `00-${randomString(32)}-${randomString(16)}-01`, | ||
| 'Content-Type': 'application/json', | ||
| }; | ||
|
|
||
| // Test user endpoint with trace context | ||
| const userResponse = http.get(`${BASE_URL}/users/${userId}`, { | ||
| headers: headers, | ||
| tags: { name: 'GetUserByID' }, | ||
| }); | ||
|
|
||
| // Check response and add custom span attributes via tags | ||
| check(userResponse, { | ||
| 'status is 401 or 422': (r) => r.status === 401 || r.status === 422, | ||
| 'response time OK': (r) => r.timings.duration < 2000, | ||
| }); | ||
|
|
||
| // Add some variation in the test pattern | ||
| if (Math.random() < 0.3) { | ||
| // Simulate slow requests occasionally | ||
| sleep(2); | ||
| } else { | ||
| sleep(1); | ||
| } | ||
|
|
||
| // Test invalid UUID to generate error traces | ||
| if (Math.random() < 0.1) { | ||
| const invalidResponse = http.get(`${BASE_URL}/users/invalid-uuid`, { | ||
| headers: headers, | ||
| tags: { name: 'GetUserByIDInvalid' }, | ||
| }); | ||
|
|
||
| check(invalidResponse, { | ||
| 'invalid uuid returns 400': (r) => r.status === 400, | ||
| }); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,56 @@ | ||
| package configuration | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| type Telemetry struct { | ||
| Enabled bool | ||
| Environment string | ||
| Endpoint string | ||
| Headers map[string]string | ||
| Insecure bool | ||
| QueueSize int | ||
| MaxExportBatchSize int | ||
| Compression string | ||
| ServiceName string | ||
| ServiceVersion string | ||
| } | ||
|
|
||
| // TelemetryNew creates a new Telemetry configuration from environment | ||
| func TelemetryNew() *Telemetry { | ||
| viper.SetDefault("OTEL_ENABLED", false) | ||
| viper.SetDefault("OTEL_SERVICE_NAME", "go-api-template") | ||
| viper.SetDefault("OTEL_SERVICE_VERSION", "1.0.0") | ||
| viper.SetDefault("OTEL_EXPORTER_OTLP_ENDPOINT", "localhost:4317") | ||
| viper.SetDefault("OTEL_INSECURE", true) | ||
| viper.SetDefault("OTEL_EXPORTER_OTLP_HEADERS", "") | ||
| viper.SetDefault("OTEL_EXPORTER_OTLP_QUEUE_SIZE", 4096) | ||
| viper.SetDefault("OTEL_EXPORTER_OTLP_MAX_EXPORT_BATCH_SIZE", 512) | ||
| viper.SetDefault("OTEL_EXPORTER_OTLP_COMPRESSION", "gzip") | ||
|
|
||
| headers := make(map[string]string) | ||
| if headerStr := viper.GetString("OTEL_EXPORTER_OTLP_HEADERS"); headerStr != "" { | ||
| for _, header := range strings.Split(headerStr, ",") { | ||
| parts := strings.SplitN(header, "=", 2) | ||
| if len(parts) == 2 { | ||
| headers[parts[0]] = parts[1] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return &Telemetry{ | ||
| Enabled: viper.GetBool("OTEL_ENABLED"), | ||
| ServiceName: viper.GetString("OTEL_SERVICE_NAME"), | ||
| ServiceVersion: viper.GetString("OTEL_SERVICE_VERSION"), | ||
| Environment: viper.GetString("APP_ENV"), | ||
| Endpoint: viper.GetString("OTEL_EXPORTER_OTLP_ENDPOINT"), | ||
| Headers: headers, | ||
| Insecure: viper.GetBool("OTEL_INSECURE"), | ||
| QueueSize: viper.GetInt("OTEL_EXPORTER_OTLP_QUEUE_SIZE"), | ||
| MaxExportBatchSize: viper.GetInt("OTEL_EXPORTER_OTLP_MAX_EXPORT_BATCH_SIZE"), | ||
| Compression: viper.GetString("OTEL_EXPORTER_OTLP_COMPRESSION"), | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ import ( | |
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/go-openapi/strfmt" | ||
| "go.opentelemetry.io/otel" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| type IUser interface { | ||
|
|
@@ -14,11 +16,13 @@ type IUser interface { | |
|
|
||
| type user struct { | ||
| service service.IUser | ||
| tracer trace.Tracer | ||
| } | ||
|
|
||
| func NewUser(service service.IUser) IUser { | ||
| return &user{ | ||
| service: service, | ||
| tracer: otel.Tracer("controller/user"), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -36,15 +40,19 @@ func NewUser(service service.IUser) IUser { | |
| // @Failure 500 {object} model.InternalErrorResponse | ||
| // @Router /users/{user_id} [get] | ||
| func (controller *user) UserByID(ctx *gin.Context) { | ||
| // Validate path params | ||
| spanCtx, span := controller.tracer.Start(ctx.Request.Context(), "UserByID") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets just call the |
||
| defer span.End() | ||
|
|
||
| userID := ctx.Param("user_id") | ||
| if !strfmt.IsUUID4(userID) { | ||
| span.RecordError(commonerrors.ErrInvalidUserID) | ||
| StatusBadRequest(ctx, commonerrors.ErrInvalidUserID) | ||
| return | ||
| } | ||
|
|
||
| response, err := controller.service.UserByID(ctx, strfmt.UUID4(userID)) | ||
| response, err := controller.service.UserByID(spanCtx, strfmt.UUID4(userID)) | ||
| if err != nil { | ||
| span.RecordError(err) | ||
| HandleCommonErrors(ctx, err) | ||
| return | ||
| } | ||
|
|
||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets call it
NewTelemetry()orNewOpenTelemetry()even