Skip to content

Commit

Permalink
Merge pull request #52 from reactivegroup/feature/metrics
Browse files Browse the repository at this point in the history
feat: telemetry api
  • Loading branch information
kevinten10 authored Nov 30, 2021
2 parents bb9def8 + 623b3f6 commit f8c07ec
Show file tree
Hide file tree
Showing 128 changed files with 6,428 additions and 1,040 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ For a Maven project, add the following to your pom.xml file:
<dependency>
<groupId>group.rxcloud</groupId>
<artifactId>capa-sdk</artifactId>
<version>1.0.6.RELEASE</version>
<version>1.0.7.RELEASE</version>
</dependency>
...
</dependencies>
Expand All @@ -140,7 +140,7 @@ Sample implementation library:
<dependency>
<groupId>group.rxcloud</groupId>
<artifactId>capa-sdk-spi-demo</artifactId>
<version>1.0.6.RELEASE</version>
<version>1.0.7.RELEASE</version>
</dependency>
...
</dependencies>
Expand Down
4 changes: 2 additions & 2 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ For a Maven project, add the following to your pom.xml file:
<dependency>
<groupId>group.rxcloud</groupId>
<artifactId>capa-sdk</artifactId>
<version>1.0.6.RELEASE</version>
<version>1.0.7.RELEASE</version>
</dependency>
...
</dependencies>
Expand All @@ -138,7 +138,7 @@ Sample implementation library:
<dependency>
<groupId>group.rxcloud</groupId>
<artifactId>capa-sdk-spi-demo</artifactId>
<version>1.0.6.RELEASE</version>
<version>1.0.7.RELEASE</version>
</dependency>
...
</dependencies>
Expand Down
30 changes: 17 additions & 13 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
<parent>
<artifactId>capa-parent</artifactId>
<groupId>group.rxcloud</groupId>
<version>1.0.6.RELEASE</version>
<version>1.0.7.RELEASE</version>
</parent>

<artifactId>capa-examples</artifactId>
<packaging>jar</packaging>
<name>capa-sdk-examples</name>

<properties>
<log4j.version>2.14.1</log4j.version>
<log4j.version>2.8.2</log4j.version>
<logback.version>1.1.7</logback.version>
<slf4j-api.version>1.7.32</slf4j-api.version>
</properties>

<dependencies>
Expand All @@ -58,21 +60,23 @@
</exclusion>
</exclusions>
</dependency>
<!-- log4j2 and slf4j -->
<!-- slf4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<artifactId>log4j-core</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- logback -->
<!-- <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>-->
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.examples.log;

import lombok.extern.slf4j.Slf4j;

/**
* An application cannot use log4j and logback configuration to print logs at the same time.
* So if you want to test the log4j2 configuration to print logs, then you need to copy the resources/xml/log4j2.xml file to the resources directory, and then add log4j-slf4j-impl dependency to the pom file.
* Else if you want to use logback configuration to print logs, then the resources/xml/logback.xml file needs to be copied to the resources path, and the logback-classic dependency needs to be added to the pom file.
* Notice:
* 1. Resources cannot contain log4j2.xml and logback.xml files at the same time,
* 2. log4j-slf4j-impl and logback-classic cannot exist at the same time.
*/
@Slf4j
public class DemoLog {

public static void main(String[] args) {
log.info("test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.examples.telemetry;

import group.rxcloud.capa.component.telemetry.metrics.MetricsReaderConfig;
import group.rxcloud.capa.telemetry.CapaTelemetryClient;
import group.rxcloud.capa.telemetry.CapaTelemetryClientBuilder;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;

import java.util.concurrent.TimeUnit;

public class DemoTelemetryClient {

public static void main(String[] args) throws InterruptedException {
MetricsReaderConfig readerConfig = new MetricsReaderConfig();
readerConfig.setExporterType(MetricTestExporter.class.getName());
readerConfig.setName("metric-reader");
readerConfig.setExportInterval(1, TimeUnit.SECONDS);
CapaTelemetryClient capaTelemetryClient = new CapaTelemetryClientBuilder()
.addProcessor(new TraceProcessor())
.addMetricReaderConfig(readerConfig)
.build();

// tracer
Tracer tracer = capaTelemetryClient.buildTracer("tracer-test")
.block();

LongCounter counter = capaTelemetryClient.buildMeter("meter-test")
.block()
.counterBuilder("counter-test")
.build();

Span span = tracer.spanBuilder("span-test")
.setAttribute("key1", 1)
.setAttribute("key2", 2)
.startSpan();
// working
for (int i = 0; i < 50; i++) {
Thread.sleep(200);
counter.add(i);
}

span.end();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.examples.telemetry;

import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;

import java.util.Collection;

public class MetricTestExporter implements MetricExporter {

@Override
public CompletableResultCode export(Collection<MetricData> metrics) {
metrics.forEach(System.out::println);
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode shutdown() {
return CompletableResultCode.ofSuccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.examples.telemetry;

import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;

public class TraceProcessor implements SpanProcessor {

@Override
public void onStart(Context context, ReadWriteSpan span) {

}

@Override
public boolean isStartRequired() {
return false;
}

@Override
public void onEnd(ReadableSpan span) {
System.out.println(span.toSpanData());
}

@Override
public boolean isEndRequired() {
return true;
}
}
4 changes: 3 additions & 1 deletion examples/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<CapaLog4jAppender name="log"/>
</Appenders>
<Loggers>
<Root level="debug">
<Root level="INFO">
<AppenderRef ref="Console"/>
<AppenderRef ref="log"/>
</Root>
</Loggers>
</Configuration>
31 changes: 31 additions & 0 deletions examples/src/main/resources/xml/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Configuration status="DEBUG">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<CapaLog4jAppender name="log"/>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
<AppenderRef ref="log"/>
</Root>
</Loggers>
</Configuration>
36 changes: 36 additions & 0 deletions examples/src/main/resources/xml/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration>
<!--Log-->
<appender name="Log" class="group.rxcloud.capa.component.log.agent.CapaLogbackAppenderAgent">
</appender>

<logger name="com.ctrip.ibu.market.e2c" level="INFO" additivity="false">
<!-- <appender-ref ref="STDOUT"/>-->
<appender-ref ref="Log"/>
</logger>

<logger name="com.ctrip.ibu.telescope.mock.support" level="INFO" additivity="false">
<!-- <appender-ref ref="STDOUT"/>-->
<appender-ref ref="Log"/>
</logger>

<root level="INFO">
<appender-ref ref="Log"/>
</root>
</configuration>
Loading

0 comments on commit f8c07ec

Please sign in to comment.