Skip to content

Commit

Permalink
Merge branch 'main' into feature/default_attribute_http_transport
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-filipponi authored Jul 17, 2024
2 parents 0f4aa08 + dedcf91 commit da0a543
Show file tree
Hide file tree
Showing 25 changed files with 847 additions and 96 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/protect-released-changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This action against that any PR targeting the main branch touches released
# sections in CHANGELOG file. If change to released CHANGELOG is required, like
# doing a release, add the \"Unlock Released Changelog\" label to disable this action.

name: Protect released changelog

on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled]
jobs:
protect-released-changelog:
runs-on: ubuntu-latest
if: ${{ !contains(github.event.pull_request.labels.*.name, 'Unlock Released Changelog')}}

steps:
- uses: actions/checkout@v4

- name: Protect the released changelog
run: |
./tools/verify_released_changelog.sh ${{ github.base_ref }}
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Support for stdoutlog exporter in `go.opentelemetry.io/contrib/config`. (#5850)
- Add macOS ARM64 platform to the compatibility testing suite. (#5868)
- Added option for extracting attributes from the http request in http transport in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)
- The `go.opentelemetry.io/contrib/bridges/otelzap` module.
This module provides an OpenTelemetry logging bridge for `go.uber.org/zap`. (#5191)
- The `go.opentelemetry.io/contrib/config` package supports configuring `with_resource_constant_labels` for the prometheus exporter. (#5890)
- Add new runtime metrics to `go.opentelemetry.io/contrib/instrumentation/runtime`, which are still disabled by default. (#5870)
- Support for the `OTEL_HTTP_CLIENT_COMPATIBILITY_MODE=http/dup` environment variable in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` to emit attributes for both the v1.20.0 and v1.24.0 semantic conventions. (#5401)


### Removed

- The deprecated `go.opentelemetry.io/contrib/processors/baggagecopy` package is removed. (#5853)

<!-- Released section -->
<!-- Don't change this section unless doing release -->

## [1.28.0/0.53.0/0.22.0/0.8.0/0.3.0/0.1.0] - 2024-07-02

### Added
Expand Down Expand Up @@ -1133,6 +1142,8 @@ First official tagged release of `contrib` repository.
[0.7.0]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.7.0
[0.6.1]: https://github.com/open-telemetry/opentelemetry-go-contrib/releases/tag/v0.6.1

<!-- Released section ended -->

[Go 1.22]: https://go.dev/doc/go1.22
[Go 1.21]: https://go.dev/doc/go1.21
[Go 1.20]: https://go.dev/doc/go1.20
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bridges/otelslog @open-te
bridges/otellogrus/ @open-telemetry/go-approvers @dmathieu @pellared
bridges/prometheus/ @open-telemetry/go-approvers @dashpole
bridges/otelzap/ @open-telemetry/go-approvers @pellared @khushijain21
bridges/otelzerolog/ @open-telemetry/go-approvers @dmathieu @AkhigbeEromo

config/ @open-telemetry/go-approvers @MadVikingGod @pellared @codeboten

Expand Down
3 changes: 3 additions & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ since the last release tag.
git --no-pager log --pretty=oneline "<last tag>..HEAD"
```

Make sure the new released section is under the comment for released section,
like `<!-- Released section -->`, so it is protected from being overwritten in the future.

Be sure to update all the appropriate links at the bottom of the file.

Finally, commit this change to your release branch.
Expand Down
10 changes: 5 additions & 5 deletions bridges/otellogrus/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
// The Level is transformed by using the static offset to the OpenTelemetry
// Severity types. For example:
//
// - [slog.LevelDebug] is transformed to [log.SeverityDebug]
// - [slog.LevelInfo] is transformed to [log.SeverityInfo]
// - [slog.LevelWarn] is transformed to [log.SeverityWarn]
// - [slog.LevelError] is transformed to [log.SeverityError]
// - [logrus.DebugLevel] is transformed to [log.SeverityDebug]
// - [logrus.InfoLevel] is transformed to [log.SeverityTrace4]
// - [logrus.WarnLevel] is transformed to [log.SeverityTrace3]
// - [logrus.ErrorLevel] is transformed to [log.SeverityTrace2]
//
// Attribute values are transformed based on their type into log attributes, or
// Field values are transformed based on their type into log attributes, or
// into a string value if there is no matching type.
//
// [OpenTelemetry]: https://opentelemetry.io/docs/concepts/signals/logs/
Expand Down
31 changes: 30 additions & 1 deletion bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,36 @@
// SPDX-License-Identifier: Apache-2.0

// Package otelzap provides a bridge between the [go.uber.org/zap] and
// OpenTelemetry logging.
// [OpenTelemetry].

// # Record Conversion
//
// The [zapcore.Entry] and [zapcore.Field] are converted to OpenTelemetry [log.Record] in the following
// way:
//
// - Time is set as the Timestamp.
// - Message is set as the Body using a [log.StringValue].
// - Level is transformed and set as the Severity. The SeverityText is also
// set.
// - Fields are transformed and set as the Attributes.
// - Field value of type `context.Context` is used as context when emitting log records.
// - For named loggers, LoggerName is used to access [log.Logger] from [log.LoggerProvider]

//
// The Level is transformed to the OpenTelemetry Severity types in the following way.
//
// - [zapcore.DebugLevel] is transformed to [log.SeverityDebug]
// - [zapcore.InfoLevel] is transformed to [log.SeverityInfo]
// - [zapcore.WarnLevel] is transformed to [log.SeverityWarn]
// - [zapcore.ErrorLevel] is transformed to [log.SeverityError]
// - [zapcore.DPanicLevel] is transformed to [log.SeverityFatal1]
// - [zapcore.PanicLevel] is transformed to [log.SeverityFatal2]
// - [zapcore.FatalLevel] is transformed to [log.SeverityFatal3]
//
// Fields are transformed based on their type into log attributes, or into a string value if there is no matching type.
//
// [OpenTelemetry]: https://opentelemetry.io/docs/concepts/signals/logs/

package otelzap // import "go.opentelemetry.io/contrib/bridges/otelzap"

import (
Expand Down
5 changes: 5 additions & 0 deletions bridges/otelzap/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package otelzap_test

import (
"context"
"os"

"go.opentelemetry.io/contrib/bridges/otelzap"
Expand All @@ -24,6 +25,10 @@ func Example() {

// You can now use your logger in your code.
logger.Info("something really cool")

// You can set context for trace correlation using zap.Any or zap.Reflect
ctx := context.Background()
logger.Info("setting context", zap.Any("context", ctx))
}

func Example_multiple() {
Expand Down
30 changes: 29 additions & 1 deletion bridges/otelzerolog/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,33 @@ func NewHook(name string, options ...Option) *Hook {

// Run handles the passed record, and sends it to OpenTelemetry.
func (h Hook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
// TODO
r := log.Record{}
r.SetSeverity(convertLevel(level))
r.SetBody(log.StringValue(msg))
r.SetSeverityText(level.String())

// TODO: add support for attributes
// This is limited by zerolog's inability to retrieve fields.
// https://github.com/rs/zerolog/issues/493

h.logger.Emit(e.GetCtx(), r)
}

func convertLevel(level zerolog.Level) log.Severity {
switch level {
case zerolog.DebugLevel:
return log.SeverityDebug
case zerolog.InfoLevel:
return log.SeverityInfo
case zerolog.WarnLevel:
return log.SeverityWarn
case zerolog.ErrorLevel:
return log.SeverityError
case zerolog.PanicLevel:
return log.SeverityFatal1
case zerolog.FatalLevel:
return log.SeverityFatal2
default:
return log.SeverityUndefined
}
}
84 changes: 84 additions & 0 deletions bridges/otelzerolog/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
package otelzerolog

import (
"os"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
"go.opentelemetry.io/otel/log/global"
"go.opentelemetry.io/otel/log/logtest"
)

type mockLoggerProvider struct {
Expand Down Expand Up @@ -103,3 +107,83 @@ func TestNewHook(t *testing.T) {
})
}
}

var (
testMessage = "log message"
loggerName = "name"
testKey = "key"
testValue = "value"
testEntry = zerolog.InfoLevel
)

func TestHookRun(t *testing.T) {
rec := logtest.NewRecorder()
hook := NewHook(loggerName, WithLoggerProvider(rec))

logger := zerolog.New(os.Stderr).Hook(hook)

t.Run("Run", func(t *testing.T) {
// Create an event and run the hook
event := logger.Info().Str(testKey, testValue)
hook.Run(event, testEntry, testMessage)

// Check the results
require.Len(t, rec.Result(), 1)
require.Len(t, rec.Result()[0].Records, 1)
got := rec.Result()[0].Records[0]
assert.Equal(t, testMessage, got.Body().AsString())
assert.Equal(t, log.SeverityInfo, got.Severity())
assert.Equal(t, zerolog.InfoLevel.String(), got.SeverityText())
})
}

func TestConvertLevel(t *testing.T) {
tests := []struct {
name string
zerologLevel zerolog.Level
expected log.Severity
}{
{
name: "DebugLevel",
zerologLevel: zerolog.DebugLevel,
expected: log.SeverityDebug,
},
{
name: "InfoLevel",
zerologLevel: zerolog.InfoLevel,
expected: log.SeverityInfo,
},
{
name: "WarnLevel",
zerologLevel: zerolog.WarnLevel,
expected: log.SeverityWarn,
},
{
name: "ErrorLevel",
zerologLevel: zerolog.ErrorLevel,
expected: log.SeverityError,
},
{
name: "PanicLevel",
zerologLevel: zerolog.PanicLevel,
expected: log.SeverityFatal1,
},
{
name: "FatalLevel",
zerologLevel: zerolog.FatalLevel,
expected: log.SeverityFatal2,
},
{
name: "UnknownLevel",
zerologLevel: zerolog.NoLevel, // An unknown level
expected: log.SeverityUndefined,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := convertLevel(tt.zerologLevel)
assert.Equal(t, tt.expected, actual, "severity mismatch")
})
}
}
19 changes: 16 additions & 3 deletions config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet
if prometheusConfig.WithoutUnits != nil && *prometheusConfig.WithoutUnits {
opts = append(opts, otelprom.WithoutUnits())
}
if prometheusConfig.WithResourceConstantLabels != nil {
if prometheusConfig.WithResourceConstantLabels.Included != nil {
var keys []attribute.Key
for _, val := range prometheusConfig.WithResourceConstantLabels.Included {
keys = append(keys, attribute.Key(val))
}
otelprom.WithResourceAsConstantLabels(attribute.NewAllowKeysFilter(keys...))
}
if prometheusConfig.WithResourceConstantLabels.Excluded != nil {
var keys []attribute.Key
for _, val := range prometheusConfig.WithResourceConstantLabels.Included {
keys = append(keys, attribute.Key(val))
}
otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter(keys...))
}
}

reg := prometheus.NewRegistry()
opts = append(opts, otelprom.WithRegisterer(reg))
Expand All @@ -246,9 +262,6 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet
}
addr := fmt.Sprintf("%s:%d", *prometheusConfig.Host, *prometheusConfig.Port)

// TODO: add support for constant label filter
// otelprom.WithResourceAsConstantLabels(attribute.NewDenyKeysFilter()),
// )
reader, err := otelprom.New(opts...)
if err != nil {
return nil, fmt.Errorf("error creating otel prometheus exporter: %w", err)
Expand Down
11 changes: 9 additions & 2 deletions config/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ func TestReader(t *testing.T) {
Pull: &PullMetricReader{
Exporter: MetricExporter{
Prometheus: &Prometheus{
Host: ptr("localhost"),
Port: ptr(8888),
Host: ptr("localhost"),
Port: ptr(8888),
WithoutScopeInfo: ptr(true),
WithoutUnits: ptr(true),
WithoutTypeSuffix: ptr(true),
WithResourceConstantLabels: &IncludeExclude{
Included: []string{"include"},
Excluded: []string{"exclude"},
},
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion detectors/aws/ec2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module go.opentelemetry.io/contrib/detectors/aws/ec2
go 1.21

require (
github.com/aws/aws-sdk-go v1.54.18
github.com/aws/aws-sdk-go v1.54.19
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
Expand Down
4 changes: 2 additions & 2 deletions detectors/aws/ec2/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aws/aws-sdk-go v1.54.18 h1:t8DGtN8A2wEiazoJxeDbfPsbxCKtjoRLuO7jBSgJzo4=
github.com/aws/aws-sdk-go v1.54.18/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go v1.54.19 h1:tyWV+07jagrNiCcGRzRhdtVjQs7Vy41NwsuOcl0IbVI=
github.com/aws/aws-sdk-go v1.54.19/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
12 changes: 12 additions & 0 deletions instrumentation/runtime/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,16 @@
// runtime.go.mem.heap_sys (bytes) Bytes of heap memory obtained from the OS
// runtime.go.mem.live_objects - Number of live objects is the number of cumulative Mallocs - Frees
// runtime.uptime (ms) Milliseconds since application was initialized
//
// When the OTEL_GO_X_DEPRECATED_RUNTIME_METRICS environment variable is set to
// false, the metrics produced are:
//
// go.memory.used By Memory used by the Go runtime.
// go.memory.limit By Go runtime memory limit configured by the user, if a limit exists.
// go.memory.allocated By Memory allocated to the heap by the application.
// go.memory.allocations {allocation} Count of allocations to the heap by the application.
// go.memory.gc.goal By Heap size target for the end of the GC cycle.
// go.goroutine.count {goroutine} Count of live goroutines.
// go.processor.limit {thread} The number of OS threads that can execute user-level Go code simultaneously.
// go.config.gogc % Heap size target percentage configured by the user, otherwise 100.
package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime"
Loading

0 comments on commit da0a543

Please sign in to comment.