forkjo/modules/opentelemetry/resource_test.go
TheFox0x7 c738542201 Open telemetry integration (#3972)
This PR adds opentelemetry and chi wrapper to have basic instrumentation

<!--start release-notes-assistant-->

## Draft release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/3972): <!--number 3972 --><!--line 0 --><!--description YWRkIHN1cHBvcnQgZm9yIGJhc2ljIHJlcXVlc3QgdHJhY2luZyB3aXRoIG9wZW50ZWxlbWV0cnk=-->add support for basic request tracing with opentelemetry<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3972
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
2024-08-05 06:04:39 +00:00

73 lines
2.4 KiB
Go

// Copyright 2024 TheFox0x7. All rights reserved.
// SPDX-License-Identifier: EUPL-1.2
package opentelemetry
import (
"context"
"slices"
"testing"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
)
func TestResourceServiceName(t *testing.T) {
ctx := context.Background()
resource, err := newResource(ctx)
require.NoError(t, err)
serviceKeyIdx := slices.IndexFunc(resource.Attributes(), func(v attribute.KeyValue) bool {
return v.Key == semconv.ServiceNameKey
})
require.NotEqual(t, -1, serviceKeyIdx)
assert.Equal(t, "forgejo", resource.Attributes()[serviceKeyIdx].Value.AsString())
defer test.MockVariableValue(&setting.OpenTelemetry.ServiceName, "non-default value")()
resource, err = newResource(ctx)
require.NoError(t, err)
serviceKeyIdx = slices.IndexFunc(resource.Attributes(), func(v attribute.KeyValue) bool {
return v.Key == semconv.ServiceNameKey
})
require.NotEqual(t, -1, serviceKeyIdx)
assert.Equal(t, "non-default value", resource.Attributes()[serviceKeyIdx].Value.AsString())
}
func TestResourceAttributes(t *testing.T) {
ctx := context.Background()
defer test.MockVariableValue(&setting.OpenTelemetry.ResourceDetectors, "foo")()
defer test.MockVariableValue(&setting.OpenTelemetry.ResourceAttributes, "Test=LABEL,broken,unescape=%XXlabel")()
res, err := newResource(ctx)
require.NoError(t, err)
expected, err := resource.New(ctx, resource.WithAttributes(
semconv.ServiceName(setting.OpenTelemetry.ServiceName),
semconv.ServiceVersion(setting.ForgejoVersion),
attribute.String("Test", "LABEL"),
attribute.String("unescape", "%XXlabel"),
))
require.NoError(t, err)
assert.Equal(t, expected, res)
}
func TestDecoderParity(t *testing.T) {
ctx := context.Background()
defer test.MockVariableValue(&setting.OpenTelemetry.ResourceDetectors, "sdk,process,os,host")()
exp, err := resource.New(
ctx, resource.WithTelemetrySDK(), resource.WithOS(), resource.WithProcess(), resource.WithHost(), resource.WithAttributes(
semconv.ServiceName(setting.OpenTelemetry.ServiceName), semconv.ServiceVersion(setting.ForgejoVersion),
),
)
require.NoError(t, err)
res2, err := newResource(ctx)
require.NoError(t, err)
assert.Equal(t, exp, res2)
}